Results 1 to 4 of 4

Thread: QTableView with buttons

  1. #1
    Join Date
    Jan 2011
    Posts
    32
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    11
    Thanked 2 Times in 1 Post

    Default QTableView with buttons

    Is it possible to put pushbuttons in one column of QTableview which represen QSqlRelationalTableModel?
    For now I only succeed to put pushbutton which is only available in edit mode after dblclk the cell. I am doing that using my delegate(by sub-classing QStyledItemDelegate). My goal is to have pushbuton available always not only in edit mode and I need help for this.

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

    Default Re: QTableView with buttons

    will this work
    Qt Code:
    1. //main.cpp
    2. #include <QtGui>
    3. //QPushButton in QTreeView
    4. class MyWidget : public QTreeView
    5. {
    6. public:
    7. MyWidget(QWidget* parent = 0) : QTreeView(parent)
    8. {
    9. setModel(&model);
    10. model.setRowCount(10);
    11. model.setColumnCount(2);
    12. for(int i = 0; i < model.rowCount(); i++)
    13. {
    14. model.setData(model.index(i, 0), QString("PushButton %1").arg(i));
    15. setIndexWidget(model.index(i, 1), new QPushButton(QString("PushButton %1").arg(i)));
    16. }
    17. }
    18. private:
    19. };
    20.  
    21.  
    22. int main(int argc, char *argv[])
    23. {
    24. QApplication a(argc, argv);
    25. MyWidget w;
    26. w.show();
    27. return a.exec();
    28. }
    29. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2011
    Posts
    32
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    11
    Thanked 2 Times in 1 Post

    Default Re: QTableView with buttons

    My QTableView is not static. It is dynamically populated from database table. That is why I need to use sub-classed delegate. Any other idea?

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

    Default Re: QTableView with buttons

    QPushBotton is a widget which you want to be displayed and clickable in QTreeView cell. Delegate can be used to create a dynamic editor, and some static painting on the cell area, a picture or icon or some advanced text, but it cannot be used for a widget. The problem having a widget there is that that widget should be able to receive mouse events (in case of QPushButton widget).

    The only way the widget in the cell can receive events is when it is part(child widget) of the view widget, a widget cannot be part of delegate. Now to have a widgets dynamically added cells int the view, you need to subclass QTreeView and connect to the row insert, delete & move signals from model and create, delete or move the widget accordingly in the view. (this is some task)

  5. The following user says thank you to Santosh Reddy for this useful post:

    mak_user (2nd November 2011)

Similar Threads

  1. QTableView and QLineEdit OUTSIDE of QTableView, like Excel
    By JoZCaVaLLo in forum Qt Programming
    Replies: 10
    Last Post: 13th May 2011, 13:20
  2. Replies: 2
    Last Post: 26th November 2009, 04:45
  3. why does QTableView expand to cover the buttons
    By landonmkelsey in forum Qt Programming
    Replies: 5
    Last Post: 7th September 2008, 15:53
  4. Replies: 1
    Last Post: 16th May 2008, 20:31
  5. Several buttons
    By Mariane in forum Newbie
    Replies: 4
    Last Post: 15th March 2006, 20:50

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
  •  
Qt is a trademark of The Qt Company.