Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change the widgets property if I use QT Designer

I am using Visual Studio 2022 and C++ QT 5.14.2.

I used QT Designer to setup a PushButton and a LineEdit, and I gave them some initial property. But I met problem in button clicked signal slot function, this is my code:

void SocketChat::on_connectButton_clicked() {
    appendMessage(ui.connectButton->text());  // output button's text
    if (ui.addrEdit->isEnabled() && ui.portEdit->isEnabled()) {
        // disabled
        ui.addrEdit->setEnabled(false);
        ui.connectButton->setText(QStringLiteral("Disconnect"));
        appendMessage(ui.connectButton->text());  // output button's text
    }
    if (ui.addrEdit->isEnabled() && !ui.portEdit->isEnabled()) {
        ui_notification->setText(QStringLiteral("Please stop listening."));
    }
    if (!ui.addrEdit->isEnabled() && ui.portEdit->isEnabled()) {
        // enabled
        ui.addrEdit->setEnabled(true);
        ui.connectButton->setText(QStringLiteral("Connect"));
    }
}

Button’s text was assigned to ‘Connect’ in QT Designer, and after the function finished, the button’s text is still ‘Connect’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

appendMessage function output two words for every time I clicked:

Connect
Disconnect

The click function works properly but and the button’s text was changed successfully in this function. But after the function finished, everything recovered. The button’s text is still ‘Connect’. I tried repaint() and update(), they’re unable to work.

How can I change the widgets’ property? It seems that these properties cannot be initialized once they have been changed.

>Solution :

You are missing some else keywords.

Note that when your button is clicked the first if-block (labeled "// disabled") is execute and it disables the ui.addrEdit button. Due to the missing else keyword the program then also checks the conditions of the other if statements. And since the button is now disabled the last condition evaluates to true and therefore the block labeled "// enabled" is also run. This is not what you want.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading