Results 1 to 5 of 5

Thread: Bind a QComboBox to a Database

  1. #1
    Join Date
    Jul 2008
    Posts
    13
    Thanks
    2

    Default Bind a QComboBox to a Database

    Is it possible to bind a QComboBox to a database relation? For example, I have an authors relation and would like to load all of the authors into a QComboBox. Here is the code for what I have tried:

    Qt Code:
    1. addBookDialog::addBookDialog(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. ui.setupUi(this);
    5. createConnection();
    6.  
    7. QSqlQueryModel sqlAuthors;
    8. sqlAuthors.setQuery("SELECT nr, lname FROM authors;");
    9. ui.authorsComboBox->setModel(&sqlAuthors);
    10. //ui.authorsComboBox->setModelColumn(0);
    11.  
    12. }
    13.  
    14. addBookDialog::~addBookDialog()
    15. {
    16.  
    17. }
    18.  
    19. bool addBookDialog::createConnection()
    20. {
    21. db = QSqlDatabase::addDatabase("QSQLITE");
    22. db.setDatabaseName("stuff.s3db");
    23. if (!db.open()) {
    24. // QMessageBox::critical(0, tr("Cannot open database"), db.lastError().text());
    25. return false;
    26. }
    27. return true;
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

    Cheers

  2. #2
    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: Bind a QComboBox to a Database

    try this
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("test.db");
    3.  
    4. if (!db.open()) {
    5. qDebug() << db.lastError().text();
    6. return;
    7. }
    8.  
    9. QSqlQuery query;
    10. qDebug() << query.exec("CREATE TABLE test (id int PRIMARY KEY, test nvarchar)");
    11.  
    12. qDebug() << query.exec("INSERT INTO test VALUES(0, 'a')");
    13. qDebug() << query.exec("INSERT INTO test VALUES(1, 'b')");
    14. qDebug() << query.exec("INSERT INTO test VALUES(2, 'c')");
    15. qDebug() << query.exec("INSERT INTO test VALUES(3, 'd')");
    16. qDebug() << query.exec("SELECT test FROM test WHERE id = 1");
    17.  
    18. model->setQuery("SELECT id, test FROM test");
    19. model->setHeaderData(0, Qt::Horizontal, tr("id"));
    20. model->setHeaderData(1, Qt::Horizontal, tr("test"));
    21.  
    22. QTableView *view = new QTableView;
    23.  
    24. QComboBox *cb = new QComboBox();
    25. cb->setModel(model);
    26. cb->setView(view);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2008
    Posts
    13
    Thanks
    2

    Default Re: Bind a QComboBox to a Database

    Thanks very much. There's just one last thing I cannot get working. I want to make the "nr" column invisible, so that only the author's names are visible. Here is what I have tried:
    Qt Code:
    1. addBookDialog::addBookDialog(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. ui.setupUi(this);
    5.  
    6. QSqlQueryModel *sqlAuthors = new QSqlQueryModel;
    7. sqlAuthors->setQuery("SELECT nr, fname || ' ' || minitial || ' ' || lname FROM authors;");
    8. sqlAuthors->setHeaderData(0,Qt::Horizontal, tr("nr"));
    9. sqlAuthors->setHeaderData(1, Qt::Horizontal, tr("Name"));
    10.  
    11. QTableView *view = new QTableView;
    12. view->verticalHeader()->hide();
    13. view->horizontalHeader()->hide();
    14. view->setColumnHidden(0, true);
    15. // view->setColumnWidth(0,0);
    16. view->setSelectionBehavior(QAbstractItemView::SelectRows);
    17. view->setSelectionMode(QAbstractItemView::SingleSelection);
    18. ui.authorsComboBox->setModel(sqlAuthors);
    19. ui.authorsComboBox->setView(view);
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

    I have also tried view->hideColumn(0), but that does not hide it either. I feel I am once again missing something simple.

    Cheers.

  4. #4
    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: Bind a QComboBox to a Database

    try this
    Qt Code:
    1. ...
    2. view->setSelectionBehavior(QAbstractItemView::SelectRows);
    3. view->setSelectionMode(QAbstractItemView::SingleSelection);
    4. ui.authorsComboBox->setModel(sqlAuthors);
    5. ui.authorsComboBox->setView(view);
    6. view->setColumnHidden(0, true);
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to spirit for this useful post:

    onefootswill (16th August 2008)

  6. #5
    Join Date
    Jul 2008
    Posts
    13
    Thanks
    2

    Default Re: Bind a QComboBox to a Database

    Cheers for that. With a bit of experimentation, I found that I had to hide the second column. This was no big hassle as I just swapped the order of the return values in the SQL SELECT statement. For one reason or another, it did not like having the first column (index 0) hidden. The code is:
    Qt Code:
    1. sqlAuthors = new QSqlQueryModel;
    2. sqlAuthors->setQuery("SELECT fname || ' ' || minitial || ' ' || lname, nr FROM authors;");
    3. sqlAuthors->setHeaderData(0,Qt::Horizontal, tr("nr"));
    4. sqlAuthors->setHeaderData(1, Qt::Horizontal, tr("Name"));
    5.  
    6. view = new QTableView;
    7. view->verticalHeader()->hide();
    8. view->horizontalHeader()->hide();
    9. view->setSelectionBehavior(QAbstractItemView::SelectRows);
    10. view->setSelectionMode(QAbstractItemView::SingleSelection);
    11.  
    12. ui.authorsComboBox->setModel(sqlAuthors);
    13. ui.authorsComboBox->setView(view);
    14. view->setColumnHidden(1, true);
    To copy to clipboard, switch view to plain text mode 

    Thanks very much for your help.

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  3. Database Master-Detail Entry Form
    By Phan Sin Tian in forum Newbie
    Replies: 4
    Last Post: 3rd February 2008, 14:31
  4. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 19:25
  5. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53

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.