Qt的adjectSize()和update()
来源:互联网 发布:手机透视物体软件 编辑:程序博客网 时间:2024/06/05 19:39
一、adjectSize();
更新字体,调用adjectSize()的同时,会通过SizeHint()来确定组件的大小
例如:
QSize CharacterWidget::sizeHint() const//也就是说一个 widget 该有多大,它的一个参考来源就是这个sizeHint 属性的值,而这个值由 sizeHint() 函数来确定。{// qDebug()<<"调用sizeHint()"<<'\n'; //65536 return QSize(columns*squareSize, (65536/columns)*squareSize); /* * QSize::QSize(int width, int height) * Constructs a size with the given width and height. * 表示显示组件的大小,宽和长; */}
二、update();
update()的主要作用在于可以调用update()的同时,系统会去调用paintEvent()函数来更新整体组件
如下:
void CharacterWidget::paintEvent(QPaintEvent *event){// qDebug()<<"调用paintEvent"<<'\n'; QPainter painter(this); painter.fillRect(event->rect(), QBrush(Qt::white)); painter.setFont(displayFont);//! [6]//! [7] QRect redrawRect = event->rect(); int beginRow = redrawRect.top()/squareSize;//表示起始的表格行数beginRow int endRow = redrawRect.bottom()/squareSize;//表示终止的表格行数为endRow int beginColumn = redrawRect.left()/squareSize;//表示起始的表格列数为beginColumn int endColumn = redrawRect.right()/squareSize;//表示终止的表格列数为endColumn // // qDebug()<<redrawRect<< beginRow<<" "<< endRow<<" "<<beginColumn <<" "<< endColumn<<'\n'; ////! [7]//! [8] painter.setPen(QPen(Qt::gray));//将表格线的颜色设为褐色 for (int row = beginRow; row <= endRow; ++row) { for (int column = beginColumn; column <= endColumn; ++column) { painter.drawRect(column*squareSize, row*squareSize, squareSize, squareSize); }//! [8] //! [9] }//! [9]//! [10] QFontMetrics fontMetrics(displayFont); /* *QFontMetrics functions calculate the size of characters and strings for a given font. * 即计算了字体大小 * */// qDebug()<<fontMetrics<<'\n';无法直接输出 painter.setPen(QPen(Qt::black));//表示设置字体为黑色,因为painter机制为状态机机制 for (int row = beginRow; row <= endRow; ++row) { for (int column = beginColumn; column <= endColumn; ++column) { int key = row*columns + column;//计算出键值 painter.setClipRect(column*squareSize, row*squareSize, squareSize, squareSize); /* * void QPainter::setClipRect(int x, int y, int width, int height, Qt::ClipOperation operation = Qt::ReplaceClip) * Enables clipping, and sets the clip region to the rectangle beginning at (x, y) with the given width and height. * 官方文档解释为: * We do not need to take into account the difference between the area displayed in the viewport and the area we are drawing on * because everything outside the visible area will be clipped. * 在QPainter()中通过QPainter::setClipRect设定一个区域,我们绘图则只能在这个区域,此区域外绘图都是无效的。 * 暂时不理解 * ?????? */ if (key == lastKey) painter.fillRect(column*squareSize + 1, row*squareSize + 1, squareSize, squareSize, QBrush(Qt::red)); /* *void QPainter::fillRect(int x, int y, int width, int height, const QBrush &brush) * Fills the rectangle beginning at (x, y) with the given width and height, using the given brush. */ painter.drawText(column*squareSize + (squareSize / 2) - fontMetrics.width(QChar(key))/2, row*squareSize + 4 + fontMetrics.ascent(), QString(QChar(key))); /* * Draws the given text at position (x, y), using the painter's currently defined text direction. * 该函数用于在表格中绘制字符 */ } }}不仅如此:
paintEvent()
A paint event is a request to repaint all or part of a widget. It can happen for one of the following reasons:
- repaint() orupdate() was invoked,
- the widget was obscured and has now been uncovered, or
- many other reasons.
表示
(1)、当我们调用了repaint()和update()函数
(2)表示组件被销毁并且现在没有被覆盖(没太懂)
(3)其他原因
阅读全文
0 0
- Qt的adjectSize()和update()
- QT update和repaint的区别
- Qt 中update()和repaint()的区别
- Qt中update()和repaint()的区别
- QT update和repaint的区别
- Qt中的若干问题(update)
- 关于Qt 中update()和repaint()的区别
- 关于Qt 中update()和repaint()的区别
- 关于Qt 中update()和repaint()的区别
- 关于Qt 中update()和repaint()的区别
- 关于Qt 中update()和repaint()的区别
- 关于Qt 中update()和repaint()的区别
- Qt中update()和repaint()的源码分析
- 关于Qt 中update()和repaint()的区别
- qt的update与repaint
- qt的update与repaint
- (转)oracle for update和for update nowait的区别
- Qt重绘之update,repaint详解(转)
- Java泛型
- 短路
- 创建cocos2d工程
- leetcode--Remove Duplicates from Sorted Array II
- Java生成UUID
- Qt的adjectSize()和update()
- 栈的应用 数组模拟 (指针)
- cmake 出错可能原因有CMakeCache
- 鼠标事件中event对象的坐标
- Spring Boot Web 开发注解篇
- 并发服务器的实现
- iBatis2学习笔记:SqlMap的配置总结(18条)
- MTK Android Driver:EINT
- Linux下查看Tomcat的控制台输出信息