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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Omkar
  • 343
  • 3
  • 12
  • You might store some sort of mapping between row a combo box pointers and the corresponding row number, like: `std::map`. Once a combo box item is activated, you can know which row it was for. – vahancho May 14 '20 at 12:32
  • @vahancho No. Unfortunately that i cant use. Because sometimes user can delete a row so that logic will fail here. First suggest how to insert label in QComboBox – Omkar May 14 '20 at 13:05
  • Is `QDataWidgetMapper` not what you are looking for? – MasterAler May 14 '20 at 23:08
  • Hello @MasterAler I am newbie to QT. I dont know QDataWidgetMapper. I just want to do my tast\k by hook or by cook. 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 . Tell me how to do that. – Omkar May 15 '20 at 07:11

1 Answers1

0

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 Column 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();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Omkar
  • 343
  • 3
  • 12