Results 1 to 4 of 4

Thread: How to add QPushButton to QTableWidget cell?

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to add QPushButton to QTableWidget cell?

    Qt Code:
    1. QPushButton *btn1 = new QPushButton();
    2. btn1->setParent(ui->tableStatus);
    3. btn1->setIconSize((QSize(97,25)));
    4. QPixmap* pixmap1 = new QPixmap("status.png");
    5. QIcon icon1(*pixmap1);
    6. btn1->setIcon(icon1);
    7. btn1->setVisible(true);
    8. ui->tableStatus->setCellWidget(0,3,btn1);
    To copy to clipboard, switch view to plain text mode 

    I have used the above code and it is not placing the button on the particular cell, It just attaches in the top left corner of the table widget.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to add QPushButton to QTableWidget cell?

    This code works just fine here but the table needs at least one row and four columns.

    Line 4 is a memory leak.
    Line 7 is unnecessary and results in the problem you describe if the table does not contain at least one row, four columns. The widget cannot be put in the cell but you have parented it and made it visible so it shows as a child of the table (i.e. on top of the table widget).

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to add QPushButton to QTableWidget cell?

    I have already defined the row count as 16 and column count as 3 in constructor.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to add QPushButton to QTableWidget cell?

    Then you are not telling us what you are really doing:
    Qt Code:
    1. #include <QApplication>
    2. #include <QTableWidget>
    3. #include <QPushButton>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QTableWidget *tableStatus = new QTableWidget(4, 4);
    10.  
    11. // Your code
    12. QPushButton *btn1 = new QPushButton();
    13. btn1->setParent(tableStatus);
    14. btn1->setIconSize((QSize(97,25)));
    15. QPixmap* pixmap1 = new QPixmap("status.png");
    16. QIcon icon1(*pixmap1);
    17. btn1->setIcon(icon1);
    18. btn1->setVisible(true);
    19. tableStatus->setCellWidget(0,3,btn1);
    20. // end of your code
    21.  
    22. tableStatus->show();
    23.  
    24. int ret = app.exec();
    25. delete pixmap1;
    26. delete tableStatus;
    27. return ret;
    28. }
    To copy to clipboard, switch view to plain text mode 
    screen.png
    Or, if you change line 19 to:
    Qt Code:
    1. tableStatus->setCellWidget(0,5,btn1); // non-existent cell
    To copy to clipboard, switch view to plain text mode 
    screen.png
    Last edited by ChrisW67; 15th April 2014 at 08:35.

Similar Threads

  1. Replies: 3
    Last Post: 9th November 2012, 18:55
  2. qtablewidget cell's x() , y()
    By BalaQT in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2010, 12:23
  3. Can QPushButton be put into a QTableWidget cell?
    By ShaChris23 in forum Newbie
    Replies: 4
    Last Post: 1st September 2010, 00:18
  4. Add QPushButton to cell in QTableView?
    By nikhil in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2010, 09:01
  5. Replies: 1
    Last Post: 7th December 2009, 18:56

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.