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. Retrieve a QWidget inside QTableWidgetItem
Forum Updated to NodeBB v4.3 + New Features

Retrieve a QWidget inside QTableWidgetItem

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 669 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.
  • L Offline
    L Offline
    LCorona
    wrote on last edited by
    #1

    My app has a QTableWidget in wich in some cell was added another widget like this:

                QWidget *cWidget = new QWidget();
                WidgetColorPicker *pColor= new WidgetColorPicker();
                QHBoxLayout *cLayout = new QHBoxLayout(cWidget);
                cLayout->addWidget(pColor);
                cLayout->setAlignment(Qt::AlignLeft);
                cLayout->setContentsMargins(0,0,0,0);
                cWidget->setLayout(cLayout);
                ui->channelsTable->setCellWidget(i,1,cWidget);
                connect(pColor, SIGNAL(colorSelectionChanged(QColor)), this, SLOT(plotColorChanged(QColor)));
    

    An event is triggered and one of the color picker, for get what widget was, it needs to get the color picker that was the sender in a for loop, but I dont know of retrieve this from the QTableWidgetItem

    WidgetColorPicker *widget = (WidgetColorPicker*)sender();
     
    for (int i = 0; i < ui->channelsTable->rowCount(); i++)
    {
        QTableWidgetItem *it= ui->channelsTable->item(i,1);
        if (it ->????? == widget)
       {
           return i;
       }
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You can either use QTableWidget::cellWidget() or use the new signal/slot syntax and pass the widget directly via a lambda.

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

      L 1 Reply Last reply
      4
      • Christian EhrlicherC Christian Ehrlicher

        You can either use QTableWidget::cellWidget() or use the new signal/slot syntax and pass the widget directly via a lambda.

        L Offline
        L Offline
        LCorona
        wrote on last edited by LCorona
        #3

        @Christian-Ehrlicher
        I test with cellWidget, and use findChild

         WidgetColorPicker *widget = (WidgetColorPicker*)sender();
          
         for (int i = 0; i < ui->channelsTable->rowCount(); i++)
         {
            QWidget *it= ui->channelsTable->cellWidget(i,1);
           
            if(widget == it->findChild<WidgetColorPicker*>("pColor"))
            {
                qDebug()<<"ROWW="<<i;
            }
        }
        

        but cant find the sender qwidget, never enter the if sentence. What is wrong??

        mrjjM 1 Reply Last reply
        0
        • L LCorona

          @Christian-Ehrlicher
          I test with cellWidget, and use findChild

           WidgetColorPicker *widget = (WidgetColorPicker*)sender();
            
           for (int i = 0; i < ui->channelsTable->rowCount(); i++)
           {
              QWidget *it= ui->channelsTable->cellWidget(i,1);
             
              if(widget == it->findChild<WidgetColorPicker*>("pColor"))
              {
                  qDebug()<<"ROWW="<<i;
              }
          }
          

          but cant find the sender qwidget, never enter the if sentence. What is wrong??

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          The variable name is pColor
          you dont seem to called
          pColor->setObjectName("pColor")

          so that is why it cant find it

          However, why not just do

          it->findChild<WidgetColorPicker*>()

          since there should only be one WidgetColorPicker in that widget, correct ?

          But is the logic not wrong ?

          The cellWidget returns the holding QWidget.

          and you compare that to the inner WidgetColorPicker ?

          i would think you want

          if( widget == it )

          like is the widget i get from sender() the same as i get from CellWidget
          so you can know the row index ?

          L 1 Reply Last reply
          2
          • mrjjM mrjj

            Hi
            The variable name is pColor
            you dont seem to called
            pColor->setObjectName("pColor")

            so that is why it cant find it

            However, why not just do

            it->findChild<WidgetColorPicker*>()

            since there should only be one WidgetColorPicker in that widget, correct ?

            But is the logic not wrong ?

            The cellWidget returns the holding QWidget.

            and you compare that to the inner WidgetColorPicker ?

            i would think you want

            if( widget == it )

            like is the widget i get from sender() the same as i get from CellWidget
            so you can know the row index ?

            L Offline
            L Offline
            LCorona
            wrote on last edited by
            #5

            @mrjj
            Hi.
            Ok, Im sorry, you are all right, the concept of variable name, and object name are new for me, I miss it completly.
            The command

            it->findChild<WidgetColorPicker*>()
            

            Works fine, the name object was the problem.

            In other hand, the comparation in if sentence is correct,

            if(widget == it->findChild<WidgetColorPicker*>())
            

            because "it" is not the WidgetColorPicker, is the father that is directly inserted in the cell.
            Then, the child of "it" is the WidgetColorPicker that send the signal. Remembering that in the code of TableWidget the color picker is put in a layout, the layout inside another widget, and this widget in the table cell.

            Retaking the main issue, yes, the command it->findChild<WidgetColorPicker*>() works fine, arealdy get the row number correctly. The color picker will be used for change the color of the plot, of the logic signal analizer. Yes, there is the same project that mentioned in other questions.

            Thank you very much, also thanks of @Christian-Ehrlicher for the first tip. Problem resolved.

            mrjjM 1 Reply Last reply
            0
            • L LCorona

              @mrjj
              Hi.
              Ok, Im sorry, you are all right, the concept of variable name, and object name are new for me, I miss it completly.
              The command

              it->findChild<WidgetColorPicker*>()
              

              Works fine, the name object was the problem.

              In other hand, the comparation in if sentence is correct,

              if(widget == it->findChild<WidgetColorPicker*>())
              

              because "it" is not the WidgetColorPicker, is the father that is directly inserted in the cell.
              Then, the child of "it" is the WidgetColorPicker that send the signal. Remembering that in the code of TableWidget the color picker is put in a layout, the layout inside another widget, and this widget in the table cell.

              Retaking the main issue, yes, the command it->findChild<WidgetColorPicker*>() works fine, arealdy get the row number correctly. The color picker will be used for change the color of the plot, of the logic signal analizer. Yes, there is the same project that mentioned in other questions.

              Thank you very much, also thanks of @Christian-Ehrlicher for the first tip. Problem resolved.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @LCorona

              Np, I also forget it sometimes as the Designer gives them names
              but when you insert from code they don't have:)

              Oh yes you are right.
              The sender is the ofc the WidgetColorPicker. My bad :)

              just as a note:
              If your list is stable, meaning you adds the rows and don't delete any or insert new
              we could just have stuff the index via
              a dynamic property

              pColor->setProperty("myindex", i);

              and in the slot
              int index = widget->property("myindex").toInt();

              https://doc.qt.io/qt-5/qobject.html#setProperty

              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