QT 入门 -QApplication QPushButton QDialog Ui类型的手工使用

QT

1.工具

              assistant  帮助文档

              qtconfig  QT配置工具

              qmake     QT的make与项目文件智能创建工具

              uic          UI界面的设计文件的编译工具

              moc        moc代码创建工具

              designer  UI设计工具

 

2.QT 的模块

              qtcore

              qtgui

              qtnetwork             

              qtsvg

              qtopengl

 

3.第一个QT程序

   1.QT编程模型

   2.QT程序编译过程

   3.QT的项目组织

 

1.      QT是C++程序

2.      QT程序启动QT的环境QApplication

 

 

代码:

#include<QApplication>

#include<QDialog>

int main(int args , char **argv)

{

QApplicationapp(args , argv);

QDialog dlg;

dlg.setVisible(true);

return  app.exec();//等待子线程结束

}

 

编译:1.qmake–project

生成一个.pro的文件(项目配置文件)

2.qmake xxx.pro

生成makefile文件

3.make

      4.执行

 

 

4.*pro文件

TEMPLATE=

           :app 表明为应用程序

           :lib 表明是库

SOURCES=

          :cpp文件1 cpp文件2 cpp文件3

          :\续行符号

HEADERS=

          :h头文件

CONFIG=   影响gcc的选项

        :release (-o)|debug(-g调试)

        :warn_on | warn_off  (-Wall   -w   警告)

        : qt  (表示qt应用,会自动加      qt库)  | opengl

        :shared| static动态,静态库,只有TEMPLATE指定为lib才管用

QT=(该变量在config=qt才有意义)

:core

:gui

:network

:opengl

:svg

:xml

:sql

 

TARGET=

       :输出的文件名(-o 输出文件名)

LIBS=用户指定库(用户自己的库)

FORMS=ui文件

 

程序2:

 

#include<QApplication>

#include<QWidget>

int main(int args,char **argv)

{

      QApplicationapp(args,argv);

      QWidget win;

      win.setVisable(true);

      returnapp.exec();

}

 

创建文件2:demo.pro

 

TEMPLATE=app

SOURCES=main.cpp

HEADERS=

CONFIG=qt release

QT=core gui

TARGET=main

 

直观认识QT

     封装与帮助文档的使用

     文档的组成部分:

1.      模块

2.类作用简单描述       

3.头文件

4.父类与派生类

5.构造器/析构器()

6.共有函数

7.属性

 

例子:QPushButton

 

#include<QApplication>

#include<QWidget>

#include<QPushButton>

 

int main(int  args,char  **argv)

{

    QApplication  app(args,argc);

    QWidget  win;

     win.resize(400,300);

    win.move( (1024-400)/2,(768-300)/2);   //设置窗体的大小和位置

     win.setVisible(true);

    QPushButton  btn(&win);  //pushbutton的构造函数参数为父窗体的指针

    btn.resize(100,30);        //设置button大小

     btn.move(10,10);

    btn.setText(“ok”);

     btn.setVisible(true);

    app.exec();

 

}

 

 

2.      乱码处理-上个程序的button中text,如果是中文会出现乱码

QT提供翻译机制

QTTextCodec类

 

例子:

 

 

#include<QApplication>

#include<QDialog>

#include<QPushButton>

#include<QTextCodec>

int main(int  args, char **argv)

{

     QApplication  app(args,argv);

     QTextCodec *codec=QTextCodec::codecForName(“gb2312”);

     //设置应用程序的编码:gb2312  codecForName静态成员函数

     QTextCodec::setCodecForTr(codec); //把设置的编码设置到环境中

     QDialog dlg;

     dlg.resize(400,300);

     dlg.move((1024-400)/2,(768-300)/2);

     QPushButton  btn(&dlg);

     btn.resize(100,30);

     btn.move(100,100);

     btn.setText(QObject::tr(“确定”));  //在使用中文的地方翻译

     btn.setVisible(true);

     dlg.setVisible(true);

     return  app.exec();

}

 

例子:QLineEditS

 

#include<QApplication>

#include<QDialog>

#include<QLineEdit>

 

int main(int args,char ** argv)

{

    QApplication  app(args,argv);

    QTextCodec *codec=QTextCodec::codecForName(“gb2312”);

    QTextCodec::setCodecForTr(codec);

     QDialog  dlg;

     dlg.resize(400,300);

    dlg.move((1024-400)/2 , (768-300)/2);

    QLineEdit edit;

     edit.resize(100,30);

    edit.move(100,100);

    edit.setVisible(true);

    dlg.setVisible(true);

    return app.exec();

}

 

 

3.代码组织

       以窗体为基本单位的封装

案例:

     登陆

      QDialog

      QPushButton

      QLabel

      QLineEdit

 

代码:

main.cpp

#include<QApplication>

#include<QTextCodec>

#include “logindlg.h”

int main(int  args,char **argv)

{

     QApplication  app(args , argv);

     QTextCodec *codec=QTextCodec::codecForName(“gb2312”);

     QTextCodec::setCodecForTr(codec);

     LoginDialog  dlg;

     dlg.resize(400,300);

     dlg.setVisible(true);

     return app.exec();

}

 

 

logindlg.h

#ifndef LOGIN_DLG_H

#define LOGIN_DLG_H

#include<QDialog>

#include<QLabel>

#include<QLineEdit>

#include<QPushButton>

class LoginDialog : public QDialog

{

 public:

     LoginDialog(QWidget * parent=NULL);

 private:

     QLabel *lbluser;

     QLabel *lblpass;

     QLineEdit *edtuser;

     QLineEdit *edtpass;

       QPushButton *btnlogin;

      QPushButton *btncancel;

};

#endif

 

Logindlg.cpp

 

#include “logindlg.h”

 

LoginDialog::LoginDialog(QWidget * parent):QDialog(parent)

{

      lbluser=new  QLablel(this);

      lblpass=new  QLabel(this);

 

      this->resize(400,300);

      this->move((1024-400)/2,(768-300)/2);

      lbluser->resize(100,30);

      lbluser->move(50,40);

      lblpass->resize(100,30);

      lblpass->move(50,100);

      lbluser->setText(QObject::tr(“用户:”));

      lblpass->setText(QObject::tr(“口令:”));

 

      edtuser=new  QLineEdit(this);

      edtpass=new  QLineEdit(this);

      edtuser->resize(200,30);

      edtuser->move(150,40);

      edtpass->resize(200,30);

      edtpass->move(150,100);

      

      btnlogin = new QPushButton(this);

      btncancel=new QPushButton(this);

      

      btnlogin->resize(60,30);

      btnlogin->move(90,210);

      btncancel->resize(60,30);

      btncancel->move(250,210);

      btnlogin->setText(Qobject::tr(“登录”));

      btncancel->setText(QObject::tr(“取消”));

 

}

 

 

3.      QT的界面设计器

      designer 可视化编辑器

3.1. 工具视图:

       

                                          用户设计区

                                          工具箱                                      

                                          资源管理器

                                          属性编辑器(ctrl+i)

                                          动作编辑器

                                          信号槽编辑器

                                          对象察看器(选择对象)

3.2. 保存文件

                  *.ui

     3.3.编辑属性

        1.文本属性

        2.对象名属性

        3.字体属性

     3.4. 编辑组件

            ->  <-方向

                      shift+方向  大小改变

                      ctrl+

                      shift+ctrl+

      3.5.打开ui文件

                            ui是xml文本文件

      3.6.使用ui文件

           自动使用

           手工使用

           uic编译ui文件 : 例如:uic login.ui –o login.h

                                          产生:c++头文件

Ui_对象名的类

                                           Ui::对象名的类 Ui命名空间

                        以上两种使用对象的方法

                  

                       类的构造器:没有(缺省构造器)

                       类的方法:setUi(QDialog *);

         3.7.使用UI类型

Main.cpp

 

#include<QApplication>

#include “dlglogin.h”

int main(int args, char ** argv)

{

       QApplication  app(args,argv);

       DlgLogin dlg;

        dlg.setVisible(true);

       return app.exec();

}

 

dlglogin.h

#ifndef MY_LOGINDLG_H

#define MY_LOGINDLG_H

#include<QDialog>

#include “login.h”

class DlgLogin : public QDialog

{

 private;

        Ui_LoginDialog *ui;

public:

     DlgLogin(QDialog * parent=NULL);

     ~DlgLogin();

 

}

#endif

 

dlglogin.cpp

#include “dlglogin.h”

 

DlgLogin::DlgLogin(QDialog *parent):QDialog(parent)

{

     ui = new Ui_LoginDialog;

     ui->setupUi(this);

}

 

DlgLogin::~DlgLogin()

{

    delectui;

}

 

 

 

 

posted on 2013-09-16 19:02  新一  阅读(855)  评论(0编辑  收藏  举报

导航