创建Qt 4 Console Application工程后,如果在工程中添加Widgets,则在编译运行时会出现一些错误,解决方法如下:
一、
编译时出现大量的“undefined reference to XX”,以及“collect2: ld returned 1 exit status”。
解决方法:
打开<project_name.pro>文件,删除如下几行:
QT -= gui
CONFIG += console
CONFIG -= app_bundle
console:包含该配置,将为程序创建一个控制台;
app_bundle:编写MAC OS下GUI专用,具体参考
<http://www.cppblog.com/biao/archive/2009/04/30/81602.aspx>
On the Mac, a GUI application must be built and run from a bundle. A bundle is a directory structure that appears as a single entity when viewed in the Finder. A bundle for an application typcially contains the executable and all the resources it needs.
The bundle provides many advantages to the user. One primary advantage is that, since it is a single entity, it allows for drag-and-drop installation. As a programmer you can access bundle information in your own code. This is specific to Mac OS X and beyond the scope of this document. More information about bundles is available on Apple's Developer Website.
A Qt command line application on Mac OS X works similar to a command line application on Unix and Windows. You probably don't want to run it in a bundle: Add this to your application's .pro:
CONFIG-=app_bundle
二、
运行时提示“QWidget: Cannot create a QWidget when no GUI is being used”,然后退出
解决方法:
将程序中的
#include <QtCore/QCoreApplication>
...
<QtCore/QCoreApplication>app(argc, argv);
替换为
#include <QtGui/QApplication>
...
QApplication app(argc, argv);