Qt开发基本框架

Qt中常用的工具
assistant(Qt助手)
qmake(Qt构建器)
designer(Qt设计师)
uic(Qt转换器)
rcc(Qt资源编译器)
moc(Qt元对象编译器)
qtcreator(Qt创造器)

1、创建工程目录 mkdir Hello
注:每个Qt程序都要放在一个独立的工程目录下

2、进入工程目录创建并编写源代码 main.cpp
注:语法和C++基本一致,但使用Qt的类库不再是标准C++的库

3、执行“qmake -project” 构建工程,生成工程文件(Hello.pro)
注:需要添加构建选项,QT += widgets

4、执行“qmake” 创建Makefile,可以根据上一步的工程文件自动生成Makefile

5、执行“make”即可完成编译和链接

6、执行“./Hello”,默认名称和工程名字一致

#include<QDialog>//对话框
#include<QLabel>//标签
#include<QPushButton>//按钮
#include<QLineEdit>//行编辑控件
#include<QHBOXLayout>//水平布局器
#include<QDoubleValidator>//验证器
#include<QVBoxLayout>//垂直布局器
#include<QTime>//时间
#include<QDebug>//打印调试
#include<QFont>//字体样式
#include<QMessageBox>//信息框

创建窗口

QWidget parent;
QDialog parent;
QMainWindow parent;

设置位置和大小x y

parent.move(50,50);
parent.resize(320,240);
创建控件,停靠在父窗口上面
QLabel label("我是标签",&parent);
QPushButton button("我是按钮",&parent);
//设置label边框消息:凹陷面板
m_label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
//设置label文本对齐方式:水平/垂直居中
m_label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
//设置控件的字体大小
QFont font;
font.setPointSize(20);
m_label->setFont(font);
m_button->setFont(font);
new对象如果指定里父窗口指针,可以不写delete,在父窗口对象销毁时,它回自动被销毁。
QPushButton* button2=new QPushButton("我是按钮",&parent);
button2->move(170,100);
button2->resize(80,80);
parent.show();//父窗口显示,上面停靠的控件也将一起显示
delete button2;

通过QTextCodec实现编码转换

QTextCodec *codec=QTextCodec::codecForName("GBK");
QString string=codec->toUnicode("GBK编码的中文字符串");

信号和槽时Qt自行定义的一种通信机制,实现对象之间的数据交互。

//点击按钮关闭标签
QObject::connect(&button,SIGNAL(clicked(void)),&label,SLOT(close(void)));
//点击退出按钮关闭窗口
QObject::connect(&button2,SIGNAL(clicked(void)),&app,SLOT(closeAllWindows(void)));
//点击按钮关闭父窗口退出程序
QObject::connect(&button2,SIGNAL(clicked(void)),&parent,SLOT(close(void)));

创建水平滑块

QSlider slider(Qt::Horizontal,&parent);
slider.move(20,100);//设置位置
slider.setRange(0,200);//设置大小范围

创建选值框

QSpinBox spin(&parent);
spin.move(220,100);//设置位置
spin.setRange(0,200);	//设置大小范围

//滑块滑动让选值框数值随值变化

QObject::connect(&slider,SIGNAL(valueChanged(int)),&spin,SLOT(setValue(int)));

//选值框数值改变让滑动块随之滑动

QObject::connect(&spin,SIGNAL(valueChanged(int)),&slider,SLOT(setValue(int)));
class CalculatorDialog:public QDialog{
	Q_OBJECT	//moc,涉及到语法扩展添加
};
setWindowTitle("计算器");//设置窗口标题
QLineEdit* m_editx;//行编辑器x
行编辑器x,this即为当前父窗口指针
m_editx=new QLineEdit(this);
设置文本对齐:右对齐
m_editx->setAlignment(Qt::AlignRight);
设置数字验证器,只能输入数字形式内容
m_editx->setValidator(new QDoubleValidator(this));
text():获取输入文本(QString)
toDouble():QString转换为double,参数保存转换是否成功结果
m_editx->text().toDouble(&bXOK);
设置行编辑器文本
m_editz->setText(str);
//信号和槽函数连接
//左右操作数文本改变时,发送信号textChanged()
connect(m_editx,SIGNAL(textChanged(QString)),this,SLOT(enableButton(void)));
connect(m_edity,SIGNAL(textChanged(QString)),this,SLOT(enableButton(void)));
QLineEdit* m_editz=new QLineEdit(this);
m_editz->setReadOnly(true);//设置文本只读
QPushButton* m_button=new QPushButton("=",this);
m_button->setEnabled(false);//设置按钮禁用true | false

创建布局器:自动调用每个控件的大小和位置

QHBoxLayout* layout=new QHBoxLayout(this);
layout->addWidget(m_editx);
......

设置布局器

setLayout(layout);

创建垂直布局器

QVBoxLayout* layout=new QVBoxLayout(this);
layout->addWidget(m_label);
......

设置布局器

setLayout(layout);
//number():将double转换为QString
QString str=QString::number(res);
  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值