Results 1 to 4 of 4

Thread: QWidget in tableview

  1. #1
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QWidget in tableview

    I want to know how to put a widget into a tableView along with the row from my database table?(each row has it's own widget) like a QPushbutton which suppose is the delete button for the row and when is clicked deletes that row from corresponding table.
    should I paint the shape of the button and set editor to a QPushbutton using an QItemDelegate? or what are other approaches ?
    ---
    thanks in advance
    ---
    A situtation never gets so complicated that it can't get any more complicated!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWidget in tableview

    Hmm... it depends on the effect you want to achieve. If you only have a few such rows and the buttons are to be related strictly to the view (not tied to the model in any way), you can use QAbstractItemView::setIndexWidget(). Then you have to connect signals from the buttons to a custom slot in the view (probably through a QSignalMapper) and that's it. The downside is that this approach only makes sense if you have just a few rows, otherwise the whole view starts to slow down. In that case you have to use the delegate approach you mentioned.

    But I would like to suggest an alternative approach. Buttons in table rows came from the web world where it was the easiest way to perform actions on some parts of tables. With desktop applications we have other means of doing that - context menus, toolbars, checkboxes and external buttons. Finally you can also reuse the header or place your own widget inside the view (just like the header) and use it the way you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    sepehr (22nd February 2009)

  4. #3
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWidget in tableview

    Qt Code:
    1. void buttonDelegate::paint( QPainter *painter,const QStyleOptionViewItem &option, const QModelIndex &index ) const
    2. {
    3. if (index.column() == 1)
    4. {
    5. button.rect = option.rect;
    6. button.text = QString::fromUtf8("RemoveRow");
    7. button.state=QStyle::State_Raised |QStyle::State_Enabled;
    8. QApplication::style()->drawControl(QStyle::CE_PushButton,&button, painter);
    9. }
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 
    I managed to make the delegate like that,but there is another question arising here which is how can I catch the paint event when user moves the mouse over the row containing the drawn widget? so I can paint mouse over state of the widget
    ---
    A situtation never gets so complicated that it can't get any more complicated!

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWidget in tableview

    Set the WA_Hover attribute on the viewport of the view and then handle the events through the delegate's editorEvent() method.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QtScript Binding problem with QWidget
    By zack in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 09:38
  2. Replies: 0
    Last Post: 11th November 2008, 15:36
  3. Dynamic updates of a QWidget in a QScrollArea
    By plamkata in forum Qt Programming
    Replies: 2
    Last Post: 20th July 2008, 23:45
  4. Error in put one QWidget in another QWidget
    By xjtu in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2008, 16:05
  5. tableview and layouts
    By zorro68 in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2007, 17:21

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.