GUI1_简历空Qt项目
添加头文件 QApplicationQDialogQLabelQTextCodec
声明QDialog类对象widger;声明QLable类对象label,并确定其父类为widget对象;
注意:此处使用QApplication,在建立控制台应用程序时使用QCoreApplication。
//Refer to Book_QtCreator quick start;chapter 2.3.1: helllo world; #include <QApplication> #include<QDialog> #include<QLabel> #include<QTextCodec> int main(int argc,char *argv[]) { QApplication a(argc,argv); //用于管理应用程序的资源,任何GUI都有一个QApplication 因为Qt需要接受命令行参数 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); QDialog widget; QLabel label(&widget); //QLabel label; label.setText(QObject::tr("hello world")); //label.show(); //setCodecForTr用来设置QObject中使用的字符集 widget.resize(400,300); label.move(120,120); widget.show(); return a.exec(); }
GUI2_建立Qt GUI Application模板
文件命名,选择Basic class_QMainWidget|QDialog|QWidget,有三种模板。我们选择QWidget,然后自动生成模板。
模板总共有四个文件:
widget_base.h,widget_base.cpp //GUI界面的类
widget_base.ui //GUI界面的设计文件
main.cpp
//widget_base.h 声明类
#ifndef WIDGET_BASE_H #define WIDGET_BASE_H #include <QWidget> #include <QLabel> namespace Ui {class Widget_base; } //确定命名空间 class Widget_base : public QWidget { Q_OBJECT public: explicit Widget_base(QWidget *parent = 0); ~Widget_base(); private: Ui::Widget_base *ui; //建立UI QLabel *label; //类中的私有对象,不同类之间的相互调用 }; #endif // WIDGET_BASE_H
//widget_base.cpp 定义类
#include "widget_base.h" #include "ui_widget_base.h" Widget_base::Widget_base(QWidget *parent) :QWidget(parent),ui(new Ui::Widget_base) //构造 { ui->setupUi(this); QLabel *label=new QLabel(this); //以UI界面为父类,建立QLable对象,并对其属性进行设置 label->setText(this->windowTitle()); label->show(); } Widget_base::~Widget_base() //析构 { delete ui; }
//mian.cpp
#include <QApplication> #include "widget_base.h" /*建立以widget为基类的 窗口部件类,在类widget_base中使用Qlabel并确定父窗口;*/ int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget_base w; w.setWindowTitle("Windows1"); w.show(); return a.exec(); }
Widget_base.pro文件
#------------------------------------------------- # # Project created by QtCreator 2016-01-30T12:51:55 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = Qt_widget TEMPLATE = app SOURCES += main.cpp widget_base.cpp HEADERS += widget_base.h FORMS += widget_base.ui