Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with enable method for QPushButton
Forum Updated to NodeBB v4.3 + New Features

Problem with enable method for QPushButton

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 381 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BeastBook
    wrote on last edited by BeastBook
    #1

    Hi i want to enable button when my text fields are not empty and disable when one or both fields are empty. I tried multiple combinations with logical operators.Any idea how to deal with this

    //by default it is off
    confirm->setEnabled(false);
    if(userTxt->text()!="" && passTxt->text()!="") //if they are not empty
    {
    confirm->setEnabled(true);
    }

    J.HilkJ 1 Reply Last reply
    0
    • B BeastBook

      Hi i want to enable button when my text fields are not empty and disable when one or both fields are empty. I tried multiple combinations with logical operators.Any idea how to deal with this

      //by default it is off
      confirm->setEnabled(false);
      if(userTxt->text()!="" && passTxt->text()!="") //if they are not empty
      {
      confirm->setEnabled(true);
      }

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @BeastBook

      a bit complex of an example, but I wanted everything in main x)

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QWidget parent;
          parent.show();
      
          QLineEdit *edit1 = new QLineEdit;
          QLineEdit *edit2 = new QLineEdit;
          QPushButton *btn = new QPushButton("OK");
          btn->setDisabled(true);
      
          QHBoxLayout *layout = new QHBoxLayout(&parent);
          layout->addWidget(edit1);
          layout->addWidget(edit2);
          layout->addWidget(btn);
      
          auto check = [edit1, edit2, btn]()->void{
              btn->setDisabled(edit1->text().isEmpty() || edit2->text().isEmpty());
          };
      
          QObject::connect(edit1, &QLineEdit::textChanged, [=]()->void{ check();});
          QObject::connect(edit2, &QLineEdit::textChanged, [=]()->void{ check();});
      
          return  a.exec();
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3
      • B Offline
        B Offline
        BeastBook
        wrote on last edited by
        #3

        Thank you for this.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved