Results 1 to 13 of 13

Thread: How to get index of a combobox delegate selection

  1. #1
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to get index of a combobox delegate selection

    In a delegate that uses a QComboBox as the editor, how does one retrieve the index of the selected item ?
    I can only get the selected text from the item.
    Qt Code:
    1. QTableWidgetItem* item = tableWidget->item(row, col);
    2. qDebug() << "data is: " << item->data(0);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How to get index of a combobox delegate selection

    Use QComboBox API to ask for an index of an item with a given text.
    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
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get index of a combobox delegate selection

    maybe this exapmle QTDIR/examples/widgets/cons/imagedelegate.cpp will help you.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get index of a combobox delegate selection

    Quote Originally Posted by wysota View Post
    Use QComboBox API to ask for an index of an item with a given text.
    This doesn't make sense.
    QComboBox::findText() is what I want, but QComboBox has no idea what is inside the combobox held in the delegate class, and the delegate class doesn't have any methods to get data.
    Last edited by vieraci; 21st July 2009 at 01:10. Reason: updated contents

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

    Default Re: How to get index of a combobox delegate selection

    Could you explain what you want to achieve?
    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.


  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get index of a combobox delegate selection


  7. #7
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get index of a combobox delegate selection

    My delegate class:
    Qt Code:
    1. class ComboBoxDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. ComboBoxDelegate(QObject *parent = 0);
    6.  
    7. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    8. const QModelIndex &index) const;
    9. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    10. void setModelData(QWidget *editor, QAbstractItemModel *model,
    11. const QModelIndex &index) const;
    12. int indexOf(QString textToFind);
    13. };
    To copy to clipboard, switch view to plain text mode 

    Initialise the delegate:
    Qt Code:
    1. sqlClause = "SELECT cat_text, return_value FROM phone_codes"
    2. " order by return_value";
    3. phone_type_delegate->items->setQuery(sqlClause);
    4. m_ui->phoneTableWidget->setItemDelegateForColumn(0,phone_type_delegate);
    To copy to clipboard, switch view to plain text mode 

    Method to get selection:
    Qt Code:
    1. void ClientDetailsDialog::onPhoneTableWidgetItemChanged(QTableWidgetItem* item)
    2. {
    3. QTableWidgetItem* item1 = m_ui->phoneTableWidget->item(item->row(), 0);
    4. }
    To copy to clipboard, switch view to plain text mode 

    I can get the combo box text from the above code, but I need to retrieve the index of the combobox selection.

    I can do it like this:

    Qt Code:
    1. int ComboBoxDelegate::indexOf(QString textToFind)
    2. {
    3. int retVal = -1;
    4. for (int x = 0; x < items->rowCount(); ++x)
    5. {
    6. if (items->data(items->index(x, 0)).toString() == textToFind)
    7. {
    8. retVal = x;
    9. break;
    10. }
    11. }
    12. return retVal;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Unless there's a simpler way of retrieving the index of the combo box, I'll have to settle for this solution. Nothing wrong with it, just figure there must be some method that simply gives the index.

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get index of a combobox delegate selection

    Did you come across QComboBox::findText or QComboBox::findData ??

  9. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get index of a combobox delegate selection

    come on man, I've already posted an example, it has all what you want
    ...QTDIR/examples/widgets/cons/imagedelegate.cpp...
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: How to get index of a combobox delegate selection

    How do your setEditorData() and setModelData() implementations look like?
    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.


  11. #11
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get index of a combobox delegate selection

    Quote Originally Posted by spirit View Post
    come on man, I've already posted an example, it has all what you want
    I don't see it...am I retarded or something ???

    The only data methods QItemDelegate has is:

    setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)
    setEditorData(QWidget *editor, const QModelIndex &index)

    These are all SET something and the combobox in each method is a parameter. It doesn't address my solution. Where's the real combobox ? from what I've read, it doesn't really exist, only when you begin to edit the item it's created.

    So, there IS no method to return the combo box index...because there is no real combo box ! (have I answered my own post here ?)

    I wrote a method in my previous post that gets the index via the QSqlQueryModel member I added to my custom delegate class, as I said, it works, but is there another not-so-obvious method that doesn't require a hammer ?

  12. #12
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get index of a combobox delegate selection

    Quote Originally Posted by wysota View Post
    How do your setEditorData() and setModelData() implementations look like?
    Qt Code:
    1. void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    2. {
    3. QComboBox *edit = qobject_cast<QComboBox *>(editor);
    4. QString value = index.model()->data(index, Qt::EditRole).toString();
    5. edit->setCurrentIndex(edit->findText(value));
    6. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    2. const QModelIndex &index) const
    3. {
    4. QComboBox *edit = qobject_cast<QComboBox *>(editor);
    5. QString oldVal = index.model()->data(index, Qt::EditRole).toString();
    6. QString newVal = edit->currentText();
    7. if (oldVal != newVal)
    8. {
    9. model->setData(index, newVal);
    10. }
    11. // qDebug() << "setModelData: index = " << edit->currentIndex();
    12. }
    To copy to clipboard, switch view to plain text mode 

    I know what you're going to say right here, the answer is in setModelData. sure, yes.
    But I don't use QComboBox::currentIndex() here, I need to access it outside these methods to evaluate the selections/entry of both columns before calling a saveRecord() method.

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

    Default Re: How to get index of a combobox delegate selection

    So save it as a custom role of your model from within setModelData(). At this point you have to either accept the change or reject it. The combobox will be destroyed after that so you can't access it.
    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. ComboBox Completer Index Problem
    By majatu in forum Qt Programming
    Replies: 3
    Last Post: 7th June 2009, 15:30
  2. NUll character output for combobox index
    By mdskpr778 in forum Qt Programming
    Replies: 2
    Last Post: 8th October 2008, 19:22
  3. Combobox Delegate 25.000 records
    By aekilic in forum Qt Programming
    Replies: 9
    Last Post: 29th July 2008, 13:26
  4. How to draw selection in delegate
    By yogeshm02 in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2008, 21:24
  5. Qtopia core 4.2.2 cross compile make error
    By smiyai18 in forum Installation and Deployment
    Replies: 2
    Last Post: 28th August 2007, 18:04

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.