Search code examples
qtsignalsslot

Have to push QPushbutton twice


I want to make a list of toolbuttons in QT. The toolbuttons should appear in a scollarea. This list should appear when a pushbutton is clicked. I have made the code and it works, exept that I have to push the pushbutton twice in order to let the list appear. Here is my code:

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();
}

So when I push the "find_btn", the scrollarea with the buttons inside should appear. At the moment the scrollarea with buttons appears, but only after I click the "find_btn" twice.

I guess I have to update the scrollarea or something like that. Maybe the connect is causing problems? Can anybody help?


Solution

  • There are a couple of options you could try:

    viewport.update();
    

    or

    scrollArea.viewport()->update();
    

    or

    QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
    

    or any combination of them.

    Probably the GUI is not redrawn until a redraw is forced by pressing the button again.