Results 1 to 14 of 14

Thread: slider control and combo box in grid view

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default slider control and combo box in grid view

    Hi,

    Is it possible and I'm sure it is, to have a view that has a slider control in one of the columns and drop down as well as edit fields? Would you use a QTableWidget/view?

    Regards,
    Steve

  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: slider control and combo box in grid view

    Yes. You need to use QTableView or QTableWidget (or the tree variants) and provide a custom delegate that will create appropriate widgets as editors.

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slider control and combo box in grid view

    Hi,

    Thanks for that. I guess I create a delegate for the drop down box derived from QItemDelegate :

    Qt Code:
    1. class DropDownDelegate : QItemDelegate
    2. {
    3. DropDownDelegate( QObject *parent = 0 );
    4. QWidget* createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
    5. QModelIndex &index );
    6.  
    7. //etc...
    8. };
    9.  
    10. DropDownDelegate::DropDownDelegate( QObject *parent ) : QItemDelegate(parent)
    11. {
    12. }
    13.  
    14. QWidget* DropDownDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    15. {
    16. QComboBox *pCombo = new QComboBox( parent );
    17. // etc...
    18. return pCombo;
    19. }
    To copy to clipboard, switch view to plain text mode 

    I would need to provide setEditorData method and setModelData and updateEditorGeometry methods?

    How do I set the above delegate for say the first column of the table view?

    Regards,
    Steve

  4. #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: slider control and combo box in grid view

    Quote Originally Posted by steg90 View Post
    Thanks for that. I guess I create a delegate for the drop down box derived from QItemDelegate
    You can create a single delegate for all types of editors. Just return a different widget depending on index.column() value.

    I would need to provide setEditorData method and setModelData and updateEditorGeometry methods?
    The default updateEditorGeometry() should be fine. As for the other two methods you should reimplement them. Again, depending on the column number cast the editor to a proper class, fetch the value and update the model (or vice versa).

    How do I set the above delegate for say the first column of the table view?
    QAbstractItemView::setItemDelegate(), QAbstractItemView::setItemDelegateForColumn(), QAbstractItemView::setItemDelegateForRow()

  5. The following 2 users say thank you to wysota for this useful post:

    bkastel1 (20th November 2007), steg90 (20th November 2007)

  6. #5
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slider control and combo box in grid view

    Many thanks for your help and quick response

    Steve

  7. #6
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slider control and combo box in grid view

    Hi again,

    I've now got a QTableView which displays a QProgressBar in column one. I want to display a check box in column two. My thinking is I could go ahead and do another custom delegate for this or within the createEditor function have the following :

    Qt Code:
    1. QWidget *SliderDelegate::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem &option, const QModelIndex &index) const
    3. {
    4. QWidget* editor = NULL;
    5. switch( index.column() )
    6. {
    7. case 1 :
    8. {
    9. editor = new QProgressBar(parent);
    10. // etc
    11. break;
    12. }
    13. case 2 :
    14. {
    15. editor = new QCheckBox(parent);
    16. // etc
    17. break;
    18. }
    19. default :
    20. editor = QItemDelegate::createEditor( parent, option, index );
    21. }
    22. return editor;
    23. }
    To copy to clipboard, switch view to plain text mode 

    Of course, I'd also have to do the index.column() function within the setEditorData, setModelData functions...Is this ok, or am I talking like I've not got a clue?!

    Thanks,
    Steve

  8. #7
    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: slider control and combo box in grid view

    Quote Originally Posted by steg90 View Post
    Of course, I'd also have to do the index.column() function within the setEditorData, setModelData functions...Is this ok, or am I talking like I've not got a clue?!
    Yes, it's exactly like you should implement it.

    As for the progress bar you might want to go for a different approach. It's hard to call the progress bar an editor, because it only displays some data. You might instead reimplement the delegate's paint routine and paint the progress bar in the cell using QStyle. The approach was described a few time on the forum, it's probably even in the wiki.

  9. #8
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slider control and combo box in grid view

    Thanks again!

    One more question...is it possible to show these widgets in the table view without having to click into a cell before they are displayed? Does this make sense???

    Thanks,
    Steve

  10. #9
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default View slider control without having to click into cell

    Is it possible to display a slider control within the table view without having to double click on the cell?

    I don't know if this is possible through delegate?

    Thanks,
    Steve

  11. #10
    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: slider control and combo box in grid view

    Quote Originally Posted by steg90 View Post
    One more question...is it possible to show these widgets in the table view without having to click into a cell before they are displayed? Does this make sense???
    Yes, that's exactly what I meant when speaking about the progress bar. Using index widgets (QAbstractItemView::setIndexWidget) is also an option, but it will get slow if you use too many of them. The same goes for every other widget of course. Search the forum - I'm sure there have been at least two or three threads about faking a progress bar using a delegate.

  12. #11
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slider control and combo box in grid view

    Hi,

    So, in order to display say a QSlider within a cell in the table without first having to double click on the cell, I need to paint it myself?

    Regards,
    Steve

  13. #12
    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: slider control and combo box in grid view

    Yes, or set it as an index widget.

  14. #13
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slider control and combo box in grid view

    Hi,

    I've set it as an index widget within the view. I guess I didn't need a custom delegate for it?!

    This is the code I've done to set column two to use a QSlider:

    Qt Code:
    1. QModelIndex modelIndex = m_pmodel->index(0,1,QModelIndex());
    2. QSlider *slider = new QSlider(Qt::Horizontal,ui.tableView);
    3. slider->setMinimum( 0 );
    4. slider->setMaximum( 100 );
    5. ui.tableView->setIndexWidget( modelIndex, slider );
    To copy to clipboard, switch view to plain text mode 

    Think I got confused with using a custom delegate, guess the use of this would have been as an editor in the table - for instance, if one of the columns I wanted an input mask?

    Regards,
    Steve

  15. #14
    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: slider control and combo box in grid view

    Quote Originally Posted by steg90 View Post
    I've set it as an index widget within the view. I guess I didn't need a custom delegate for it?!
    Correct. Just don't insert more than 10 of those

    Think I got confused with using a custom delegate, guess the use of this would have been as an editor in the table - for instance, if one of the columns I wanted an input mask?
    createEditor() returns an editor - whatever one means by that - be it a combobox or a modified QLineEdit, it doesn't matter. Another thing is displaying items. Currently your index widget has nothing to do with the item (and the model) itself - you have to program it independently of the model-view architecture.

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

    steg90 (21st November 2007)

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.