Results 1 to 11 of 11

Thread: QComboBox with QSqlTableModel

  1. #1
    Join Date
    Jun 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QComboBox with QSqlTableModel

    Hello,

    i have QSqlTableModel set to a Table with two columns (id, name). Now i want to have this Table "in my QComboBox". I found some ways to have the ID in the ComboBox with QSqlRelationDelegate - but i have it in one table (so i dont need to setup a relation between two tables).

    Can some tell me, how i can equalize the combobox-index and the model-id?

    I played with QDataWidgetMapper and with the combobox-function setRootModelIndex() but it doesnt worked yet.

    Thanks a lot.

    Havoc][

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QComboBox with QSqlTableModel

    make another model which will return one coloum only.... this would be less mess.

  3. #3
    Join Date
    Jun 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox with QSqlTableModel

    But i want to show the names, and not the IDs? What should i do with the second (new) model with only the ID-Column?

    Thanks and Regards.
    Havoc][

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QComboBox with QSqlTableModel

    dont show the id col... show the names....

    model view prog has three parts..

    data
    model
    view

    so different model can present different view of same data...

  5. #5
    Join Date
    Jun 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox with QSqlTableModel

    okay, i tried to set the combobox-view to another model:

    Qt Code:
    1. void zeige_kategorien_combobox(QComboBox *cbanzeige)
    2. {
    3. if ( datenbankverbindung )
    4. {
    5. QSqlTableModel *kategorie_combobox_model = new QSqlTableModel;
    6. kategorie_combobox_model->setTable("kategorie");
    7. kategorie_combobox_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    8. kategorie_combobox_model->select();
    9. QSqlTableModel *kategorie_combobox_model2 = new QSqlTableModel;
    10. kategorie_combobox_model2->setTable("kategorie");
    11. kategorie_combobox_model2->setEditStrategy(QSqlTableModel::OnManualSubmit);
    12. kategorie_combobox_model2->select();
    13. cbanzeige->setModel(kategorie_combobox_model);
    14. kategorie_combobox_model2->removeColumn(0);
    15. cbanzeige->view()->setModel(kategorie_combobox_model2);
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    But that didnt worked.

    Is there no easy way to set one column of a model to the Indexes, and one Column of a model to the shown names?

    Thanks,
    Havoc][

  6. #6
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QComboBox with QSqlTableModel

    qt assistant provides the example as

    Qt Code:
    1. model->setTable("employee");
    2. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    3. model->select();
    4. model->removeColumn(0); // don't show the ID
    5. model->setHeaderData(0, Qt::Horizontal, tr("Name"));
    6. model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
    7.  
    8. QTableView *view = new QTableView;
    9. view->setModel(model);
    10. view->show();
    To copy to clipboard, switch view to plain text mode 
    i dono why its not working for you..

  7. #7
    Join Date
    Jun 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox with QSqlTableModel

    Yes, *THATS* working - of course. But in this case i only have the names OR the IDs. And i would have to show the names, and give back the ID. So i tought i have to set the combobox indexes to my IDs.

  8. #8
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QComboBox with QSqlTableModel

    let me tell what i understood...

    u have a table like

    ID NAME
    1 Name1
    2 Name2

    now u want a combobox to show
    Name1 --> combobox index 0
    Name2 --> combobox index 1

    rite??

  9. #9
    Join Date
    Jun 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox with QSqlTableModel

    Not exactly :-).

    I have a table:
    ID NAME
    101 name1
    102 name2
    103 name3
    104 name4

    And i want to show NAME in my ComboBox. And i someone set it to name2 i want to get "102" (the ID) :-).

  10. #10
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QComboBox with QSqlTableModel

    bit tricky..

    i think you need to subclass QSqlTableModel and/or QCombobox... and define your own Qt::userrole on the ids

    QCombobox::itemData() can fetch the id...

  11. #11
    Join Date
    Jun 2009
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox with QSqlTableModel

    Okay - i think, to subclass and create my own display-type is very circuitous for this case.
    I asked in #qt (freenode) and daff told me that i could use the combobox index to get the data from my model at the same index :-).

    So i did:
    Qt Code:
    1. model->index(ui->combobox->currentIndex(), 0).data(0).toString().toInt()
    To copy to clipboard, switch view to plain text mode 

    That works for me.

    Thanks anyway!

Similar Threads

  1. QItemDelegate, QDataWidgetMapper and QComboBox
    By cydside in forum Qt Programming
    Replies: 7
    Last Post: 8th April 2009, 19:44
  2. QDataWidgetMapper and QCombobox
    By miraks in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2008, 18:53
  3. QComboBox - Few virtual methods
    By gruszczy in forum Qt Programming
    Replies: 17
    Last Post: 16th July 2008, 17:08
  4. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 20:25
  5. using QComboBox as an ItemView
    By EricTheFruitbat in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2007, 17:14

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.