I have custom widget made with qt designer and i want to add it to QTableWidget cell. But it doesn't work.
Here is the code :
int nRows =10;
for(int row = 0; row < nRows;row++;)
{
QTableWidgetItem* item = new QTableWidgetItem();
CustomWdg* wdg=new CustomWdg( );
mTableWdg->insertRow( row );
mTableWdg->setItem(row, 0, item);
mTableWdg->setCellWidget( row, 0, wdg );
}
Your code is correct, so the only thing that comes to my mind is that you didn't
setColumnCount(1)
beforefor
loop. If that's not the case, you could try to set row and column count before thatfor
loop instead inserting row by row in loop:If you really need
item
("line one" and "line three") you should set it like this:QTableWidgetItem* item = new QTableWidgetItem("");
, otherwise you don't need those lines, yourCustomWdg
is properly set withsetCellWidget
If you want to add custom widget into table cell you can use QItemDelegate.
Create your own Delegate class and inherit it from QItemDelegate.
And then set delegate for Table with this methods on your own.
I have tried this code:
and result is: