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. Making a checkbox checked

Making a checkbox checked

Scheduled Pinned Locked Moved General and Desktop
16 Posts 4 Posters 36.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.
  • R Offline
    R Offline
    roboterprogrammer
    wrote on last edited by
    #1

    hi,

    in the ui, i wanna have some checkboxes who are not checkable by the user.
    they are there to indicate some status of the program and the tcpip connection.

    for that i have to use the "setchecked" bool.

    1. how can i make this static from the main.cpp?
      s.th. like

    w.chkbox_client.setchecked

    1. the same syntax will then be usable to make a slot-function connection to the qtcpsocket right.
      so whenever the connection is etablished, the checkbox will be on.

    then the next step, to have a disconnect-uncheck function will be the analoge from it.

    thanks for hinds.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      If you want the inital state of the checkboxes to be checked and disabled either set both properties in the UI designer or manually set them right after the call to setupUi().

      The most sensible approach is most probably adding two private slots enableCheckboxes() and disableCheckboxes() which contain the corresponding code and then can be connected to the QTcpSocket connected and disconnected signal.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        roboterprogrammer
        wrote on last edited by
        #3

        y
        thanks thats the good way

        i try to have this method in the robbie20 (ui class)

        @void Robbie20::chkbox_client()
        {
        ui->chkbox_client->setChecked(1);
        ui->chkbox_client->isChecked();
        }
        @
        and try to run it after initialisation in the main.cpp
        but nothing get checked:

        @int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);

        Robbie20 w;
        w.show();
        
        w.loadTextFile();
        w.chkbox_client();
        

        ....
        }@

        do you c any mistake? why isn't it checked, and how can i fix it?

        1 Reply Last reply
        1
        • V Offline
          V Offline
          veeraps
          wrote on last edited by
          #4

          Would suggest to use QLabel with different icons/pixmaps to indicate the TCP/IP connection status like Red means no connection, Green means connected etc something like that rather than a CheckBox that is operated internally - mean it is my suggestion.

          Because in your application, the CheckBox is used as a kind of indication whether TCP/IP connections is connected or not, so suggest to use some kind of Display Widgets which might add more meaning to the UI controls.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            roboterprogrammer
            wrote on last edited by
            #5

            hi,
            thanks,
            i made already the gui and there are only 3 checkboxes, so its not so confusing.

            and i would really like to know how to make those checkboxes checked.

            1 Reply Last reply
            0
            • V Offline
              V Offline
              veeraps
              wrote on last edited by
              #6

              If you have void Robbie20::chkbox_client() method in Robbie20, why don't you call chkbox_client() within loadTextFile() method, which is also a method of Robbie20. Try to minimize the call in main and encapsulate them in your Concrete Class.

              How about showing Robbie20 Widget once you are done with loading Text File and changing check box state.

              @
              int main(int argc, char *argv[])
              {
              QApplication a(argc, argv);
              Robbie20 w;

                  w.loadTextFile();
                  w.chkbox_client();
              
                  w.show();
              ....
              }
              

              @

              1 Reply Last reply
              0
              • V Offline
                V Offline
                veeraps
                wrote on last edited by
                #7

                Pardon me - actually it shouldn't make any difference whether you call setChecked(1) after show() call or before. Tried a sample code, to just confirm it, and it is working for me.

                Just added a CheckBox to the UI and added a method as you said and called the same after showing the widget, I see the checkbox is checked.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  roboterprogrammer
                  wrote on last edited by
                  #8

                  hi,
                  i will show screenshot when project finished, because we have to buy licence before using it on robot.

                  ah the error was that i made the checkbox "not checkable"
                  but now when it is checkable, the user can uncheck it. thats not mentioned.
                  so i always have to
                  make it checkable, then check/uncheck it and then make it again uncheckable ...

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #9

                    setChecked() takes a boolean, not an integer; the call to isChecked() is unnecessary.

                    You might post a small, compilable example that reproduces your problem.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      veeraps
                      wrote on last edited by
                      #10

                      That's correct - setChecked() will work provided setCheckable() is enabled.

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        roboterprogrammer
                        wrote on last edited by
                        #11

                        thank u guys for helping.

                        when i only make
                        @ ui->chkbox_client->setChecked(1);@
                        its checked, but its changable by hand - it mustn't and should be stable

                        when

                        @ ui->chkbox_client->setChecked(1);
                        ui->chkbox_client->setCheckable(0);@
                        its not checkable by hand, but the check is removed

                        any ideas?

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          veeraps
                          wrote on last edited by
                          #12

                          The additional effort is whenever you want to change the state (setCheckable()), you need to first change the check box as checkable and then set the new state and set the checkable flag to false.

                          1 Reply Last reply
                          0
                          • sierdzioS Offline
                            sierdzioS Offline
                            sierdzio
                            Moderators
                            wrote on last edited by
                            #13

                            Or just use checkbox::setEnabled(false). It will gray the widget out, though. This probably can be overridden.

                            (Z(:^

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              roboterprogrammer
                              wrote on last edited by
                              #14

                              hi,
                              now i have a strange problem

                              in main.cpp
                              @
                              w.chkbox_client;@

                              works great and set the box checked.

                              but also in main.cpp,
                              @
                              QObject::connect(st.socket,SIGNAL(connected()), &w, SLOT(chkbox_client()));@
                              doesn't work, even i see there is a signal connected emittet.

                              i can see it because in the tcpconnect class, i put this signals out with qdebug, so i see exactly its connected.

                              and in my main.cpp
                              i have this:@
                              QObject::connect(st.socket,SIGNAL(readyRead()), &w, SLOT(loadTextFile() ) );@
                              and this works great.

                              thankful for any hints

                              PS:
                              btw st is an object made of the class qtcpsocket, and there its made with socket = new QTcpSocket(this);
                              and there i have piped the connection signal out@
                              connect(socket,SIGNAL(connected()), this, SLOT(connected()));@

                              where connected is a method who tells the connection via qdebug to me

                              general second question:
                              is the main.cpp the right place for those - inter class connections?

                              1 Reply Last reply
                              0
                              • sierdzioS Offline
                                sierdzioS Offline
                                sierdzio
                                Moderators
                                wrote on last edited by
                                #15

                                Does st.socked return a pointer?

                                (Z(:^

                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  roboterprogrammer
                                  wrote on last edited by
                                  #16

                                  hi,
                                  i think so,
                                  because no errors when compiling, and when i do with
                                  &st.socket
                                  there is an error
                                  @C:\Dokumente und Einstellungen\loetrobo\Robbie20-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug..\Robbie20\main.cpp:19: error: no matching function for call to 'QObject::connect(QTcpSocket**, const char*, Robbie20*, const char*)'@
                                  looking like a double pointer so without &
                                  it should be normal pointer

                                  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