How to create main.cpp when using Qt designer?
I am new to Qt, and I use QT 4.4.3 on Linux.
When I design a new Qt G开发者_运维技巧UI with Qt Designer, is there a way to make it create main.cpp automatically, or do I have to manually create a file and manually add it to the Makefile created by qmake?
The output of QT Designer is a GUI element, essentially a subclass of QWidget
. It will not generate a main
function for you (the entry point for the C runtime to locate). You'll have to write your own where you create a QApplication
, and then instantiate and show
the main window widget.
For example, when using Qt Creator to create a default GUI project with a Designer-based main window, the designer creates a main window class named MainWindow
, and Qt Creator also generates a main.cpp
with these contents:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Where the application (QApplication
) is defined, the main window is instantiated, and finally the event loop of the application is executed.
Update: The .pro
file (suitable for qmake
's consumption - read here about the syntax) generated by Qt Creator for such a basic project is:
QT += core gui
TARGET = test1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
You may use this as a basis for your own .pro
file, should you choose to do this manually instead of via Qt Creator.
As an addition to Eli's post, you don't add files manually to the makefiles generated by qmake. Instead, you add files to the .pro file (or a .pri file which is included by the .pro file) you run qmake on.
This link will show you how to add sources to a PRI file and how to include that file in your PRO file.
精彩评论