Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How to change the text of a pushbuttom with a dialog?

    General and Desktop
    4
    8
    840
    Loading More Posts
    • 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.
    • M
      MasterBlade last edited by

      Hello, I have a pushbuttom that looks like this.
      0_1524470999533_1.PNG

      When a user clicks it, I want a dialog appear, then the user types in 3 integers and clicks OK. Then the first 2 integers appear on the pushbuttom.
      0_1524471006987_2.PNG

      I tried to make a new QtDesignerFormClass for that dialog, and used on_Pushbuttom_clicked(), but I didn't know how to return those 3 integers. Maybe this is not the best way to do it. Any suggestions?

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @MasterBlade last edited by jsulm

        @MasterBlade Implement getter methods in your dialog which return these integers.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply Reply Quote 0
        • M
          MasterBlade @jsulm last edited by MasterBlade

          @jsulm said in How to change the text of a pushbuttom with a dialog?:

          @MasterBlade Implement getter methods in your dialog which return these integers.

          But this dialog is in a different class and file. How can I set value to another class and file?

          I used these functions:

          void Database::on_Numbers_clicked()
          {
          DBNoChange *db = new DBNoChange;
          db->setWindowTitle(tr("Set Value"));
          db->show();
          }

          void DBNoChange::on_Confirm_accepted()
          {
          bool ok1, ok2, ok3;
          uint16_t no1, no2, no3;
          no1 = ui->no1_value->text().toUInt(&ok1);
          no2 = ui->no2_value->text().toUInt(&ok2);
          no3 = ui->no3_value->text().toUInt(&ok3);
          if (ok1 && ok2 && ok3){
          Database::Setno1(no1);
          Database::Setno2(no2);
          Database::Setno3(no3);
          close();
          }
          else{
          QMessageBox::warning(this, "Input Error", "Please Enter Valid Integers.");
          }
          }

          void DBNoChange::on_Confirm_rejected()
          {
          close();
          }

          But I got an error message saying "error: cannot call member function 'void Database::Setno1(uint16_t)' without object"
          Database::Setno1(no1);
          ^

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @MasterBlade last edited by

            @MasterBlade This is actually way easier:

            void Database::on_Numbers_clicked()
            {
                DBNoChange db;
                db.setWindowTitle(tr("Set Value"));
                db.exec();
                // Here use db.number1(), db.number2(), db.number3() to get the numbers
            }
            
            int DBNoChange::number1()
            {
                return ui->lineEdit1->text().toInt();
            }
            
            // same for number2() and number3()
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply Reply Quote 3
            • M
              MasterBlade @jsulm last edited by

              @jsulm said in How to change the text of a pushbuttom with a dialog?:

              @MasterBlade This is actually way easier:

              void Database::on_Numbers_clicked()
              {
                  DBNoChange db;
                  db.setWindowTitle(tr("Set Value"));
                  db.exec();
                  // Here use db.number1(), db.number2(), db.number3() to get the numbers
              }
              
              int DBNoChange::number1()
              {
                  return ui->lineEdit1->text().toInt();
              }
              
              // same for number2() and number3()
              

              That's helpful. Thanks!

              My next problem is that when I click 'X' on top-right corner of "Set Value", I can only get some random numbers from its functions. I even tried to give those variables new values on QObject::destroyed(); but that won't work.

              Is there a way to prevent it?
              0_1524557142842_1.PNG
              1_1524557142843_2.PNG

              void Database::on_Numbers_clicked()
              {
                  uint16_t no1, no2, no3;
                  DBNoChange db;
                  db.setWindowTitle(tr("Set Value"));
                  db.Setno1(ui->no1->text().toUInt());
                  db.Setno2(ui->no2->text().toUInt());
                  db.Setno3(ui->no3->text().toUInt());
                  db.exec();
                  no1 = db.Getno1();
                  no2 = db.Getno2();
                  no3 = db.Getno3();
                  ui->no1->setText(QString::number(no1).rightJustified(3, '0'));
                  ui->no2->setText(QString::number(no2).rightJustified(3, '0'));
                  ui->no3->setText(QString::number(no3).rightJustified(3, '0'));
              
                  if (no1 || no2 || no3){
                      ui->Numbers->setText(tr("Card: ") + ui->no1->text() + "_" + ui->no2->text());
                  }
              }
              
              void DBNoChange::on_Confirm_accepted()
              {
                  uint16_t no1v, no2v, no3v;
                  bool ok1, ok2, ok3;
                  no1v = ui->no1_value->text().toUInt(&ok1);
                  no2v = ui->no2_value->text().toUInt(&ok2);
                  no3v = ui->no3_value->text().toUInt(&ok3);
                  if (ok1 && ok2 && ok3){
                      no1 = no1v;
                      no2 = no2v;
                      no3 = no3v;
                      close();
                  }
                  else{
                      QMessageBox::warning(this, "Input Error", "Please Enter Valid Integers.");
                  }
              }
              
              void DBNoChange::on_Confirm_rejected()
              {
                  no1 = ui->no1_value->text().toUInt();
                  no2 = ui->no2_value->text().toUInt();
                  no3 = ui->no3_value->text().toUInt();
                  close();
              }
              
              void DBNoChange::Setno1(uint16_t no1)
              {
                  ui->no1_value->setText(QString::number(no1));
              }
              
              void DBNoChange::Setno2(uint16_t no2)
              {
                  ui->no2_value->setText(QString::number(no2));
              }
              
              void DBNoChange::Setno3(uint16_t no3)
              {
                  ui->no3_value->setText(QString::number(no3));
              }
              
              void DBNoChange::on_DBNoChange_destroyed()
              {
                  no1 = ui->no1_value->text().toUInt();
                  no2 = ui->no2_value->text().toUInt();
                  no3 = ui->no3_value->text().toUInt();
              }
              
              
              JonB 1 Reply Last reply Reply Quote 0
              • JonB
                JonB @MasterBlade last edited by JonB

                @MasterBlade said in How to change the text of a pushbuttom with a dialog?:

                My next problem is that when I click 'X' on top-right corner of "Set Value", I can only get some random numbers from its functions.

                Although we can tell you how to get this right if you really want, why do you want to know what values those widgets have when the user clicks the X close button in your dialog?

                If the user closes the dialog via the X it's the same as if he clicks your Cancel button. The return result from the QDialog::exec() will tell you whether the user clicked the OK button versus the Cancel/X buttons to exit the dialog, and if it's the latter you really ought not care/examine the values in the widgets...?

                M 1 Reply Last reply Reply Quote 2
                • M
                  MasterBlade @JonB last edited by MasterBlade

                  @JonB said in How to change the text of a pushbuttom with a dialog?:

                  @MasterBlade said in How to change the text of a pushbuttom with a dialog?:

                  My next problem is that when I click 'X' on top-right corner of "Set Value", I can only get some random numbers from its functions.

                  Although we can tell you how to get this right if you really want, why do you want to know what values those widgets have when the user clicks the X close button in your dialog?

                  If the user closes the dialog via the X it's the same as if he clicks your Cancel button. The return result from the QDialog::exec() will tell you whether the user clicked the OK button versus the Cancel/X buttons to exit the dialog, and if it's the latter you really ought not care/examine the values in the widgets...?

                  Sorry I misunderstood it.
                  So I can use if (db.exec() == QDialog::Accepted) to determine if user clicked 'Yes'?

                  And please tell me more on why I get random numbers if a user clicks 'X'. I'm a real noob and just want to know more about the mechanism of how things were done.

                  1 Reply Last reply Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion last edited by mrjj

                    Hi

                    • if (db.exec() == QDialog::Accepted)
                      to determine if user clicked 'Yes'?

                    Yes. it stays inside exec() until user accept or reject the dialog.
                    So only when result is QDialog::Accepted , you should use the numbers etc.

                    • And please tell me more on why I get random numbers if a user clicks 'X'.
                      Most likely you acces the variables when no values have been assigned.

                      uint16_t no1, no2, no3;
                      those will be random numbers if not assigned and u use them.
                      try
                      uint16_t no1 = 0, no2 = 0, no3 = 0;

                    and see if the random numbers are now zero.

                    1 Reply Last reply Reply Quote 2
                    • First post
                      Last post