QFileDialog has not been declared



  • Hello,

    I'm using Qt 8.4.8 compiled form sources on a Debian linux installation (Linux version 2.6.32-5-amd64 (Debian 2.6.32-46) (dannf@debian.org) (gcc version 4.3.5 (Debian 4.3.5-4)
    ))

    In QtCreator (version 2.6.1) I'm trying to use the QFileDialog class.

    Here is the code for main.cpp

    @#include "compkit.h"
    #include <QApplication>
    #include <QFileDialog>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    CompKit w;
    w.show();

    return a.exec();
    }@

    And here is the class code :

    @#include "compkit.h"
    #include "ui_compkit.h"

    CompKit::CompKit(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::CompKit)
    {
    ui->setupUi(this);
    }

    {
    delete ui;
    }

    void CompKit::on_actionOpen_file_triggered()
    {
    QString fileName;
    fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "/home/faugra", tr("Image Files (.)"));
    }@

    The build phase return this error:

    /home/faugra/work/c++/compilatori/ui/compkit/compkit/compkit.cpp:19: error: 'QFileDialog' has not been declared

    I've tried to comment out the #ifndef QT_NO_FILEDIALOG #endif in /usr/local/Trolltech/Qt-4.8.4/QtGui but nothing changed.
    Googling for the issue didn't help. The only information I got was that, in order to reduce library size, the QfileDialog class might have been excluded form the build. I do not know how to check if this is the reason that prevents me to use a QFileDialog object in my project.

    I'm new to Qt, I really like it, but this is a very strange behaviour...

    Thank you in advance for your help!

    f.



  • Did you add #include <QFileDialog> in the source code file (.cpp) where you use it ???

    I guess your "class code" is in a separate source code file, but I cannot see the required include there. Including it in your "main.cpp" doesn't help here, because C++ compiles every source code file separately and independently.

    You could put the include statement for QFileDialog into "compkit.h", but it's better to have it in "compkit.cpp" only. If required, you could put a forward declaration into "compkit.h".



  • Hello MuldeR

    Now it works, I've made a stupid mistake. Thank you very much!

    f.



  • Wow, you are using Qt8? We are still waiting for 5 to mature ;)

    Rule of thumb, only include what you need where you need it. And even then, you may face circular dependencies, which happens when two headers include each other, in which case you must do a forward declaration of your class in one of the headers, and include the actual header of the second class in the cpp of the first class. Also, if you have included something in the header, you don't need to include it again in the cpp, since it includes the header and all its includes. If you only need something in the cpp, don't include it in the header, the visibility is one way, the header is visible in the cpp, but the cpp is not visible in the header.

    All of this is one of the reasons many people don't like C++, most other languages don't even have all this stuff to be considered.



  • Thank you utcenter. The Qt version is 4.8.4 of course! :-)

    f.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.