PDA

View Full Version : QTableWidget, one column editable



stella1016
18th December 2009, 10:24
I want my tablewidget has these functionality:
1) one column can be editable, the rest not;
2) row select

I tried it out in this way:



tableWidget->horizontalHeader()->setClickable(false);
tableWidget->horizontalHeader()->setStretchLastSection(true);
tableWidget->setSelectionMode(QAbstractItemView::SingleSelectio n);
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows );


tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers) will forbiden all the edition. I couldn't find out the correct method for enable edition only for on column. The method of setFlags() to TableWidgetItem with Qt::ItemIsEditable will disable the edition. I am confused. Any one can help? Thanks.

darshan.hardas
18th December 2009, 11:24
Hi,

It can be done by catching itemClicked or double clicked event.
Compare the column of clicked item and set the flag as editable or un-editable.

Firstly for all items you can set edig triggers as NoEditTriggers

gmaa45
19th December 2009, 10:22
hi
use 'void cellClicked ( int row, int column )' signal
and Put yor code in this signal,

for example:
first connect signal to table
ok
void MyClass::cellClickedMyTable(int _row, int _col){
if( _col == 2 ){
myTable->item( _row, _col )->setFlags(Qt::ItemIsEditable); // or another code
}
}

stella1016
21st December 2009, 14:08
hi
use 'void cellClicked ( int row, int column )' signal
and Put yor code in this signal,

for example:
first connect signal to table
ok
void MyClass::cellClickedMyTable(int _row, int _col){
if( _col == 2 ){
myTable->item( _row, _col )->setFlags(Qt::ItemIsEditable); // or another code
}
}

Thanks for your reply.

I am using your way to test it out.
I think it is working. But the 'Flag' thing is not working as I expected.

After set one cell with


myTable->item( _row, _col )->setFlags(Qt::ItemIsEditable);

this cell is actually not editable any more. I tried also with flag Qt::ItemIsSelectable, after setting this, the cell is not selectable. Is this totally working like this, or other things related to this parameter have conflicts with it?

What I expected is, after set it with ItemIsSelectable, the cell can be selected, isn't it? The testing result is something opposite....:(

stella1016
21st December 2009, 14:24
After some search here, I found the answer:


void MyTableWidget::click_cell(int row, int column)
{
if( column != 1 )
tableWidget->item(row, column)->setFlags(tableWidget->item(row, column)->flags() & ~Qt::ItemIsEditable);
}


:D

pcheng
11th July 2012, 09:35
I used the above solution to make the cells uneditable except for a specific column. The problem now is that every click also triggers the following SIGNAL-SLOT

connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(recalculate(QTableWidgetItem*)));

This causes the recalculation to occur on every click on a non-editable field. Any idea on how to stop this from happening? Is there a way to state which column the itemChanged SIGNAL will look at?

Thanks,

Pericles

EDIT* I found the solution by disconnecting the SIGNAL-SLOT right before the change and connecting again after the change.