How to update a window in QT?

后端 未结 1 1039
不知归路
不知归路 2021-01-29 02:12

Im working on a simple project in QtCreator where you input text into a line_edit which then gets printed after clicking a button. It works but I need to resize the window in or

相关标签:
1条回答
  • 2021-01-29 02:46

    The problem is not the update of the GUI but the QLabel does not change size, the initial size depends on the initial text, and if you set a text with larger size only part of the text will be displayed. To adjust the size of the label to the size of the text you must use adjustSize():

    void MainWindow::setText()
    {
        QString temp = ui->inputText->text();
        ui->displayLabel->setText(temp);
        ui->displayLabel->adjustSize();
    }
    

    On the other hand in Qt5 it is advisable to use the new connection syntax since they have several advantages as indicated by the docs, in your case you must change your code to:

    connect(ui->textBtn, &QPushButton::clicked, this, &MainWindow::setText);
    
    0 讨论(0)
提交回复
热议问题