(原)Qt之HelloWorld
孔令春
posted @ 2009年9月21日 05:43
in 开发语言
with tags
Qt之HelloWorld
, 4825 阅读
怪了,好像学任何语言都是从Hello world的,呵呵,Qt也不例外。
代码如下:
/* Hello/hello.cpp */
#include <QApplication>
#include <QMainWindow>
#include <QCore/QTextCodec>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName(
"utf-8"
));
QMainWindow *window = new QMainWindow;
window->setWindowTitle("Hello Qt");
QLabel *label = new QLabel("<h2><font color =
red><i>Hello</i></font>"
"<b>World!</b></h2>");
label->setAlignment(Qt::AlignCenter); //居中对齐
window->setCentralWidget(label); //将Label设置为中间部件
window->adjustSize(); //调整窗口到合适大小
window->show();
return app.exec();
}
必須的东西:
#include <QApplication> int main(int argc,char *argv[]) { QApplication app(argc,argv); ... return app.exec(); }
编译:
qmake -project ---生成Qt的项目文件
qmake ---生成MakeFile文件
make ---执行MakeFile文件(调用g++编译)
注意:
生成的项目文件名,默认是与上一级文件夹同名的,所以建议你最好为你的程序单独建一个文件夹。