Important: Please read the Qt Code of Conduct -

Making a checkbox checked

  • 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? like


    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.

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

  • y
    thanks thats the good way

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

    @void Robbie20::chkbox_client()
    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;;


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

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

  • hi,
    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.

  • 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;



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

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

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

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

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

  • 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


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

    any ideas?

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

  • Moderators

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

  • hi,
    now i have a strange problem

    in main.cpp

    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

    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?

  • Moderators

    Does st.socked return a pointer?

  • hi,
    i think so,
    because no errors when compiling, and when i do with
    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

Log in to reply