Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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



  • 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



  • 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();
    }
    


  • I can't disagree more with the above solution


Log in to reply