且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Qt中调整子窗口小部件的大小后调整大小

更新时间:2022-05-06 15:51:08

调用 adjustSize()时,以前的调用都没有任何可见的效果,因为这些效果仅在事件循环已运行.多次调用它的操作可能是间接地从事件循环中耗尽了一些事件,就像通过 exec()或静态方法显示 QMessageBox 一样.

When you call the adjustSize(), none of the previous calls had any visible effects, since those effects are caused only when the event loop has been run. What you do by calling it multiple times is probably indirectly draining some events from the event loop, same with displaying a QMessageBox via exec() or a static method.

您需要从事件循环中调用 adjustSize .由于它不可调用,因此需要在小部件类(或辅助类)中做到这一点.

You need to invoke adjustSize from the event loop. Since it is not invokable, you need to make it so in your widget class (or in a helper class).

// Interface
class MyWidget : public QWidget {
  Q_OBJECT
  Q_INVOKABLE void adjustSize() { QWidget::adjustSize(); }
  ...
};

// Implementation
void MyWidget::myMethod() {
  // ...
  ui->graphicsView->show();
  ui->graphicsView->updateGeometry();

  QMetaObject::invokeMethod(this, "adjustSize", Qt::QueuedConnection);
}