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. how to handle too many similar checkbox(30+ slot functions)?
Qt 6.11 is out! See what's new in the release blog

how to handle too many similar checkbox(30+ slot functions)?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.5k 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.
  • H Offline
    H Offline
    hitbuyi
    wrote on last edited by hitbuyi
    #1

    this code is generated by qt designer,
    QCheckBox *sC1;
    QCheckBox *sC2;
    QCheckBox *sC3;
    QCheckBox *sC4;
    QCheckBox *sC5;
    QCheckBox *sC6;
    QCheckBox *sC7;
    QCheckBox *sC8;
    QCheckBox *sC9;
    QCheckBox *sC10;

    ...

    QCheckBox *sC30;

    I need to write slot function to response the check event, since so many similiar checkboxes, it is tedious to write 30 slot functions, so I wanna

    1. put this checkbox pointer in a pointer array mySC[30], how to set the qt designer? is it possible?

    2. I just want to write one slot function to responce those checkbox envents, in one slot function, i check which box is checked

    I know in MFC framework, it provides a solution to deal with this kind things, in QT, is there any method to cope with this situation?

    Thanks a lot

    jsulmJ JonBJ 2 Replies Last reply
    0
    • H hitbuyi

      this code is generated by qt designer,
      QCheckBox *sC1;
      QCheckBox *sC2;
      QCheckBox *sC3;
      QCheckBox *sC4;
      QCheckBox *sC5;
      QCheckBox *sC6;
      QCheckBox *sC7;
      QCheckBox *sC8;
      QCheckBox *sC9;
      QCheckBox *sC10;

      ...

      QCheckBox *sC30;

      I need to write slot function to response the check event, since so many similiar checkboxes, it is tedious to write 30 slot functions, so I wanna

      1. put this checkbox pointer in a pointer array mySC[30], how to set the qt designer? is it possible?

      2. I just want to write one slot function to responce those checkbox envents, in one slot function, i check which box is checked

      I know in MFC framework, it provides a solution to deal with this kind things, in QT, is there any method to cope with this situation?

      Thanks a lot

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @hitbuyi said in how to handle too many similar checkbox(30+ slot functions)?:

      put this checkbox pointer in a pointer array mySC[30]

      Why?

      You can use one slot for all your check boxes.
      In the slot you get access to the check box which emitted the signal using sender() method.

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

      1 Reply Last reply
      4
      • H hitbuyi

        this code is generated by qt designer,
        QCheckBox *sC1;
        QCheckBox *sC2;
        QCheckBox *sC3;
        QCheckBox *sC4;
        QCheckBox *sC5;
        QCheckBox *sC6;
        QCheckBox *sC7;
        QCheckBox *sC8;
        QCheckBox *sC9;
        QCheckBox *sC10;

        ...

        QCheckBox *sC30;

        I need to write slot function to response the check event, since so many similiar checkboxes, it is tedious to write 30 slot functions, so I wanna

        1. put this checkbox pointer in a pointer array mySC[30], how to set the qt designer? is it possible?

        2. I just want to write one slot function to responce those checkbox envents, in one slot function, i check which box is checked

        I know in MFC framework, it provides a solution to deal with this kind things, in QT, is there any method to cope with this situation?

        Thanks a lot

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @hitbuyi
        In addition to @jsulm's sender() approach (which sometimes has issues, though I am not dissing it here), there are lambdas for passing a parameter or https://doc.qt.io/qt-5/qsignalmapper.html for doing just what you want here.

        H J.HilkJ 2 Replies Last reply
        1
        • JonBJ JonB

          @hitbuyi
          In addition to @jsulm's sender() approach (which sometimes has issues, though I am not dissing it here), there are lambdas for passing a parameter or https://doc.qt.io/qt-5/qsignalmapper.html for doing just what you want here.

          H Offline
          H Offline
          hitbuyi
          wrote on last edited by hitbuyi
          #4

          @JonB great, in future I'll try it. my current Qt is 5.9.9

          I have figured out a solution, not so perfect, but works in some degree:
          I labeled those QCheckBox as label_1,label_2, ..., label_30 in Qt designer, in my progam, I read label text, get int of 1,2,...,30 from label_xx, thus I know which checkbox is selected .

          void Simulator::on_showRadar() {
          QString tag;
          QString str;
          int idx;
          QCheckBox chk = qobject_cast<QCheckBox>(sender());
          tag = chk->text();
          str = tag.mid(5,6);
          idx = str.toInt();
          ...
          }

          the is method is faster than searching from beginning to end one by one

          jsulmJ 1 Reply Last reply
          0
          • H hitbuyi

            @JonB great, in future I'll try it. my current Qt is 5.9.9

            I have figured out a solution, not so perfect, but works in some degree:
            I labeled those QCheckBox as label_1,label_2, ..., label_30 in Qt designer, in my progam, I read label text, get int of 1,2,...,30 from label_xx, thus I know which checkbox is selected .

            void Simulator::on_showRadar() {
            QString tag;
            QString str;
            int idx;
            QCheckBox chk = qobject_cast<QCheckBox>(sender());
            tag = chk->text();
            str = tag.mid(5,6);
            idx = str.toInt();
            ...
            }

            the is method is faster than searching from beginning to end one by one

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @hitbuyi said in how to handle too many similar checkbox(30+ slot functions)?:

            QCheckBox chk = qobject_cast<QCheckBox>(sender());

            Should be

            QCheckBox chk = qobject_cast<QCheckBox*>(sender());
            

            Also you should check whether chk is nullptr or not before using it.

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

            1 Reply Last reply
            2
            • JonBJ JonB

              @hitbuyi
              In addition to @jsulm's sender() approach (which sometimes has issues, though I am not dissing it here), there are lambdas for passing a parameter or https://doc.qt.io/qt-5/qsignalmapper.html for doing just what you want here.

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

              @JonB I'm dissing it, sender() same as processEvents() should be part of the private api only and not accessible by normal framework users

              55d42ee4-3301-4ca9-be4d-f6c7b94915f8-image.png

              bbfcb29d-e105-4028-a592-9f4ee0a96f90-image.png

              Btw, before lambdas, there was a thing called https://doc.qt.io/qt-5/qbuttongroup.html
              in cases like this one


              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.

              JonBJ 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                @JonB I'm dissing it, sender() same as processEvents() should be part of the private api only and not accessible by normal framework users

                55d42ee4-3301-4ca9-be4d-f6c7b94915f8-image.png

                bbfcb29d-e105-4028-a592-9f4ee0a96f90-image.png

                Btw, before lambdas, there was a thing called https://doc.qt.io/qt-5/qbuttongroup.html
                in cases like this one

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @J-Hilk
                I don't diss @jsulm because he knows more than I :)

                My reservation about sender() was that as I understand it (not tested) it doesn't work if the connection is a lambda, as well as your case.

                QButtonGroup is possible here, but it's a bit, well, button-y! QSignalMapper is much broader.

                @hitbuyi
                If you're going to use your way of a "tag", instead of parsing it out of the button text consider using setObjectName() which is applicable to all QObjects. That can be set in Designer too. It has a number of advantages over the text of a button as an identifier.

                1 Reply Last reply
                1

                • Login

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