PDA

View Full Version : QtableWidget With Pushutton get row error



ShapeShiftme
17th January 2011, 05:43
Good day all.

I have created a Qtablewidget in qt designer called tab_items.
I then created A Pusg button and added it to the table.


but1=new QPushButton(QIcon(":/images/icons/search.png"),"",parentWidget());
connect(but1, SIGNAL(clicked()),this,SLOT(productSearch()));
ui->tab_items->setCellWidget(0,2,but1);

This gives me my search page. Which works great. But as i get more rows in the table i add more buttons. all works fine with more thatn one button.

However i would like to know what row the button is on.

At first i tried this


connect(ui->tab_items, SIGNAL(cellClicked(int,int)), this, SLOT(myCellActivated(int,int)));

The above works fine for all the cells exept the one with the widget inserted.
I also tried cellactivated which did not work. So i though it must be because it i s a widget

So i then tried this


connect(ui->tab_items, SIGNAL(itemActivated(QTableWidgetItem*)), this, {
...
SLOT(myitemClicked(QTableWidgetItem*)));
...}

void frm_tabdemo::myitemClicked(QTableWidgetItem* myitem)
{
qDebug() << "*DEBUG : Global myrow set to : " << myitem->row();
}


This time "myitemclicked" does not even display.

So with all that i have tried above could someone please tell me how i could get the row of the button that was clicked

Regards

high_flyer
17th January 2011, 14:39
The above works fine for all the cells exept the one with the widget inserted.
I don't know if there is an elegant way to solve this via the ModelView classes without overloading/subclassing - maybe some heavier users of ModelView can asnwer that.
One easy way (although no elegant) woudl be to have one slot connected to all the buttons, and in that slot do:


for(row=0; row<rowCount(); row++){
for(col=0; col<columnCount(); col++){
if(sender() == cellWidget(row,col)){
//this is your row and column for the clicked button
}
}
}

ShapeShiftme
17th January 2011, 16:18
Thanks i grasp the concept of your idea and it would work perfectly for my solution

It took me a while to figure out the correct way to reference the widget.
Thanks very much.
in case someone wants to see the final procedure here it is


void frm_quote::productSearch()
{

int row;
int col;


for(row=0; row<ui->tab_items->rowCount(); row++){
for(col=0; col<ui->tab_items->columnCount(); col++){
//if(sender() == but1(row,col)){

if(sender() == ui->tab_items->cellWidget(row,col)){
qDebug() << row << col << sender();
}
}
}

}

For my use i then set a global variable in this procedure and im sorted

d_stranz
17th January 2011, 19:19
You could also derive a custom button from QPushButton that contains a member variable "rowNumber" and a new signal "clicked( int rowNumber )". In this new class, connect the QPushButton::clicked() signal to a private slot that issues the new clicked( int ) signal, and insert the rowNumber value as argument, then emit the new signal.

When adding the new buttons to the table, set the rowNumber appropriately. Connect the clicked(int) signal for -all- of the buttons to an onClicked( int ) slot in your "frm_quote" class. When the button is clicked, it will tell you its row number without having to loop through all of the rows of the table.

You can generalize this to have the button hold both row and column number, and implement the signal as clicked( int row, int cell ). This way you could have a table with multiple buttons per row which could all be handled by a single slot in the form class.

CSeggelin
22nd August 2012, 20:03
This is kind of icky, but you could set a custom property on the button containing the row index it is in.

btnWidget->setProperty("TableRowIndex", rowIndex);

Then in the slot call sender()->property("TableRowIndex") to fetch the row index back out.

The drawback is anything that adds rows or removes rows from the table (or reorders them) will necessitate looping through the rows and resetting all the row indices.

What would be nice is if widgets that were attached to table cells simply had this property set and maintained automatically by the table widget itself--putting controls in rows of tables is hardly a unique activity, and it seems silly that everybody who uses one slot to accept signals from controls in multiple rows has to reinvent a solution. I guess if you need this solution in multiple places you could subclass QTableWidget itself and have it add and maintain this property.

-- C. Seggelin

Ashkan_s
17th February 2013, 15:31
This can be done using QSignalMapper, found here (http://stackoverflow.com/questions/9672249/how-to-detect-button-clicked-in-a-cell-from-qtablewidget)