Results 1 to 8 of 8

Thread: QCombobox as QTableView delegate

  1. #1
    Join Date
    Oct 2007
    Posts
    11
    Thanks
    3
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default QCombobox as QTableView delegate

    Hello all,

    I would like to display a QComboBox in my QTableView.
    I have tried to use the QItemDelegate, but it only shows it when I editing the cell.
    Now I am using the QStyledItemDelegate in the following way:

    Qt Code:
    1. void FuseDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. painter->save();
    4. switch(index.model()->data(index, Qt::UserRole).toInt()) {
    5. case FuseBitField_boolean: {
    6. QCheckBox *editor = new QCheckBox();
    7. editor->setGeometry(option.rect);
    8. editor->render(painter);
    9. } break;
    10. case FuseBitField_enum: {
    11. QComboBox *editor = new QComboBox();
    12. editor->setGeometry(option.rect);
    13. editor->render(painter);
    14. } break;
    15. default:
    16. return QStyledItemDelegate::paint(painter, option, index);
    17. }
    18. painter->restore();
    19. }
    To copy to clipboard, switch view to plain text mode 

    But it does not displays anything. Editing works fine.

    Is it possible to show a a widget with a delegate in non editing mode, or I should base my design on QTableWidget, and use addCellWidget()?

  2. The following user says thank you to martonmiklos for this useful post:

    frankiefrank (25th July 2011)

  3. #2
    Join Date
    Mar 2008
    Posts
    52
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCombobox as QTableView delegate

    Dear martonmiklos ,
    I think you should change the base Design(QTableWidget), that time only you will get that combobox as a cell widget.
    So what ever you think that is right so please go head with that..

    Thanks&Regards,
    Tavit.

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

    Default Re: QCombobox as QTableView delegate

    You can use setIndexWidget() but be aware of two things:
    1. the widgets will not be tied to your model in any way - they will just occupy space in the table view and you have to interconnect them with your model by yourself
    2. if you add to many such widgets, everything will become slow as hell.

    An alternative is to have a delegate that will use QStyle API to render the looks of a combobox in non-edit mode and will create a real combobox only when a particular cell is edited. It's more work but a much better result.
    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.


  5. #4
    Join Date
    Oct 2007
    Posts
    11
    Thanks
    3
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QCombobox as QTableView delegate

    Hello,

    Thank you for yout ideas.
    @Tavit
    I would like to solve my problem using a Model View solution, because using QTableWidget would case a lot of other programming problems, so it would be tha last way what I would choose.

    @wysota
    I will check it.
    Now I have done:
    Qt Code:
    1. ui->tableViewFuses->openPersistentEditor(fuseModel->index(i, 1));
    To copy to clipboard, switch view to plain text mode 
    and now all of my comboboxes and checkboxes are visible, but under the checkboxes the value of the binary field is visible too:
    qt..png

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

    Default Re: QCombobox as QTableView delegate

    Make your editor fill its background to avoid this effect.
    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.


  7. #6
    Join Date
    Oct 2007
    Posts
    11
    Thanks
    3
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QCombobox as QTableView delegate

    I have set autoFillBackground on my tableview, btu still the same issue.
    I have overridden the following member of the QItemDelegate:
    createEditor. setEditorData, setModelData, updateEditorGeometry
    In which function should I fill the background?
    Or should I reimplement the painte member too?

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

    Default Re: QCombobox as QTableView delegate

    Quote Originally Posted by martonmiklos View Post
    I have set autoFillBackground on my tableview, btu still the same issue.
    The editor, not the view.
    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.


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

    martonmiklos (13th June 2010)

  10. #8
    Join Date
    Oct 2007
    Posts
    11
    Thanks
    3
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QCombobox as QTableView delegate

    Thank you very much it works fine.

    So summarize my problem, and my solution:

    If you need to display a custom QWidget in a view do the following:

    Make your own subclass as it is written in the Spinbox Delegate Example.
    Set autoFillBackground(true) to your editor widget in the createEditor method.
    Set openPersistentEditor on your view's preferred rows/columns.
    And voila your view will display always your preferred widget.
    Last edited by martonmiklos; 13th June 2010 at 17:50.

  11. The following 2 users say thank you to martonmiklos for this useful post:

    d_stranz (21st August 2014), frankiefrank (25th July 2011)

Similar Threads

  1. Replies: 2
    Last Post: 29th December 2012, 15:14
  2. QTableView doesn't use delegate
    By treaves in forum Qt Programming
    Replies: 3
    Last Post: 1st February 2010, 10:45
  3. QTableView in ui with model/delegate
    By tpf80 in forum Qt Programming
    Replies: 7
    Last Post: 7th November 2008, 00:00
  4. QTableView Delegate Usage
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2008, 08:03
  5. Again QTableWidget and QComboBox delegate
    By Aki-Matti in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2008, 14:40

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.