Important: Please read the Qt Code of Conduct -

QFileDialog file not found

  • Hi,

    I am relatively new to QT and have come back to coding after a small (10 year) hiatus.... things have definitely changed since then.

    I am writing code to try and access a sqlite database and have run into a few snags. I can create the database and access it (including add tables etc) but have an issue with trying to locate where the database is saved. According to all the information I can source it should save in the same director that the application runs as I have set the db to a filename without a directory path.

    In trying to source the location I came across some code to set the directory through use of QFileDialog::getOpenFileName(.....);

    Unfortunately I can't get the include statement #include <QFileDialog> to work. When I use this include statement I get a file not found error from the compiler. (also tried #include <QTGui> as I found reference that if this is included then I do not need to specifically include QFileDialog)

    See code below (apologies in advance for the messy coding and lack of comments)
    @#include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include "databasemanager.h"
    #include <QTextStream>
    #include <QFile>
    #include <iostream>
    #include <fstream>
    #include <sys/stat.h>
    #include <QDir>
    #include <QFileDialog>

    using namespace std;

    int main(int argc, char *argv[])
    char *filename = "testdb.sqlite";
    DeviceData *displayDevice = new DeviceData();
    QString tableNames;
    QByteArray tableNameArray;
    const char *table;

    QString homedir(QDir::home().path());
    printf("home path is %s\n",homedir.toLocal8Bit().constData());
    //get the database file with QFileDialog
    QString fileName1 = QFileDialog::getOpenFileName(this,
     tr("Open Database"), "/home/yourhome", tr("SQLite Database Files (*.sqlite)"));
    FILE *fp = fopen&#40;filename, "r"&#41;;
        printf("The file exists: %s\n",fp);
    else printf("file does not exist\n");
    DataBaseManager *db = new DataBaseManager();
    bool isopen = db->opendb(*filename);
    if(isopen)printf("data base is open\n");
    else printf("database not open\n");
    //bool tableCreated =db->createDeviceTable();
    //if(tableCreated)printf("device table created!\n");
      //      else printf("device table failed to create");
    QStringList tableList = db->getDbTables();
    for(int i =0 ; i< tableList.length();i++)
        printf("Table %i: %s\n",i,;
    QSqlError error = db->lasterror();
        QString new_error = error.text();
        QByteArray ba = new_error.toLocal8Bit();
        const char *c =;
    bool isclosed = db->closedb();
    if(isclosed)printf("database closed\n");
    else("database remains open!!\n");
    //QGuiApplication app(argc, argv);
    //QtQuick2ApplicationViewer viewer;
    //return app.exec();


    Any help would be appreciated!

  • Moderators


    I think you must write your code within
    @QGuiApplication app(argc, argv); @


    As QGuiApplication does all the initialization stuff.

  • QGuiApplication is not enough. QApplication must be used.

    and don't forget following line
    QT += widgets

  • Am I correct that all the other includes like:
    @#include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include "databasemanager.h"
    #include <QTextStream>
    #include <QFile>
    #include <iostream>
    #include <fstream>
    #include <sys/stat.h>
    #include <QDir>@

    do work correctly and only the last one:

    @#include <QFileDialog>

    triggers the warning?

    In my opinion, this looks like a Qt setup problem. Are you sure that all include paths are set up correctly? Another issue might be the Qt version. The include file name has changed in version 4.0, before that it was "qfiledialog.h".

  • Hi,
    Everything that is depending on the EventHandler will not work! There might be parts of the Qt libs that do work. So QTextStream will probably work, it's only reading and writing to files etc, so that might be without Events.
    The dialogs/windows or any other kind of Widget shall not function!! These depend on the QApplication to be running.
    It is possible to run a Qt application without the QApplication, but then in basic it's a stand alone C++ program.
    So remember there MUST always be a QApplication or a QGuiApplication/QtCoreApplication running in your main!!

  • Thanks guys,

    Makes sense regarding the QT Application or QApplications needing to be running. All other includes have worked without any issues so haven't really faced this problem until know.

    Probably need to reset to why I tried to use the QFileDialog in the first place. I still can't seem to find where QT is saving the sqlite database file when I create it. I can open the file, create tables, confirm that the tables are present, close the file and then re open it again at a later time and access the information. Yet I can't seem to find the file anywhere on my mac. IT's like it is being held in memory and not physically written to the hard drive??

    Is there any other way that I can get the file path for the database file to find it's location?

  • Out of interest the suggestion by 1+1=2 worked to access the correct libraries. Inserting

    QT += widgets

    This has allowed me to include the QFileDialog

Log in to reply