Results 1 to 6 of 6

Thread: QtableWidget With Pushutton get row error

  1. #1
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Question QtableWidget With Pushutton get row error

    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.

    Qt Code:
    1. but1=new QPushButton(QIcon(":/images/icons/search.png"),"",parentWidget());
    2. connect(but1, SIGNAL(clicked()),this,SLOT(productSearch()));
    3. ui->tab_items->setCellWidget(0,2,but1);
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. connect(ui->tab_items, SIGNAL(cellClicked(int,int)), this, SLOT(myCellActivated(int,int)));
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. connect(ui->tab_items, SIGNAL(itemActivated(QTableWidgetItem*)), this, {
    2. ...
    3. SLOT(myitemClicked(QTableWidgetItem*)));
    4. ...}
    5.  
    6. void frm_tabdemo::myitemClicked(QTableWidgetItem* myitem)
    7. {
    8. qDebug() << "*DEBUG : Global myrow set to : " << myitem->row();
    9. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtableWidget With Pushutton get row error

    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:
    Qt Code:
    1. for(row=0; row<rowCount(); row++){
    2. for(col=0; col<columnCount(); col++){
    3. if(sender() == cellWidget(row,col)){
    4. //this is your row and column for the clicked button
    5. }
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    ShapeShiftme (17th January 2011)

  4. #3
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default SOLVED QtableWidget With Pushutton get row error

    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

    Qt Code:
    1. void frm_quote::productSearch()
    2. {
    3.  
    4. int row;
    5. int col;
    6.  
    7.  
    8. for(row=0; row<ui->tab_items->rowCount(); row++){
    9. for(col=0; col<ui->tab_items->columnCount(); col++){
    10. //if(sender() == but1(row,col)){
    11.  
    12. if(sender() == ui->tab_items->cellWidget(row,col)){
    13. qDebug() << row << col << sender();
    14. }
    15. }
    16. }
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

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

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: SOLVED QtableWidget With Pushutton get row error

    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.

  6. #5
    Join Date
    Aug 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtableWidget With Pushutton get row error

    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

  7. #6
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Post Re: QtableWidget With Pushutton get row error

    This can be done using QSignalMapper, found here

  8. The following user says thank you to Ashkan_s for this useful post:

    liangsai (21st February 2013)

Similar Threads

  1. Replies: 1
    Last Post: 6th January 2011, 05:19
  2. fatal error C1001: An internal error has occurred in the compiler
    By noodles in forum Installation and Deployment
    Replies: 0
    Last Post: 12th August 2010, 12:24
  3. Is an error in doc whith QTableWidget::setModel?
    By mikolaj in forum Qt Programming
    Replies: 11
    Last Post: 8th January 2009, 21:31
  4. Replies: 1
    Last Post: 25th October 2008, 20:18
  5. QTableWidget click in empty space results in error?
    By Giel Peters in forum Qt Programming
    Replies: 4
    Last Post: 21st January 2006, 01:07

Tags for this Thread

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.