Results 1 to 5 of 5

Thread: Item Delegates QStringList and QComboBox

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Item Delegates QStringList and QComboBox

    Hello!

    I’m implementing an item delegate to extend the QStyledItemDelegate. The idea is whe an value is a QStringList i use a QComboBox as editor in the item delegate. But i have some problems

    1. When i set the QStringList in the data of a view (lets say a tree), the cell doesn’t show a text. When i double click the cell a combobox appears, i can make a selection, the combobox closes and nothin is diplayed in the cell

    2. When i want to know what is the selection i can’t figure ir out how to get the item selected in the QComboBox.

    Any ideas?

    Qt Code:
    1. class ItemDelegate : public QStyledItemDelegate {
    2. Q_OBJECT
    3.  
    4. public:
    5. ItemDelegate(QObject *parent = 0): QStyledItemDelegate(parent) {;}
    6. virtual ~ItemDelegate() { ; }
    7.  
    8. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    9. const QModelIndex &index) const;
    10.  
    11. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    12.  
    13. void setModelData(QWidget *editor, QAbstractItemModel *model,
    14. const QModelIndex &index) const;
    15.  
    16. inline void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex& ) const {
    17. editor->setGeometry(option.rect);
    18. }
    19.  
    20. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QWidget* ItemDelegate::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem & option ,
    3. const QModelIndex & index ) const {
    4.  
    5. QVariant data = index.model()->data(index, Qt::EditRole);
    6.  
    7. QWidget* editor;
    8. switch( data.type() ) {
    9. case QVariant::StringList:
    10. editor = new QComboBox(parent);
    11. dynamic_cast<QComboBox*>(editor)->setFrame(false);
    12. break;
    13. default:
    14. editor = QStyledItemDelegate::createEditor(parent, option, index);
    15. }
    16.  
    17. return editor;
    18. }
    19.  
    20. void ItemDelegate::setEditorData(QWidget *editor,
    21. const QModelIndex &index) const {
    22.  
    23. QVariant data = index.model()->data(index, Qt::EditRole);
    24.  
    25. switch( data.type() ) {
    26. case QVariant::StringList:
    27. static_cast<QComboBox*>(editor)->addItems(data.toStringList());
    28. break;
    29. default:
    30. QStyledItemDelegate::setEditorData(editor, index);
    31. }
    32. }
    33.  
    34. void ItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    35. const QModelIndex &index) const
    36. {
    37. QVariant data = index.model()->data(index, Qt::EditRole);
    38.  
    39. switch( data.type() ) {
    40. case QVariant::StringList: {
    41. for( int i = 0; i < static_cast<QComboBox*>(editor)->count(); ++i )
    42. list << static_cast<QComboBox*>(editor)->itemText(i);
    43. model->setData(index, QVariant(list), Qt::EditRole);
    44. break;
    45. }
    46. default:
    47. QStyledItemDelegate::setModelData(editor, model, index);
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Item Delegates QStringList and QComboBox

    Quote Originally Posted by hec_mex View Post
    1. When i set the QStringList in the data of a view (lets say a tree), the cell doesn’t show a text. When i double click the cell a combobox appears, i can make a selection, the combobox closes and nothin is diplayed in the cell
    Return the current string as the Qt::DisplayRole value.

    2. When i want to know what is the selection i can’t figure ir out how to get the item selected in the QComboBox.
    It depends. Usually this is the current item but if you want to know the item the user chooses when the popup is open, it is the currently highlighted item (carried through a signal).
    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. #3
    Join Date
    Jun 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Item Delegates QStringList and QComboBox

    Quote Originally Posted by wysota View Post
    Return the current string as the Qt:isplayRole value.
    Maybe i'm too newbie.

    In wich moment i get the current string?
    Return the current string from where? to where?


    Quote Originally Posted by wysota View Post
    It depends. Usually this is the current item but if you want to know the item the user chooses when the popup is open, it is the currently highlighted item (carried through a signal).
    But outside of the item delegate i can connect any signal or even know the ComboBox

    Lets try an example

    Qt Code:
    1. tree->setModel(new QStandardItemModel);
    2. tree->setItemDelegate(new ItemDelegate);
    3.  
    4. QStringList strList;
    5.  
    6. strList << "A string" << "Another string";
    7.  
    8. item->setData(strList, Qt::EditRole);
    9.  
    10. tree->model()->appendRow(tree->model()->invisibleRootItem(), item);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Item Delegates QStringList and QComboBox

    Quote Originally Posted by hec_mex View Post
    In wich moment i get the current string?
    I have no idea. It's your project, not mine. You should be the one who knows what he wants displayed.

    Return the current string from where?
    From the model's data() method
    to where?
    To the caller.

    But outside of the item delegate i can connect any signal or even know the ComboBox
    I have no idea what that means.
    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. #5
    Join Date
    Jun 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Item Delegates QStringList and QComboBox

    ok pal, it's obvious that we don't understand each other. Thanks anyway.

    Quote Originally Posted by wysota View Post
    I have no idea. It's your project, not mine. You should be the one who knows what he wants displayed.


    From the model's data() method

    To the caller.


    I have no idea what that means.

Similar Threads

  1. Joining together a QComboBox, QStringList, and enum
    By Phlucious in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2012, 16:47
  2. Proxy Model and Item delegates causing issues
    By snydesc in forum Qt Programming
    Replies: 0
    Last Post: 14th March 2012, 18:17
  3. QComboBox addItems() inserts QStringList twice
    By jshafferman in forum Qt Programming
    Replies: 1
    Last Post: 3rd January 2012, 16:53
  4. how to avoid switch statements in item delegates
    By GrahamLabdon in forum Newbie
    Replies: 1
    Last Post: 9th March 2011, 21:11
  5. Add separator to QComboBox using QStringList entries
    By mrknight in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2010, 23:31

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.