简体   繁体   English

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

[英]How can I call a QDir lib in C++?

I download qt in my computer and called QDir as #include<QDir> . 我在计算机上下载了qt,并将其称为QDir作为#include<QDir> But it come up with error fatal error: QDir: No such file or directory . 但是它会带来fatal error: QDir: No such file or directory Is there anyway to use QDir without creating a .pro file? 有没有使用QDir而不创建.pro文件吗?

I tried to create a .pro file: 我试图创建一个.pro文件:

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

Minimal .pro file which builds your src.cpp ,assuming you also have main function there: 最小的.pro文件可以构建src.cpp ,假设您在其中也具有main功能:

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. 请使用Qt Creator新项目向导为您创建.pro文件(或CMake的cmakelist.txt),或使用众所周知的示例/模板开始,这样您就可以正确使用。 You do not want to use a complex framework like Qt without a makefile generator! 您不想在没有makefile生成器的情况下使用像Qt这样的复杂框架! But if you really must, use qmake (or cmake) to generate the makefile once, then remove the .pro file and keep editing the makefile. 但是,如果确实需要,请使用qmake(或cmake)生成一次makefile,然后删除.pro文件并继续编辑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: 使用QDir做一些事情的完整工作示例:

.pro file: .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

Example 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)

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM