Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Database driven application builds without errors, but resulting screen is blank.
Qt 6.11 is out! See what's new in the release blog

Database driven application builds without errors, but resulting screen is blank.

Scheduled Pinned Locked Moved General and Desktop
17 Posts 3 Posters 7.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    geytjie
    wrote on last edited by
    #1

    I have adjusted the tables example that comes with Qt 4.7.4, which uses sqlite. In the example, however, the data is hard-coded into the app, which is of course of no use if you need to build an application that can connect and update the application's db regularly.
    I must be doing something wrong, but I just don't see where.
    My code in .cpp file:
    @#include <QtGui>
    #include <QtSql>

    #include "myCon.h"
    #include <QSqlQuery>

    void initializeModel(QSqlTableModel *model)
    {
    model->setTable("wines");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();

    model->setHeaderData(0, Qt::Horizontal, QObject::tr("id"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("name"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("winemaker_id"));
    

    }

    QTableView *createView(QSqlTableModel *model, const QString &title = "")
    {
    QTableView *view = new QTableView;
    view->setModel(model);
    #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_SIMULATOR)
    Q_UNUSED(title)
    #else
    view->setWindowTitle(title);
    #endif
    return view;
    }

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    if (!createConnection())
    return 1;

    QSqlTableModel model;
    
    initializeModel(&model);
    

    #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_SIMULATOR)
    QTabWidget *tabWidget = new QTabWidget;
    tabWidget->addTab(createView(&model), "View 1");
    tabWidget->addTab(createView(&model), "View 2");

    tabWidget->showMaximized();
    

    #else
    QTableView *view1 = createView(&model, QObject::tr("Table Model (View 1)"));
    QTableView *view2 = createView(&model, QObject::tr("Table Model (View 2)"));

    view1->show();
    view2->move(view1->x() + view1->width() + 20, view1->y());
    view2->show();
    

    #endif

    return app.exec&#40;&#41;;
    

    }
    @
    and the header file:
    @
    #ifndef CONNECTION_H
    #define CONNECTION_H

    #include <QMessageBox>
    #include <QSqlDatabase>
    #include <QSqlError>
    #include <QSqlQuery>

    /*
    This file defines a helper function to open a connection to an
    in-memory SQLITE database and to create a test table.

    If you want to use another database, simply modify the code
    below. All the examples in this directory use this function to
    connect to a database.
    

    */
    //! [0]
    static bool createConnection()
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("wyne.sqlite");
    if (!db.open()) {
    QMessageBox::critical(0, qApp->tr("Cannot open database"),
    qApp->tr("Unable to establish a database connection.\n"
    "This example needs SQLite support. Please read "
    "the Qt SQL driver documentation for information how "
    "to build it.\n\n"
    "Click Cancel to exit."), QMessageBox::Cancel);
    return false;
    }

    QSqlQuery query;
    query.exec("select id, name, winemaker_id FROM wines"&#41;;
    
    
    
    return true;
    

    }
    //! [0]

    #endif
    @

    I would appreciate some help on this. From the very deepest bottom of my hart - still have a lot of work to do on this app and the deadline is next week.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      Your database does not contain any tables nor is it populated with any data. Therefor there is nothing for the view to display.
      @
      static bool createConnection()
      {
      // ...

      QSqlQuery query;
      qDebug() << query.exec&#40;"create table wines(id integer, name text, winemaker_id integer&#41;"&#41;;
      qDebug() << query.exec&#40;"insert into wines values(1, 'one', 0&#41;"&#41;;
      qDebug() << query.exec&#40;"insert into wines values(2, 'two', 0&#41;"&#41;;
      qDebug() << query.exec&#40;"insert into wines values(3, 'three', 0&#41;"&#41;;
      
      return true;
      

      }
      @

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fluca1978
        wrote on last edited by
        #3

        Check what the query @"select id, name, winemaker_id FROM wines"@ gives as result when executed on the database directly.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          geytjie
          wrote on last edited by
          #4

          Hi
          in sqlite manager it displays all the records, and if I save that file to a svg file it comes out like this:
          1,"fiffi","hond"
          2,"betsie","kat"
          3,"gerrie","wolf"
          4,"lettie","hond"
          5,"wagner","wolf"

          Which are all the records.
          So there is nothing wrong with the sql statement or the db, it seems.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            [quote author="geytjie" date="1316661056"]
            @
            query.exec("select id, name, winemaker_id FROM wines");
            @
            [/quote]
            Is this query successful?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              geytjie
              wrote on last edited by
              #6

              [quote author="Lukas Geyer" date="1316669649"]Your database does not contain any tables nor is it populated with any data. Therefor there is nothing for the view to display.
              @
              static bool createConnection()
              {
              // ...

              QSqlQuery query;
              qDebug() << query.exec("create table wines(id integer, name text, winemaker_id integer&#41;"&#41;;
              qDebug(&#41; << query.exec("insert into wines values(1, 'one', 0&#41;"&#41;;
              qDebug(&#41; << query.exec("insert into wines values(2, 'two', 0&#41;"&#41;;
              qDebug(&#41; << query.exec("insert into wines values(3, 'three', 0&#41;"&#41;;
              
              return true;
              

              }
              @[/quote]

              Pardon my ignorance - should I add this in my .cpp file? I have the .pro (which, as I understand it, puts everything toghether), the .cpp file (which does the work, I think), .h which calls the different components, as I understand. I think I get a little confused how to use what and where. :-(

              1 Reply Last reply
              0
              • G Offline
                G Offline
                geytjie
                wrote on last edited by
                #7

                I haven't yet showed my .pro file, here is the code:

                @
                HEADERS = myCon.h
                SOURCES = myTablemodel.cpp
                QT += sql
                #install
                target.path = $$[QT_INSTALL_EXAMPLES]/sql/myTablemodel
                sources.files = $$SOURCES * .h $$RESOURCES $$FORMS myTablemodel.pro
                INSTALLS += target sources

                symbian: include($$PWD/../../symbianpkgrules.pri)
                maemo5: include($$PWD/../../maemo5pkgrules.pri)

                OTHER_FILES +=
                qtc_packaging/debian_harmattan/rules
                qtc_packaging/debian_harmattan/README
                qtc_packaging/debian_harmattan/control
                qtc_packaging/debian_harmattan/compat
                qtc_packaging/debian_harmattan/changelog
                QMAKE_MAC_SDK = /Developer/SDKs/MacOSX10.6.sdk
                @

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #8

                  The snippet I have posted just creates a table and inserts some data to see if the UI code works as expected (and it does).

                  For further diagnosis of your problem please alter your example the following way
                  @
                  #ifndef CONNECTION_H
                  #define CONNECTION_H

                  #include <QMessageBox>
                  #include <QSqlDatabase>
                  #include <QSqlError>
                  #include <QSqlQuery>
                  #include <QtDebug> // <-- HERE

                  /*
                  This file defines a helper function to open a connection to an
                  in-memory SQLITE database and to create a test table.

                  If you want to use another database, simply modify the code
                  below. All the examples in this directory use this function to
                  connect to a database.
                  

                  */
                  //! [0]
                  static bool createConnection()
                  {
                  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                  db.setDatabaseName("wyne.sqlite");
                  if (!db.open()) {
                  QMessageBox::critical(0, qApp->tr("Cannot open database"),
                  qApp->tr("Unable to establish a database connection.\n"
                  "This example needs SQLite support. Please read "
                  "the Qt SQL driver documentation for information how "
                  "to build it.\n\n"
                  "Click Cancel to exit."), QMessageBox::Cancel);
                  return false;
                  }

                  QSqlQuery query;
                  qDebug() << query.exec&#40;"select id, name, winemaker_id FROM wines"&#41;; // <-- HERE
                  qDebug(&#41; << query.lastError().type() << query.lastError().text(); // <-- HERE
                  
                  
                  return true;
                  

                  }
                  //! [0]
                  @
                  and provide the debug output.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    geytjie
                    wrote on last edited by
                    #9

                    Thanx for helping.

                    I did the test with create table - and it showed in the program. But that is obviously not from the db that I'm trying to show the data from.
                    I deleted that test code and inserted the error test you suggested - it returned the following in the debug application output window:
                    @
                    true
                    0 " "
                    @
                    but the program still shows the values we inserted with the test create table. This puzzles me. Does the compiler keep this in the memory?

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      fluca1978
                      wrote on last edited by
                      #10

                      No way the compiler will hard code database data into your application!
                      However I don't understand the real problem: the application code is showing you there is no error and that the query (select) works fine. You say that the program shows the values into the database, that is reasonably for the kind of query you have done. Can you explain better what is the problem=

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        geytjie
                        wrote on last edited by
                        #11

                        I will try to explain it the way my web-based brain figured it out, ok? So please don't get annoyed with me :-)
                        I have created a db wyne.sqlite which is going to be hosted somewhere. I have to create an application that can be installed on cellphones with a starting copy of this db on their phone. This app must enable them to search for wines according to certain criteria and, once a week or so, the app must request permission to update the db. When the user clikcs ok, the app makes the connection with the server on-line and updates.
                        We also want to give the user a membership to be able to make comments etc.
                        Now I am trying to connect to this db (located in the same folder) and extract the data to display to the user, according to the criteria they enter. We don't just want to host a web app that they must connect to everytime it runs.
                        It seems that the connection with the db is made ok, but the app displays nothing.

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          fluca1978
                          wrote on last edited by
                          #12

                          Please change your initializeModel method so that it provides a few more debug information, for instance after the model->select() place a debug print of the number of found rows, just to see where the model is behaving oddly:

                          @model->select();
                          qDebug() << "Found rows " << model->rowCount();
                          qDebug() << "Database error " << model->database()->lastError()->text();
                          @

                          This is just a way to see if there is something wrong with the database connection and/or the query executed from the model. I don't think there is nothing wrong with the model, there must be a problem with the database.
                          Have you the opportunity to test the application against another database?

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            geytjie
                            wrote on last edited by
                            #13

                            Remember I told you I kept on seeing the old test data? Well, I physically deleted the debug and release folders and reran the build process. And now this error came out:

                            The test code I added:
                            @model->select();
                            qDebug() << "Found rows " << model->rowCount();
                            qDebug() << "Database error " << model->database()->lastError()->text();
                            @

                            The error code:
                            false
                            2 "no such table: wines Unable to execute statement"

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              lgeyer
                              wrote on last edited by
                              #14

                              Well, are you sure you are working on the correct database then? If there is no wyne.sqlite database file in the working directory the SQLITE driver will just create one.

                              Use an absulte path for QSqlDatabase::setDatabaseName() to make absoutely sure you are working on the correct database (one which contains your data).

                              1 Reply Last reply
                              0
                              • F Offline
                                F Offline
                                fluca1978
                                wrote on last edited by
                                #15

                                Maybe you are not connecting interactively to the right database, or you have more than one in your machine and are doing testing against two different databases....at least now we know that the application is behaving correctly and the database too. The next step is to find out which database is behaving instead of which!

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  geytjie
                                  wrote on last edited by
                                  #16

                                  I tried changing the db name, but I'm working on a mac and not all the drivers/plug-ins seem to be installed. Have been trying to figure this one out. Also, where can I find a plug-in for Qt 474 so I can create apps for Symbian 3 Nokia phones?

                                  1 Reply Last reply
                                  0
                                  • L Offline
                                    L Offline
                                    lgeyer
                                    wrote on last edited by
                                    #17

                                    Just install the Symbian^3 toolchain using the Qt SDK maintenance utility.

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved