如何在C ++中调用QDir库?

时间:2019-07-12 23:44:50

标签: c++ qt qdir

我在计算机上下载了qt,并将其命名为QDir #include<QDir>。 但是出现错误fatal error: QDir: No such file or directory。无论如何,可以在不创建.pro文件的情况下使用QDir?

我试图创建一个.pro文件:

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

但这不起作用

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

1 个答案:

答案 0 :(得分:2)

用于构建您的 src.cpp 的最小.pro文件,假设您在其中还具有main功能:

SOURCES += src.cpp

请使用Qt Creator新项目向导为您创建.pro文件(或CMake的cmakelist.txt),或使用众所周知的示例/模板开始,这样您就可以正确使用。您不想在没有makefile生成器的情况下使用像Qt这样的复杂框架!但是,如果确实需要,请使用qmake(或cmake)生成一次makefile,然后删除.pro文件并继续编辑makefile。请注意,如果您没有很多额外的工作,它可能对任何人都不起作用。所以不要去那里。


使用QDir进行操作的完整工作示例:

.pro文件:

# 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

示例 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)

}