Ziming Qi
Ziming Qi

Reputation: 1

How can I call a QDir lib in C++?

I download qt in my computer and called QDir as#include<QDir> . But it come up with error fatal error: QDir: No such file or directory . Is there anyway to use QDir without creating a .pro file?

I tried to create a .pro file:

Template += app 
QT += core
Source += src.cpp

But it does not work

#include <QDir>
src.cpp:1:16: fatal error: QDir: No such file or directory

Upvotes: 0

Views: 747

Answers (1)

hyde
hyde

Reputation: 62797

Minimal .pro file which builds your src.cpp,assuming you also have main function there:

SOURCES += src.cpp

Please use Qt Creator new project wizard to create the .pro file (or CMake's cmakelist.txt) for you, or use a known-good example/template to start from, so you get everything right. You do not want to use a complex framework like Qt without a makefile generator! But if you really must, use qmake (or cmake) to generate the makefile once, then remove the .pro file and keep editing the makefile. Just note that it probably won't work for anybody except you without a lot of extra work. So just don't go there.


Complete working example which does something with QDir:

.pro file:

# core and gui are defaults, remove gui
QT -= gui

# cmdline includes console for Win32, and removes app_bundle for Mac
CONFIG += cmdline

# there should be no reason to not use C++14 today
CONFIG += c++14

# enable more warnings for compiler, remember to fix them
CONFIG += warn_on

# this is nice to have
DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += main.cpp

Example main.cpp:

#include <QDir>
#include <QDebug> // or use std::cout etc

//#include <QCoreApplication>

int main(int argc, char *argv[])
{
    // for most Qt stuff, you need the application object created,
    // but this example works without
    //QCoreApplication app(argc, argv);

    for(auto name : QDir("/").entryList()) {
        qDebug() << name;
    }
    // return app.exec(); // don't start event loop (main has default return 0)

}

Upvotes: 2

Related Questions