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 get QTableWidget + QComboBox together and how to access perticular row if value is changed from QComboBox of same row
Forum Updated to NodeBB v4.3 + New Features

How to get QTableWidget + QComboBox together and how to access perticular row if value is changed from QComboBox of same row

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.9k 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.
  • O Offline
    O Offline
    OmkarD
    wrote on last edited by
    #1

    I am stucked at the combination of QTableWidget + QComboBox.

    My expected output something like this ...

    As showing in the screenshot, what I want is that, whenever I want insert new item inside table after hitting ADD button, new row should be inserted into the table and at the end I want Drop Down box with text of Allow and Block.

    webFilterCat = new QTableWidget(0);
    webFilterCat -> verticalHeader()->setDefaultSectionSize(15);
    webFilterCat -> setMaximumWidth(310);
    webFilterCat -> setMinimumWidth(310);
    webFilterCat -> setMaximumHeight(170);
    webFilterCat -> setMinimumHeight(170);
    /* Set Row and Coloumn Count*/
    //webFilterCat->setRowCount(10);
    webFilterCat->setColumnCount(3);
    
    /* Table Header */
    tableHeader<<"Category"<<"Status"<<"Action";
    webFilterCat -> setHorizontalHeaderLabels(tableHeader);
    webFilterCat -> horizontalHeader()->setDefaultSectionSize(100);
    
    // ------ Insert Data -------
    webFilterCat->insertRow ( webFilterCat->rowCount() );
    webFilterCat->setItem(( webFilterCat->rowCount() -1 ), 0, new QTableWidgetItem(extName));
    webFilterCat -> setItem(( webFilterCat->rowCount() -1 ), 1, new QTableWidgetItem("Block"));
    webFilterCat -> setCellWidget ( ( webFilterCat->rowCount() -1 ), 2, new QComboBox( webFilterCat ) );
    

    I am getting QComboBox at every row after adding item into table but I cant set label (i.e. Allow or Block) to QComboBox and How to access that perticular row after value is changed in QComboBox.

    I am using qt 4.2 and compiling code using linux terminal.

    In short not using qt creator , using command line qt.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      setCellWidget should burn in hell. See https://wiki.qt.io/Combo_Boxes_in_Item_Views for the correct way to do this. You can then connect do the dataChanged signal of the model to detect changes in the combobox

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • O Offline
        O Offline
        OmkarD
        wrote on last edited by
        #3

        I found solution for my problem .

        webFilterCat = new QTableWidget(0);
        webFilterCat -> verticalHeader()->setDefaultSectionSize(15);
        webFilterCat -> setMaximumWidth(310);
        webFilterCat -> setMinimumWidth(310);
        webFilterCat -> setMaximumHeight(170);
        webFilterCat -> setMinimumHeight(170);
        /* Set Row and Coloumn Count*/
        //webFilterCat->setRowCount(10);
        webFilterCat->setColumnCount(3);
        
        /* Table Header */
        tableHeader<<"Category"<<"Status"<<"Action";
        webFilterCat -> setHorizontalHeaderLabels(tableHeader);
        webFilterCat -> horizontalHeader()->setDefaultSectionSize(100);
        
        for(int i=0; i<row_count; i++){
        	// ------ Insert Data -------
        	webFilterCat->insertRow ( webFilterCat->rowCount() );
        	webFilterCat->setItem(( webFilterCat->rowCount() -1 ), 0, new QTableWidgetItem(extName));
        	webFilterCat -> setItem(( webFilterCat->rowCount() -1 ), 1, new QTableWidgetItem("Block"));
        	/*webFilterCat -> setCellWidget ( ( webFilterCat->rowCount() -1 ), 2, new QComboBox( webFilterCat ) );*/
        
        	cmbCatAllowOrBlock = new QComboBox ( 0 );
        
        	cmbCatAllowOrBlock -> setFixedSize ( 80, 20 ); // 75, 20
        	cmbCatAllowOrBlock -> addItem ( "Allow" );
        	cmbCatAllowOrBlock -> addItem ( "Block" );
        	cmbCatAllowOrBlock -> setCurrentIndex( 0 );
        
        	webFilterCat -> setCellWidget ( ( webFilterCat->rowCount() -1 ), 2, cmbCatAllowOrBlock );
        }
        

        I got the solution what I need as shown below.

        enter image description here

        and got solution for this problem as well

        How to access that perticular row after value is changed in QComboBox ?

        What I did, at the time of read perticular QComboBox of perticular row. I will not try to read perticular QComboBox of perticular row. I only need the changed state of the QComboBox, so I will read whole table and compare with previous one and able to find the change.

        So, how to read QComboBox row by row is

        for(int i=0;i<(webFilterCat->rowCount() -1);i++){
               QComboBox* combo=(QComboBox*) webFilterCat -> cellWidget(i, 2);
               qDebug() <<  combo -> currentText();
        }
        
        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          I can't disagree more with the above solution

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          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