在resizeEvent 函数中使用addWidget 向布局添加小部件不是问题,因为它不会立即触发绘图。
您可以通过编译和执行这个简单的项目来轻松验证这一点:
dialog.h:
#pragma once
#include <QDialog>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
void resizeEvent(QResizeEvent *event);
void paintEvent(QPaintEvent *event);
private:
bool resizing;
};
dialog.cpp:
#include "dialog.h"
#include <QResizeEvent>
#include <QVBoxLayout>
#include <QPushButton>
#include <QDebug>
Dialog::Dialog(QWidget *parent)
: QDialog(parent),
resizing(false)
{
new QVBoxLayout(this);
}
Dialog::~Dialog()
{
}
void Dialog::resizeEvent(QResizeEvent *event)
{
resizing = true;
if ( event->size().width() == event->size().height() )
{
qDebug() << "Adding widget";
// Infinite loop or not?
layout()->addWidget(new QPushButton());
}
resizing = false;
}
void Dialog::paintEvent(QPaintEvent *event)
{
if ( resizing )
{
qDebug() << "Painting while resizing widget";
}
}
main.cpp:
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
当您运行程序时,调整对话框的大小以使其为方形(宽度==高度),插入一些按钮(“添加小部件”打印到控制台),但您永远不会看到“调整大小时绘画小部件”消息。这很可能是因为addWidget 设置了一个脏显示标志,该标志稍后由框架处理。它使显示无效,但不会立即重新绘制。
所以你所做的很好,并且不违反框架要求(“不需要(或应该)在这个处理程序内完成绘图。”)。
但是,如果您不确定(也许绘画可以立即在不同的操作系统上操作,或者在未来的 Qt 版本中......您无法确定),您也可以通过发出信号来延迟插入使用Qt::QueuedConnection 连接到一个槽,这个槽将“稍后”执行,然后调用addWidget,保证它在resizeEvent 函数之外完成。