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. Accessing specific controls from widgets
Forum Updated to NodeBB v4.3 + New Features

Accessing specific controls from widgets

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 1.0k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1
    // [Add Checkbox] 
    		{
    			QWidget* pChkWidget = new QWidget();
    			
    			usrCheckBox* pChkItem = new usrCheckBox(pChkWidget);
    			pChkItem->SetCheckID(nRowIndex);
    			pChkItem->setFixedSize(size.height() * 0.055, size.height() * 0.055);
    
    			this->connect(pChkItem, &usrCheckBox::clicked, this, &wMethodInfo::BtnChecked);
    			
    			QHBoxLayout* layout = new QHBoxLayout(pChkWidget);
    			layout->addWidget(pChkItem);
    			layout->setAlignment(Qt::AlignCenter);
    			layout->setContentsMargins(QMargins(0, 0, 0, 0));
    			
    			pChkWidget->setLayout(layout);
    			this->ui.fileTableWidget->setCellWidget(nRowIndex, 0, pChkWidget);
    		}
    

    I have such code.

    The widget is put into the cell by calling the setCellWidget function.
    We got the widget by calling the CellWidget(row, col) function.
    I want to get the state of a checkbox inside a widget.
    Is there a way to access the control by accessing it as a widget?

    jsulmJ 1 Reply Last reply
    0
    • I IknowQT

      @jsulm

      QWidget* pChkWidget = new QWidget();
      

      If I do setCellWidget after declaring it as usrCheckBox without declaring it as qwidget, it seems to come out as I want.

      But I have to use Layout. please see my code

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #11

      @IknowQT
      Please be logical about this, and examine your own code. So far as I can see, glancing, I think you have a hierarchy like:

      setCellWidget(nRowIndex, 0, pChkWidget);
          QWidget* pChkWidget = new QWidget()
              QHBoxLayout* layout = new QHBoxLayout(pChkWidget);
                  usrCheckBox* pChkItem = new usrCheckBox(pChkWidget);
      

      So clearly picking up the cellWidget() is not going to return your usrCheckBox, it is going to return your pChkWidget, right?

      So from there to get to the usrCheckBox you must start from that widget and then use one of:

      • Find its child QHBoxLayout, find the child QWidget which is on that, and cast that to usrCheckBox*; or
      • Call QObject::findChild<usrCheckBox*>() on it to let it find the (first) descendant which is of type usrCheckBox*.
      1 Reply Last reply
      2
      • I IknowQT
        // [Add Checkbox] 
        		{
        			QWidget* pChkWidget = new QWidget();
        			
        			usrCheckBox* pChkItem = new usrCheckBox(pChkWidget);
        			pChkItem->SetCheckID(nRowIndex);
        			pChkItem->setFixedSize(size.height() * 0.055, size.height() * 0.055);
        
        			this->connect(pChkItem, &usrCheckBox::clicked, this, &wMethodInfo::BtnChecked);
        			
        			QHBoxLayout* layout = new QHBoxLayout(pChkWidget);
        			layout->addWidget(pChkItem);
        			layout->setAlignment(Qt::AlignCenter);
        			layout->setContentsMargins(QMargins(0, 0, 0, 0));
        			
        			pChkWidget->setLayout(layout);
        			this->ui.fileTableWidget->setCellWidget(nRowIndex, 0, pChkWidget);
        		}
        

        I have such code.

        The widget is put into the cell by calling the setCellWidget function.
        We got the widget by calling the CellWidget(row, col) function.
        I want to get the state of a checkbox inside a widget.
        Is there a way to access the control by accessing it as a widget?

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

        @IknowQT said in Accessing specific controls from widgets:

        Is there a way to access the control by accessing it as a widget?

        Yes. What exactly is the problem?
        I think you need to cast from QWidget* to what ever the widget actually is (https://doc.qt.io/qt-6/qobject.html#qobject_cast-1).

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

        1 Reply Last reply
        1
        • I Offline
          I Offline
          IknowQT
          wrote on last edited by
          #3

          @IknowQT said in Accessing specific controls from widgets:

          젯으로 액세스하여 컨트롤에 액세스하는 방법이 있습니까?

          Inside the widget of pChkWidget is the pChkItem control.
          The return value of this->ui.fileTableWidget->cellWidget(row, col) is Widget.
          You want to access the PchkItem inside the pChkWidget.

          jsulmJ I 2 Replies Last reply
          0
          • I IknowQT

            @IknowQT said in Accessing specific controls from widgets:

            젯으로 액세스하여 컨트롤에 액세스하는 방법이 있습니까?

            Inside the widget of pChkWidget is the pChkItem control.
            The return value of this->ui.fileTableWidget->cellWidget(row, col) is Widget.
            You want to access the PchkItem inside the pChkWidget.

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @IknowQT said in Accessing specific controls from widgets:

            You want to access the PchkItem inside the pChkWidget.

            I already wrote you that you need to cast QWidget* to the type you need.
            Did you actually read my response?

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

            I 1 Reply Last reply
            1
            • jsulmJ jsulm

              @IknowQT said in Accessing specific controls from widgets:

              You want to access the PchkItem inside the pChkWidget.

              I already wrote you that you need to cast QWidget* to the type you need.
              Did you actually read my response?

              I Offline
              I Offline
              IknowQT
              wrote on last edited by
              #5

              @jsulm

              for (int i = 0; i < this->ui.fileTableWidget->rowCount(); i++)
              {
              usrCheckBox* pChkItem = static_cast<usrCheckBox*>(this->ui.fileTableWidget->cellWidget(i, 0));
              qDebug() << "GetSelectedItems()"<< pChkItem->GetCheckID();
              }

              I tested it like this.
              I didn't get the results I wanted.

              Casting the widget doesn't seem to work.

              jsulmJ 1 Reply Last reply
              0
              • I IknowQT

                @jsulm

                for (int i = 0; i < this->ui.fileTableWidget->rowCount(); i++)
                {
                usrCheckBox* pChkItem = static_cast<usrCheckBox*>(this->ui.fileTableWidget->cellWidget(i, 0));
                qDebug() << "GetSelectedItems()"<< pChkItem->GetCheckID();
                }

                I tested it like this.
                I didn't get the results I wanted.

                Casting the widget doesn't seem to work.

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @IknowQT You should really read more carefully! I suggested to use https://doc.qt.io/qt-6/qobject.html#qobject_cast-1 for casting, not static_cast.

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

                I 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @IknowQT You should really read more carefully! I suggested to use https://doc.qt.io/qt-6/qobject.html#qobject_cast-1 for casting, not static_cast.

                  I Offline
                  I Offline
                  IknowQT
                  wrote on last edited by
                  #7

                  @jsulm

                  usrCheckBox* pChkItem = qobject_cast<usrCheckBox*>(this->ui.fileTableWidget->cellWidget(i, 0));
                  

                  I tested it, but it comes out as Null after casting.
                  I don't think there is any way to access this

                  jsulmJ 1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    ChrisW67
                    wrote on last edited by
                    #8

                    Yes, that is because the cell widget is not of type usrCheckBox.

                    1 Reply Last reply
                    1
                    • I IknowQT

                      @jsulm

                      usrCheckBox* pChkItem = qobject_cast<usrCheckBox*>(this->ui.fileTableWidget->cellWidget(i, 0));
                      

                      I tested it, but it comes out as Null after casting.
                      I don't think there is any way to access this

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      @IknowQT said in Accessing specific controls from widgets:

                      I don't think there is any way to access this

                      Of course there is, but as @ChrisW67 wrote: what this->ui.fileTableWidget->cellWidget(i, 0) returns is apparently not an usrCheckBox...

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

                      I 1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @IknowQT said in Accessing specific controls from widgets:

                        I don't think there is any way to access this

                        Of course there is, but as @ChrisW67 wrote: what this->ui.fileTableWidget->cellWidget(i, 0) returns is apparently not an usrCheckBox...

                        I Offline
                        I Offline
                        IknowQT
                        wrote on last edited by
                        #10

                        @jsulm

                        QWidget* pChkWidget = new QWidget();
                        

                        If I do setCellWidget after declaring it as usrCheckBox without declaring it as qwidget, it seems to come out as I want.

                        But I have to use Layout. please see my code

                        JonBJ jsulmJ 2 Replies Last reply
                        0
                        • I IknowQT

                          @jsulm

                          QWidget* pChkWidget = new QWidget();
                          

                          If I do setCellWidget after declaring it as usrCheckBox without declaring it as qwidget, it seems to come out as I want.

                          But I have to use Layout. please see my code

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by
                          #11

                          @IknowQT
                          Please be logical about this, and examine your own code. So far as I can see, glancing, I think you have a hierarchy like:

                          setCellWidget(nRowIndex, 0, pChkWidget);
                              QWidget* pChkWidget = new QWidget()
                                  QHBoxLayout* layout = new QHBoxLayout(pChkWidget);
                                      usrCheckBox* pChkItem = new usrCheckBox(pChkWidget);
                          

                          So clearly picking up the cellWidget() is not going to return your usrCheckBox, it is going to return your pChkWidget, right?

                          So from there to get to the usrCheckBox you must start from that widget and then use one of:

                          • Find its child QHBoxLayout, find the child QWidget which is on that, and cast that to usrCheckBox*; or
                          • Call QObject::findChild<usrCheckBox*>() on it to let it find the (first) descendant which is of type usrCheckBox*.
                          1 Reply Last reply
                          2
                          • I IknowQT

                            @jsulm

                            QWidget* pChkWidget = new QWidget();
                            

                            If I do setCellWidget after declaring it as usrCheckBox without declaring it as qwidget, it seems to come out as I want.

                            But I have to use Layout. please see my code

                            jsulmJ Online
                            jsulmJ Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @IknowQT So, in fact you're setting pChkWidget (which is a QWidget) as cell widget, right? Then why do you expect this->ui.fileTableWidget->cellWidget(i, 0) to return usrCheckBox?!
                            As @JonB wrote usrCheckBox is a child of pChkWidget. So get pChkWidget first (calling this->ui.fileTableWidget->cellWidget(i, 0)) and then its child usrCheckBox.

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

                            1 Reply Last reply
                            1
                            • C Offline
                              C Offline
                              ChrisW67
                              wrote on last edited by
                              #13

                              Another approach would be to define your own widget class that exposes the state of the check box it contains.

                              class FancyCellWidget: pubic QWidget {
                                Q_OBJECT
                              public:
                                explicit FancyCellWidget(QWidget *p = nullptr);
                                Qt::CheckState 	checkState() const; // just return this from the internal QCheckbox
                                void 	setCheckState(Qt::CheckState state); // pass through to the internal QCheckbox
                                ...
                              signals:
                                void clicked(); // connect the internal check box clicked() signal to this one in the constructor
                              };
                              

                              and then use that to create your cell widget.

                              1 Reply Last reply
                              0
                              • I IknowQT

                                @IknowQT said in Accessing specific controls from widgets:

                                젯으로 액세스하여 컨트롤에 액세스하는 방법이 있습니까?

                                Inside the widget of pChkWidget is the pChkItem control.
                                The return value of this->ui.fileTableWidget->cellWidget(row, col) is Widget.
                                You want to access the PchkItem inside the pChkWidget.

                                I Offline
                                I Offline
                                IknowQT
                                wrote on last edited by IknowQT
                                #14

                                @jsulm

                                @jsulm said in Accessing specific controls from widgets:

                                Yes. What exactly is the problem?
                                I think you need to cast from QWidget* to what ever the widget actually is (https://doc.qt.io/qt-6/qobject.html#qobject_cast-1).

                                @IknowQT said in Accessing specific controls from widgets:

                                Inside the widget of pChkWidget is the pChkItem control.
                                The return value of this->ui.fileTableWidget->cellWidget(row, col) is Widget.
                                You want to access the PchkItem inside the pChkWidget.

                                You recommended objectcast.
                                I said clearly. I have a Checkbox control inside a Widget, so if I cast an object, does it become a checkbox?

                                I showed the test results. Returning as null is a natural result.
                                I think you suggested just object cast without looking closely at the code you asked in the first place.

                                @JonB
                                I was able to access that control using findChild, which Jonby recommended.

                                jsulmJ 1 Reply Last reply
                                0
                                • I IknowQT

                                  @jsulm

                                  @jsulm said in Accessing specific controls from widgets:

                                  Yes. What exactly is the problem?
                                  I think you need to cast from QWidget* to what ever the widget actually is (https://doc.qt.io/qt-6/qobject.html#qobject_cast-1).

                                  @IknowQT said in Accessing specific controls from widgets:

                                  Inside the widget of pChkWidget is the pChkItem control.
                                  The return value of this->ui.fileTableWidget->cellWidget(row, col) is Widget.
                                  You want to access the PchkItem inside the pChkWidget.

                                  You recommended objectcast.
                                  I said clearly. I have a Checkbox control inside a Widget, so if I cast an object, does it become a checkbox?

                                  I showed the test results. Returning as null is a natural result.
                                  I think you suggested just object cast without looking closely at the code you asked in the first place.

                                  @JonB
                                  I was able to access that control using findChild, which Jonby recommended.

                                  jsulmJ Online
                                  jsulmJ Online
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #15

                                  @IknowQT said in Accessing specific controls from widgets:

                                  so if I cast an object, does it become a checkbox

                                  Casting does not magically change the type of an object. That's why qobject_cast will return nullptr if the object you're trying to cast is not of the type you want to cast to. Casting only works if the object is really an object of the target type.

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

                                  1 Reply Last reply
                                  2

                                  • Login

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