PDA

View Full Version : How to get the selected row when click the button in it?



weixj2003ld
7th November 2011, 00:15
I create a button and put it in a cell of a table(QTableWidget),but I find that the currentRow of the table does not change when I click the button,how to do ?

cincirin
8th November 2011, 10:45
QTableView::selectRow (http://doc.qt.nokia.com/stable/qtableview.html#selectRow)

weixj2003ld
8th November 2011, 14:29
No.
I explain my question.
1.I create a n*3 table(created with QTableWidget name table1);
2. I put buttons in the third column
3. When I click one of the buttons,I want to get the row of the button lies,

I use the follow signals,but it can not work well.
1.connect(button,signal(clicked()),this, signal(btnclicked()));
I find that table1->currRow() always does not change whenere I click what button.
When I click the cell in the first or second column,it is ok.
2. connect(table1,signal(cellclicked(int ,int)),signal(cellclickedslot(int ,int)));
it works like 1.

cincirin
8th November 2011, 14:43
You cannot connect signal to signal.
Your code should be something like this:


1. connect(button, SIGNAL(clicked()),this, SLOT(btnclicked()));
2. connect(table1, SIGNAL(cellclicked(int ,int)), SLOT(cellclickedslot(int ,int)));

See Signals & Slots documentation (http://doc.qt.nokia.com/stable/signalsandslots.html)

weixj2003ld
9th November 2011, 03:41
Ok,that my error.
But,I have change the code as you said,and it still not work.

I think that,how to catch the signal cellclicked when I cklick the button,but I don't know how to do.

Santosh Reddy
9th November 2011, 04:21
Signals can be connected to Signals, the problem in not related to this.

The problem is the mouse click event is taken by the button, and QTableWidget never receives it and hence cannot give current row information.

You need to associate each button with the row number, when you create the button. One way to do so is sub-class QPushButton and add a member variable (say buttonRow), and store the row number in it when you create the button. Now in the slot connected to clicked signal, check the member var.

weixj2003ld
10th November 2011, 07:58
You need to associate each button with the row number, when you create the button. One way to do so is sub-class QPushButton and add a member variable (say buttonRow), and store the row number in it when you create the button. Now in the slot connected to clicked signal, check the member var.

Could you explain"check the member var"?
I sub-class QPushButton as follows:

myPushButton :public QPushButton
{

public :
myPushButton (QString txt,int mrow);
private:
int row;
}
and use it as follows:
...

for(int i=0;i<count();i++)
{
....
myPushButton *myBtn=new myPushButton("convert",i);
connect(myBtn,SIGNAL(clicked()),this,convertSlot() );

}
...

void TableMyclass::convertSlot()
{
how to check the row?
}...

FuturePrimitive
10th November 2011, 10:00
Take a look here:
http://developer.qt.nokia.com/forums/viewthread/5913

Connect clicked() signal to custom slot of your sub-classed QPushButton that emits new signal with the row variable and connect this new signal to convertSlot that can accept int variable.
Not sure if its the best solution but it should work.