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. Qt value from lineEdit and isChecked from QCheckBox not working as expected

Qt value from lineEdit and isChecked from QCheckBox not working as expected

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt 4.8connect failuredisplay qstring
8 Posts 4 Posters 2.9k 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.
  • N Offline
    N Offline
    Nano-95
    wrote on 22 Jun 2018, 15:47 last edited by Nano-95
    #1

    I'm trying to make a QMessageBox pop up when i press a submit button, which works fine, however the message inside is dependent based on whether some checkboxes are checked, and on the value of the lineEdit, as such:

    QApplication a(argc, argv);
    QMainWindow *w = new QMainWindow();
    .
    .
    .
    QPushButton but_submit("Submit");
    QMessageBox msg_submit;
    
    // The following will be so that we can get the val of the GPA and then add it
    // To the full message that will contain the info of person
    QString submit_string = "Hello, here's the summary: \n";
    submit_string += "Here\'s your value: " +  line_misc[0]->text() +  ".\n";
    if (chk_art->isChecked())
        submit_string += "Art major!";
    msg_submit.setText(submit_string);
    .
    . 
    .
    QObject::connect(&but_submit, SIGNAL(clicked()), &msg_submit, SLOT(exec()));
    
    w->show();
    return a.exec();
    

    Everything that is not in the code here i have defined or initialized, and i don't have any warnings or errors when i run the code, all is well, everything is displayed, but it's like nothing is connected...

    The message box appears but the message is "Hello, here's the summary: Here's your value: ." When i'm really expecting to see a number, or the art major comment as well if i have the checkbox checked, but i do not, unfortunately.

    I've gone through the docs and tried variations, such as using a spinBox and slider, and using the property function value() to just get the value and wrapping it in QString::number(), as such:

        QSlider *slider = new QSlider(Qt::Horizontal);
        QSpinBox *spinBox = new QSpinBox;
        spinBox->setRange(0, 130);
        slider->setRange(0, 130);
        QObject::connect(spinBox, SIGNAL(valueChanged(int)),
            slider, SLOT(setValue(int)));
        QObject::connect(slider, SIGNAL(valueChanged(int)),
            spinBox, SLOT(setValue(int)));
        spinBox->setValue(35);
        lay_grades->addWidget(spinBox);
        lay_grades->addWidget(slider);    
    

    but no matter what i change the value to, it's like setValue property isn't called when I change the value, or the slot valueChanged() doesn't run either.

    Am i doing something wrong?

    Thanks for your time

    EDIT: Here is a link to the code http://pasted.co/c941c495

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 22 Jun 2018, 17:09 last edited by
      #2

      @Nano-95 said in Qt value from lineEdit and isChecked from QCheckBox not working as expected:

      line_misc

      where / how is this object filled/set? The same goes for chk_art ...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      N 1 Reply Last reply 22 Jun 2018, 18:08
      0
      • C Christian Ehrlicher
        22 Jun 2018, 17:09

        @Nano-95 said in Qt value from lineEdit and isChecked from QCheckBox not working as expected:

        line_misc

        where / how is this object filled/set? The same goes for chk_art ...

        N Offline
        N Offline
        Nano-95
        wrote on 22 Jun 2018, 18:08 last edited by
        #3

        @Christian-Ehrlicher Hey, sorry it's just a lengthy .cpp, so i did not want to put all of the code

        The line_misc[miscRows] array is initialized in a for loop

        where miscRows is: enum { miscRows = 4 };

        and I have some checkboxes for different hobbies, chk_art being one of them:

        for (int i = 0; i < miscRows; ++i) {
                line_misc[i] = new QLineEdit;
                lay_grades->addWidget(line_misc[i]);
            }
        
        QCheckBox *chk_art = new QCheckBox();
        chk_art->setText("Art");
        

        These are all in a layout already added as widgets, and are visual when i run the code

        P 1 Reply Last reply 22 Jun 2018, 18:44
        0
        • N Nano-95
          22 Jun 2018, 18:08

          @Christian-Ehrlicher Hey, sorry it's just a lengthy .cpp, so i did not want to put all of the code

          The line_misc[miscRows] array is initialized in a for loop

          where miscRows is: enum { miscRows = 4 };

          and I have some checkboxes for different hobbies, chk_art being one of them:

          for (int i = 0; i < miscRows; ++i) {
                  line_misc[i] = new QLineEdit;
                  lay_grades->addWidget(line_misc[i]);
              }
          
          QCheckBox *chk_art = new QCheckBox();
          chk_art->setText("Art");
          

          These are all in a layout already added as widgets, and are visual when i run the code

          P Offline
          P Offline
          Pablo J. Rogina
          wrote on 22 Jun 2018, 18:44 last edited by
          #4

          @Nano-95 without seeing the whole source code, I suspect that you need an interim step from the moment submit button is clicked until message box is display to build the message with all the current values. Pseudo-code:

          QObject::connect(&but_submit, SIGNAL(clicked()), &this, SLOT(displayMBox()));
          ...
          void displayMBox() {
              QString submit_string = "Hello, here's the summary: \n";
              submit_string += "Here\'s your value: " +  line_misc[0]->text() +  ".\n";
              if (chk_art->isChecked())
                  submit_string += "Art major!";
               msg_submit.setText(submit_string);
               msg_submit.exec()
          }
          

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          N 1 Reply Last reply 22 Jun 2018, 19:54
          2
          • P Pablo J. Rogina
            22 Jun 2018, 18:44

            @Nano-95 without seeing the whole source code, I suspect that you need an interim step from the moment submit button is clicked until message box is display to build the message with all the current values. Pseudo-code:

            QObject::connect(&but_submit, SIGNAL(clicked()), &this, SLOT(displayMBox()));
            ...
            void displayMBox() {
                QString submit_string = "Hello, here's the summary: \n";
                submit_string += "Here\'s your value: " +  line_misc[0]->text() +  ".\n";
                if (chk_art->isChecked())
                    submit_string += "Art major!";
                 msg_submit.setText(submit_string);
                 msg_submit.exec()
            }
            
            N Offline
            N Offline
            Nano-95
            wrote on 22 Jun 2018, 19:54 last edited by
            #5

            @Pablo-J.-Rogina
            Thanks for the response, I'm assuming that the use of &this would work inside a class... But i get errors due to the fact that this is all inside the main.cpp?
            Here's the code: http://pasted.co/c941c495

            I got some errors, so i fixed it up a bit, nothing happened

            Wit the code in the link, any ideas?

            Thanks so much for your time!

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 23 Jun 2018, 11:07 last edited by mrjj
              #6

              Hi
              One error is that you use "this" in main.cpp.
              There is no this there so that cannot work.
              You should always check the return of connect to see if it did work.
              connect return true /false.

              So this line
              QObject::connect(&but_submit, SIGNAL(clicked()), &this, SLOT(displayMBox()));
              says "this" for main.cpp and that wont work.

              you can however use lambda with the new syntax. Note the & to have access to the widgets.

              QObject::connect(&but_submit, &QPushButton::clicked, [&]() {
                  QString submit_string = "Hello, here's the summary: \n";
                  submit_string += "Here\'s your value: " +  line_misc[0]->text() +  ".\n";
                  if (chk_art->isChecked())
                  { submit_string += "Art major!"; }
                  msg_submit.setText(submit_string);
                  msg_submit.exec();
                });
                //void displayMBox()  code is move inside the lambda
              

              I can see you tried the lamda before so not sure why you was not happy with that ?

              N 1 Reply Last reply 23 Jun 2018, 16:03
              1
              • mrjjM mrjj
                23 Jun 2018, 11:07

                Hi
                One error is that you use "this" in main.cpp.
                There is no this there so that cannot work.
                You should always check the return of connect to see if it did work.
                connect return true /false.

                So this line
                QObject::connect(&but_submit, SIGNAL(clicked()), &this, SLOT(displayMBox()));
                says "this" for main.cpp and that wont work.

                you can however use lambda with the new syntax. Note the & to have access to the widgets.

                QObject::connect(&but_submit, &QPushButton::clicked, [&]() {
                    QString submit_string = "Hello, here's the summary: \n";
                    submit_string += "Here\'s your value: " +  line_misc[0]->text() +  ".\n";
                    if (chk_art->isChecked())
                    { submit_string += "Art major!"; }
                    msg_submit.setText(submit_string);
                    msg_submit.exec();
                  });
                  //void displayMBox()  code is move inside the lambda
                

                I can see you tried the lamda before so not sure why you was not happy with that ?

                N Offline
                N Offline
                Nano-95
                wrote on 23 Jun 2018, 16:03 last edited by
                #7

                @mrjj
                First, thanks for your response,

                I tried it, yes, \ but it never worked :L I've tried variations to stop receiving errors but I get this with this code

                QObject::connect(&but_submit, SIGNAL(clicked()), [&]() {
                        QString submit_string = "Hello, here's the summary: \n";
                        submit_string += "Here\'s your value: " +  line_misc[0]->text() +  ".\n";
                        if (chk_art->isChecked())
                        { submit_string += "Art major!"; }
                        msg_submit.setText(submit_string);
                        msg_submit.exec();
                      });
                
                

                "no matching function for call to 'QObject::connect(QPushButton*, const char*, main(int, char**)::<lambda()>)'"
                with what may be a relevant warning "warning: lambda expressions only available with -std=c++0x or -std=gnu++0x [enabled by default]"

                When i try your code, I get a different error which is why i tried switching it up

                /usr/local/Trolltech/Qt-4.8.5/include/QtGui/qabstractbutton.h:127: error: 'void QAbstractButton::clicked(bool)' is protected

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 23 Jun 2018, 20:25 last edited by mrjj
                  #8

                  Hi
                  You are mixing old syntax with lambda which it cannot do.
                  With lambdas you cannot use the SIGNAL() macro.
                  Has to be the new syntax.

                  I used

                  QObject::connect(&but_submit, &QPushButton::clicked, [&]() {...
                  

                  which compiled fine and did show the entered string.

                  But you seems to be using
                  Qt-4.8.5 at and new syntax first came in Qt5
                  so i guess that is why you get errors.

                  So basically, with that old Qt , you cannot do it in main.
                  You need a QOBJECT based object to send signals to.
                  In that old Qt, its not possible to connect to functions.
                  Only to member slots in a class.

                  You must use that so old Qt ?

                  1 Reply Last reply
                  3

                  1/8

                  22 Jun 2018, 15:47

                  • Login

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