QT第一天学习

sudo apt-get install libqt4-dev
回顾:
面向对象方法:
封装、继承、多态
封装:类
数据和操作
实现了信息隐藏
public: 类的内部 类的外部
private:
protected:


class Test
{
public:
Test();
void show();
static void showB();
private:
int a;
static int b;
};
int Test::b = 0;//静态成员变量的初始化
void Test::show()
{
cout << a << endl;
}
构造函数:
拷贝构造函数(const 类的引用);
=
析构函数
静态成员函数
Test::showB();

友元函数
friend

运算符重载
返回类型 operator运算符(参数);

继承:
实现代码重用
继承方式:
public:公用继承

private:
protected:

多态:
静态多态性:
动态多态性:虚函数 virtual

--------------------------------------------------
QT :
官网:www.qt.io
商业版 开源版
1.简介:
Qt 是一个1991年由奇趣科技开发的跨平台C++图形用户界面应用程序开发框架。

不限于GUI
文件,网络,数据,图形处理...

qmake //构建器
uic
moc
qt creator
qt designer
assitant QT助手

1.第一个QT程序
#include<QApplication>//QApplication:用于管理应用程序
#include<QWidget>//widget 表示窗体

int main(int argc,char *argv[])
{
//创建一个app对象,管理应用程序
QApplication app(argc,argv);
//创建一个窗体w
QWidget w;
//把窗体显示出来
w.show();
//w.setVisiable(true);
//exec()让应用程序进入事件循环,等待用户操作
return app.exec();
//app.exec();return 0;
}
--------------
qmake -project //生成*.pro文件(工程文件)
qmake //根据*.pro文件生成 Makefile
make //生成可执行程序
./hello

注:如果qmake的版本>=5
那么 在*.pro文件中添加如下代码
QT += widgets

2.qt的版本为4.x.x的乱码处理
QT提供翻译机制:QTextCodec
1.创建一可以理解utf-8格式的解码器:
QTextCodec *codec = QTextCodec::codecForName("utf-8");

2.把解码器交给专司翻译的翻译器
QTextCodec::setCodecForTr(codec);

3.使用中文
w.setWindowTitle(QObject::tr("第一个QT程序"))

QWidget:
标签 按钮 单行文本框 下拉框
QLabel
QPushButton
QLineEdit
QComboBox

3.父窗体与子窗体
当父窗体显示出来的时候,它会负责把它所有的子窗体一起显示出来

当父窗体被销毁时,它会先把它所有的子窗体一起销毁掉


4.常用快捷键
对齐:ctrl+i

5.帮助文档的使用
版本
模块
概要说明
头文件
父子类
属性:不是成员变量,是访问成员变量的函数
公有函数
构造函数
析构函数
信号与槽
6.Linux下QtCreator中文输入法问题
输入命令:env | grep QT
结果:
QT_IM_MODULE=ibus
QT_QPA_PLATFORMTHEME=appmenu-qt5
QT4_IM_MODULE=xim

------------------
把以下命令写入配置文件.profile
export QT_IM_MODULE=ibus

重启一下使生效。

7.窗体的几何属性
位置move()
大小size()
resize()
setGeometry(int x, int y, int w, int h)
setFixedSize()//设置固定大小


作业:
1.在程序运行时,窗口能够抖动。
2.设计一个类,在程序运行时,窗口从最小(100,100)变到最大(800,800),再从最大变回最小.

代码

myWidget.h 代码

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include<QPushButton>
#include<QLabel>
#include<QTime>

void sleep(unsigned int msec);

class myWidget:public QWidget
{
public:
myWidget();
~myWidget();
private:
QPushButton *btn;
QLabel *label;
};
#endif // MYWIDGET_H

 

myWidget.cpp代码

#include"myWidget.h"
#include<QDebug>
#include<QApplication>

myWidget::myWidget()
{
btn=new QPushButton;
btn->setText("按钮");
btn->setParent(this);
btn->sizeHint();

label=new QLabel("标签",this);
label->move(100,100);
btn->move((400-100)/2,(300-100)/2);
this->resize(400,300);
for(int i=0;i<10;i++)
{
this->move(30,30);
this->show();
sleep(300);
this->move(0,0);
this->show();
sleep(300);
}
this->move(30,30);
for(int i=0;i<10;i++)
{
this->resize(200,100);
this->show();
sleep(300);
this->resize(400,300);
this->show();
sleep(300);
}
}

myWidget::~myWidget()
{

}

void sleep(unsigned int msec)
{
QTime dieTime = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

 

hello.cpp代码

#include<QApplication>
#include"myWidget.h"

int main(int argc,char *argv[])
{
QApplication app(argc,argv);
myWidget w;
return app.exec();
}

posted @ 2016-07-18 20:22  立刻行动  阅读(305)  评论(0编辑  收藏  举报