Home > Software engineering >  How to change the widgets property if I use QT Designer
How to change the widgets property if I use QT Designer

Time:12-30

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'.

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.

CodePudding user response:

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.

  • Related