设置QT应用程序图标
- 1.将ico文件加入源文件目录
- 2.pro文件中加入下面模块
RC_FILE = myapp.rc
-3.源目录中添加myappico.rc代码
IDI_ICON1 ICON DISCARDABLE "myappico.ico"
以上是使用msvc编译的方法,如果使用mingw的qmake的话直接在pro加上这句就行
RC_ICONS = myappico.ico
以上是Windows程序设置图标的方法,其他的参考qt助手:SettingtheApplicationIcon
设置显示中文
#include <QTextCodec>
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QT程序中所有要显示到界面的字符串最好都使用tr()函数括起来,而对于不是要显示到界面的字符串中如果包含了中文,可以使用QString()进行编码转换
命令行编译
(在含有UI文件中需要使用uic工具)
uic XXXX.ui
1.源文件一定要设置utf-8编码,不然中文乱码
2.生成pro工程文件
qmake -project QT+=widgets
3.编译Makefile
qmake
4.make来编译
mingw32-make
设置窗口类型
QWidget::QWidget(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags())
parent指定父窗口,f指定窗口的类型,如FramelessWindowHint无边框,WindowStaysOnTopHint停留在所有窗口之上,SplashScreen欢迎窗口类型等等
QRect geometry=widget.geometry()
QRect frame=widget.frameGeometry()
QRect是一个矩形类型包含(x,y,宽,高),geometry返回没有边框的,frameGeometry返回有边框的
QT程序调试,包含头文件QDebug,常用第二种
qDebug("x:%d",x);
qDebug()<<"geometry:"<<geometry<<"frame:"<<frame;
QT程序调试,常用第二种
对话框
1.模态对话框:
QDialog dialog(this);
dialog.exec();
QDialog *dialog=new QDialog(this);
dialog->setModal(true);
dialog->show();
2.非模态对话框:
QDialog *dialog=new QDialog(this);
dialog->show();
模态对话框和非模态对话框的区别在于,模态对话框在没有关闭之前不能和其他的应用程序交互,而非模态对话框可以
与setModal(true)相似的还有setWindowModality(),他的参数设置对话框要阻塞的窗口的类型
在定义槽函数后可以单击槽函数按ALT+Enter在源文件中添加定义
信号和槽的关联分手动关联和自动关联,手动关联就是slot,connect,自动关联就是槽函数名为:"on发射信号的部件对象名信号名",就可以不用connect关联了
accept()隐藏模态对话框并且返回QDialog::Accepted
close()隐藏窗口,show()显示窗口
常见的几种标准的对话框,用各种对话框的静态方法创建比较方便
1.颜色对话框:QCloorDialog
2.文件对话框:QFileDialog
3.字体对话框:QFontDialog
4.输入对话框:QInputDialog
5.消息对话框:QMessageBox
6.进度对话框:QProgressDialog
7.错误信息对话框:QErrorMessage
8.向导对话框:QWizard
9.页面设置对话框(打印):QPageSetupDialog
8.打印对话框(打印:QPrintDialog
8.打印预览对话框:QPrintPreviewDialog
其他窗口部件
QFrame类是带有边框部件的基类
QAbstractScrollArea类是所有带有滚动区域的部件类的抽象基类
Qt中带有Abstract类的都是抽象类,不能直接使用。
以下类都是QFrame的子类,继承他的属性
1.QLabel类显示文本或图片
2.QLCDNumber类数码显示与液晶数字一样的效果
3.QStackedWidget可以有多个界面,每次只能显示一个界面
4.QToolBox类一列层叠窗口部件
QAbstractButton类是按钮部件的抽象基类
1.QPushButton类提供一个标准的按钮
2.QCheckBox复选框提供了同时选中多项的功能,QRadioButton只能选中一项,QGroupBox管理一组按钮
QLineEdit单行文本编辑器
1.显示模式,4种,echomode
2.输入掩码,限制输入内容,inputMask
3.输入验证,对输入进行约束,使用验证器validator
4.自动补全,使用QCompleter来完成
QAbstractSpinBox提供数值设定框和行编辑器来显示设定值
1.QDateTimeEdit,日期时间设定
2.QSpinBox,整数设定
3.QDoubleSpinBox,浮点数设定
滑块部件
QAbstractSlider提供了一个区间内的整数值,有一个滑块,可以定位到一个区间的任意值
1.QScrollBar实现滚动区域
2.QSlider是常见的音量控制或多媒体播放进度等滑块
3.QDial是一个刻度表盘