我想在QT中制作一个工具按钮列表。工具按钮应出现在 scolla 区域中。单击按钮时应出现此列表。我已经编写了代码并且它可以工作,除了我必须按两次按钮才能让列表出现。这是我的代码:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
find_btn.setParent(ui->centralWidget);
find_btn.setGeometry(480,250,130,132);
viewport.setLayout(&scrollLayout);
scrollArea.setParent(ui->centralWidget);
scrollArea.setGeometry(0,116,339,404);
scrollArea.setWidget(&viewport);
connect(&find_btn,SIGNAL(clicked()),this,SLOT(import()));
}
void MainWindow::import()
{
button.setCheckable(true);
button.setMinimumSize(317,60);
button2.setCheckable(true);
button2.setMinimumSize(317,60);
scrollLayout.addWidget(&button);
scrollLayout.addWidget(&button2);
viewport.adjustSize();
}
因此,当我按下“find_btn”时,应该会出现带有按钮的滚动区域。目前,带有按钮的滚动区域出现,但只有在我单击“find_btn”两次之后。
我想我必须更新滚动区域或类似的东西。也许连接引起了问题?有人可以帮忙吗?
您可以尝试以下几种选择:
viewport.update();
或
scrollArea.viewport()->update();
或
QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
或它们的任意组合。
在再次按下按钮强制重绘之前,GUI 可能不会重绘。