c++ - 在 WIndows 中从命令提示符运行 QT 应用程序

标签 c++ qt makefile qtgui qmainwindow

我制作了一个小型 QT 应用程序,我试图在 Windows 上通过命令提示符运行它:

#include <QMainWindow>
#include <QLabel>

int main(int argc,char* argv[])
{
    QMainWindow a(argc,argv)
    QLabel *NewLabel = new QLabel("Hi i am a label");
    NewLabel->show();
    return a.exec();
}

在完成 qmake -project 之后 然后 qmake -TestPrg.pro 然后我尝试 make,这里失败并出现以下错误:

D:\TestPrg>make
make -f Makefile.Debug
make[1]: Entering directory `D:/TestPrg'
Makefile.Debug:58: *** missing separator.  Stop.
make[1]: Leaving directory `D:/TestPrg'
make: *** [debug] Error 2

如果我们查看 makefile.debug,第 58 行并在“<<”之前添加一个制表符,它会提示其他行号。所以我觉得编译器选项有问题,有人可以指导如何使其工作。

非常感谢

最佳答案

我刚刚在我的机器上做了一个例子。代码在下面,但你至少有一些错误,即:

  • 您使用 QMainWindow 作为应用程序,因为它看起来与 QApplication 相反。那不会编译。

  • 相应地,您需要包含 QApplication 而不是 QMainWindow。

  • 在 main 函数的第一个语句之后漏掉了一个分号。

  • 您不必要地在堆上构造了一个QLabel。在这个特定场景中,它可以是一个简单的堆栈对象。

  • 您使用 qmake -foo 调用 qmake 而不仅仅是 qmakemake foo

  • 您正在尝试在 Windows 命令提示符中使用“make”而不是 nmakejom。如果你用Visual Studio和MSVC,不要和mingw、cygwin之类的混用。只需使用 nmake,否则,是的,为后面的选项使用 make。

main.cpp

#include <QApplication>
#include <QLabel>

int main(int argc, char **argv)
{
    QApplication a(argc, argv);
    QLabel NewLabel("Hi i am a label");
    NewLabel.show();
    return a.exec();
}

主程序

TEMPLATE = app
TARGET = main
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += main.cpp

构建并运行

* qmake
* nmake
* main.exe

关于c++ - 在 WIndows 中从命令提示符运行 QT 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21041690/

相关文章:

c++ - istream_iterator 和 ifstream

c++ - 使用在全局范围内声明的 Qt 本地化字符串

c++ - QTreeWidget通过一个函数传递多个项目(多个选择)

c++ - QT MVC 模式不更新 View - 特定的 SimpleTreeModel 示例

c++ - 如何覆盖自定义委托(delegate)类的 'paint'函数来绘制QSpinBox

gcc - 安卓 NDK : how to let gcc to use additional include directories

c++ - 如何使用makefile从C中的另一个目录包含.a静态库和.h文件?

c - Makefile 不创建最终的可执行文件

c++ - 如何以线程安全的方式初始化 C++ 全局互斥量

c++ - 从另一个 C++ 订购数组