PDA

View Full Version : Problem with QTableWidget and Signals



rmagro
17th September 2008, 10:41
Hi all,

I have a QtableWidget defines as follows:



tableWidget = new QTableWidget(3, 4, this);

for (int row_j=0; row_j < 3; ++row_j)
{
for (int column_i=0; column_i < 4; column_i++)
{
QTableWidgetItem *item = new QTableWidgetItem;
item->setTextAlignment (Qt::AlignCenter);
tableWidget->setItem(row_j, column_i, item);
}
}

tableWidget->item(0, 0)->setCheckState ( Qt::Checked ); <------
tableWidget->item(1, 0)->setCheckState ( Qt::Checked ); <------
tableWidget->item(2, 0)->setCheckState ( Qt::Checked ); <------

QGroupBox *frameQTable = new QGroupBox(tr(""));

QVBoxLayout *TableLayout = new QVBoxLayout(frameQTable);
TableLayout->addWidget (tableWidget);

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(frameQTable);
setLayout(layout);


Using the code as the shows by the arrows above, I put into the first cell of each row a CheckBox..

The Question is :
is there a way to emit a signal when the I click on the CheckBox?

So far the only signal is working for me is itemSelectionChanged ():



connect(tableWidget, SIGNAL(itemSelectionChanged () ),
this, SIGNAL(completeChanged()));


that works indeed when I move to another item....
but not when I put the check..

Thank you for your kind reply..
Roberto

caduel
17th September 2008, 10:53
Try connecting to QTableWidget::cellChanged() and check the Qt::CheckStateRole of the item.

HTH

rmagro
17th September 2008, 11:07
Nothing happens whe I click on the checkbox...
Ho do I have to use the Qt::CheckStateRole

Here is the code I use:



what I am trying:

connect(tableWidget, SIGNAL(cellChanged(0,0) ),
this, SIGNAL(completeChanged()));

and then in the method called I have the code:

if ( (tableWidget->item(1, 0)->checkState () != Qt::Checked) )
{
return false;
}
else
return true;

but nothing..


Thx

caduel
17th September 2008, 11:58
You must not use values for the arguments of the slot in the connect call!
(You should get an error message printed for your code, by the way.)

Try like this


connect(tableWidget, SIGNAL(cellChanged(int,int) ),
this, SIGNAL(completeChanged(int,int)));




void completeChanged(int row, int col)
{
QTableWidgetItem *item = tableWidget->item(row,col);
if (item->checkState() == ...)
...
}

rmagro
17th September 2008, 14:28
BINGOOO!!!

It works Perfectly...as you suggested..

GREAT!

Many thanks..

Roberto