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)其他原因


原创粉丝点击