Results 1 to 4 of 4

Thread: Qt4 Databases and Model-View problem...

  1. #1
    Join Date
    Feb 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Qt4 Databases and Model-View problem...

    Hi there folks!

    First of all I am a bit of a newbie (both in C++ and in Qt), and I am having a bit of trouble with an application I am writing to manage databases.

    the following code is giving me a very bad headache...

    Qt Code:
    1. void mainwindow::manipdatabase() {
    2. QSqlDatabase *meddb = new QSqlDatabase;
    3. meddb->addDatabase("QMYSQL");
    4. meddb->setHostName ("localhost");
    5. meddb->setDatabaseName ("medadmin");
    6. meddb->setUserName ("medadmin");
    7. meddb->setPassword ("DEEPthoughts1234567890");
    8. if (!meddb->open()) {
    9. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    10. qApp->tr("Unable to establish a database connection.\n"
    11. "This program requires MySql to operate. Please read "
    12. "the Qt SQL driver documentation for information how "
    13. "to build it.\n\n"
    14. "Click Cancel to exit."), QMessageBox::Cancel);
    15. }
    16. QSqlTableModel *model = new QSqlTableModel(0, meddb);
    17. model->setTable ("test");
    18. model->setEditStrategy (QSqlTableModel::OnManualSubmit);
    19. model->select();
    20. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    21. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Surname"));
    22.  
    23. ui->View->setWindowTitle ("Patients List");
    24. ui->View->setModel (model);
    25. ui->View->setSelectionBehavior (QAbstractItemView::SelectRows);
    26. ui->View->setSelectionMode (QTableView::SingleSelection);
    27. ui->View->setVisible (true);
    28. }
    To copy to clipboard, switch view to plain text mode 

    It complains about the line were the model is declared according to qmake "expected QObject", but shouldn't that set the parent widget to none (and therefore work)???

    My apologies for being such a newbie, I hope that any of you could please help me (if you want you can always write a little line of code explaining how it should be done).

    Thank you in advance,


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

    Default Re: Qt4 Databases and Model-View problem...

    You use QSqlDatabase incorrectly.

    You should do it like this:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    2. db.setHostName("acidalia");
    3. db.setDatabaseName("customdb");
    4. db.setUserName("mojito");
    5. db.setPassword("J0a1m8");
    6. bool ok = db.open();
    To copy to clipboard, switch view to plain text mode 

    As for the error - the model constructor awaits an object but you are giving it a pointer. I suggest you don't give the second argument at all, it's optional and if you initialize the database object properly, the model will pick it up and work on 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.


  3. #3
    Join Date
    Feb 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Qt4 Databases and Model-View problem...

    Thank you very much for your help!

    If it wasn't for you I probably would have never got there by myself!

    If it wouldn't be way too troublesome I would also like to ask a little question: the table view shows up completely blank, is there anything I need to do to at least have the column headers showing up???????

    And once more, thank you.

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

    Default Re: Qt4 Databases and Model-View problem...

    Yes, you need to set a model on the table. Instead of a table view you can use a table widget and tell it how many columns it should have. That should be enough to get the horizontal header.
    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.


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.