2011-04-01 328 views
3

我使用Qt creator 2.0.1,并进入这个行当:Qt创建和QLabel,为什么会出现错误?

#include <QLabel>

我得到以下错误:

QLabel: No such file or directory

这是为什么?而且,在这种情况下如何包含标签?

UPDATE

@maverik告诉我如何解决QLabel错误,但现在我得到这个错误:

enter image description here

我想运行的程序是:

#include <QtCore/QCoreApplication> 
#include <QtGui/QLabel> 

int main(int argc, char *argv[]) { 
QCoreApplication myapp(argc, argv); 
QLabel *label = new QLabel("Hello"); 
label->show(); 
return myapp.exec(); 
} 

任何想法?

谢谢。

回答

1

试试这个:

#include <QtGui/QLabel> 
+0

感谢您的回复。是的,我认为这消除了'QLabel'问题。但是,我收到新的错误。检查我的更新后的错误和我试图运行的代码。 – Simplicity 2011-04-01 09:27:32

+0

你以什么方式建立项目?你使用makefile还是qmake?这个问题似乎是在链接时。你应该链接Qt库,如qtgui和qtcore。最好的办法是使用qmake。 – maverik 2011-04-01 09:58:17

3

检查你的Qt项目 - 文件包含

QT += gui 
CONFIG += qt 

,并且不包含

QT -= gui 

我认为这会导致连接问题。另外,我认为

#include <QLabel> 

...应该是足够的,如果项目文件是正确的。

+0

这可能是Maverik的首选答案。 – 2011-04-01 11:12:32

+0

感谢您的回复。当我这样做时,我得到一个控制台窗口提到:'QWidget:无法在不使用GUI时创建QWidget',而第二行'此应用程序请求运行时终止.....',而' .exe文件因此停止工作。有任何想法吗? – Simplicity 2011-04-02 06:58:55

5

使用

QApplication

不是

QCoreApplication

从QCoreApplication文档:

The QCoreApplication class provides an event loop for console Qt applications. This class is used by non-GUI applications to provide their event loop. For non-GUI application that uses Qt, there should be exactly one QCoreApplication object. For GUI applications, see QApplication.

然后包括相关的头文件,它将编译就好了。 QCoreApplication适用于非Gui应用程序(控制台)。

+1

是的,也许原来的海报已经使用Qt Creator来生成默认的控制台应用程序,并在那里添加了QLabel(这就是项目文件为什么会有QT - = gui定义的原因)。在这种情况下,他应该使用“Qt Gui应用程序”选项从创建者生成默认GUI应用程序。 – deo 2011-04-01 11:50:34

+0

感谢您的回复。当我这样做,我得到'QApplication:没有这样的文件或目录' – Simplicity 2011-04-02 06:47:17

0

我想我找到了问题所在。

由于我使用的是Qt Creator,并且在创建新项目时,我选择的是Qt Console Application而不是Qt Gui Application

+0

这是@deo得到的,所以你应该真的接受这个答案(@ soulSurfer2010解释创建一个'QWidget'的后续问题)。 – Troubadour 2011-08-15 20:54:17

相关问题