Results 1 to 5 of 5

Thread: Qcombobox Binding With text and id ???

  1. #1
    Join Date
    Jul 2012
    Posts
    40
    Qt products
    Qt4 Qt/Embedded

    Default Qcombobox Binding With text and id ???

    For a combobox i have added list of items using query from database table.
    query.exec("select bookname from books_totallist");
    while(query.next())
    {
    ui->comboBoxbook->addItem(query.value(0).toString());
    }
    Now Say combobox contains items as book1,book2,book3,.....etc.Each book has corresponding id in table.
    If i select any book i need to get respective id of the book..? So please provide me some hint on this............



    Regards
    sai

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qcombobox Binding With text and id ???

    QComboBox::addItem ( const QString & text, const QVariant & userData = QVariant() )
    Let the text be the same as what you added. Also add the id [say query.value(1)] . Now use functions like QComboBox::itemData ( int index, int role = Qt::UserRole ) const to get Id.
    Go through the QComboBox documentation and find related functions.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qcombobox Binding With text and id ???

    You could also experiment with using a QSqlQueryModel and setting that as the model of QComboBox.

    Cheers,
    _

  4. #4
    Join Date
    Jul 2012
    Posts
    40
    Qt products
    Qt4 Qt/Embedded

    Default Re: Qcombobox Binding With text and id ???

    Yeah i have done that in same way.....Below is my code..
    QSqlQueryModel *model = new QSqlQueryModel;
    model->setQuery("SELECT bookID,bookName FROM books_totallist");
    model->setHeaderData(0,Qt::Horizontal, tr("Book_ID"));
    model->setHeaderData(1, Qt::Horizontal, tr("Book_Name"));
    QTableView *view = new QTableView;
    view->setSelectionBehavior(QAbstractItemView::SelectRow s);
    view->setSelectionMode(QAbstractItemView::SingleSelecti on);
    ui->authorsComboBox->setModel(model);
    ui->authorsComboBox->setView(view);
    //view->setColumnHidden(0, true); --------->>here now for my combobox i am hidding " id coloumn " ....SO that showing only names in combobox.
    Now if we select any bookname,i need to get the corresponding id of that...?How could this be acheived ????

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qcombobox Binding With text and id ???

    Use QComboBox::currentIndex() to get the model row number, then use the model's index() and data() to get the id column value.

    You should not need a custom view for the combo box if you are only showing one column from a multi-column model. Try QComboBox::setModelColumn().
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. binding text in QMl to c++
    By rajeshk.ict in forum Newbie
    Replies: 1
    Last Post: 16th March 2012, 08:40
  2. QComboBox text alignment
    By mentalmushroom in forum Qt Programming
    Replies: 1
    Last Post: 31st January 2012, 21:33
  3. Get text from QCombobox.
    By dragon in forum Newbie
    Replies: 13
    Last Post: 31st January 2012, 20:50
  4. How to use rich text in a QComboBox?
    By aarunt1 in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2010, 20:20
  5. QComboBox with fixed text?
    By squidge in forum Qt Programming
    Replies: 0
    Last Post: 14th November 2009, 21:39

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.