Results 1 to 8 of 8

Thread: How to get the selected row when click the button in it?

  1. #1
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default How to get the selected row when click the button in it?

    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 ?
    Last edited by weixj2003ld; 7th November 2011 at 02:55. Reason: sorry

  2. #2
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get the selected row when click the button in it?


  3. #3
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get the selected row when click the button in it?

    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.

  4. #4
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get the selected row when click the button in it?

    You cannot connect signal to signal.
    Your code should be something like this:
    Qt Code:
    1. 1. connect(button, SIGNAL(clicked()),this, SLOT(btnclicked()));
    2. 2. connect(table1, SIGNAL(cellclicked(int ,int)), SLOT(cellclickedslot(int ,int)));
    To copy to clipboard, switch view to plain text mode 
    See Signals & Slots documentation

  5. #5
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get the selected row when click the button in it?

    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.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to get the selected row when click the button in it?

    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.

  7. #7
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get the selected row when click the button in it?

    Quote Originally Posted by Santosh Reddy View Post
    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:
    Qt Code:
    1. myPushButton :public QPushButton
    2. {
    3.  
    4. public :
    5. myPushButton (QString txt,int mrow);
    6. private:
    7. int row;
    8. }
    To copy to clipboard, switch view to plain text mode 
    and use it as follows:
    ...
    Qt Code:
    1. for(int i=0;i<count();i++)
    2. {
    3. ....
    4. myPushButton *myBtn=new myPushButton("convert",i);
    5. connect(myBtn,SIGNAL(clicked()),this,convertSlot());
    6.  
    7. }
    8. ...
    9.  
    10. void TableMyclass::convertSlot()
    11. {
    12. how to check the row?
    13. }
    To copy to clipboard, switch view to plain text mode 
    ...
    Last edited by weixj2003ld; 10th November 2011 at 07:59. Reason: sorry

  8. #8
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to get the selected row when click the button in it?

    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.

Similar Threads

  1. Disable button click
    By "BumbleBee" in forum Newbie
    Replies: 19
    Last Post: 30th March 2011, 13:16
  2. How to prohibit click to button
    By somename in forum Qt Programming
    Replies: 8
    Last Post: 27th May 2010, 22:32
  3. How can I know which button click.
    By electronicboy in forum Qt Programming
    Replies: 10
    Last Post: 4th October 2009, 14:27
  4. button click in webview
    By mind_freak in forum Qt Programming
    Replies: 1
    Last Post: 29th September 2009, 13:48
  5. QPaintEvent on button click?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2007, 08:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.