提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
QT学习过程中常用语句
- 前言
- 1.水平布局器QHBoxLayout的使用,垂直布局器QVBoxLayout类似。
- 2.新建类添加横向布局后,布局器中的内容显示不了。笔者参考了该博主的解决方案https://www.cnblogs.com/meime7/p/6491604.html
- 3.QLbel添加边框和Wideget添加边框
- 4.水平布局中添加竖直布局
- 5.按钮槽函数
- 5.调试打印
- 6.在水平布局中的控件resize之后,尺寸没有发生变化。可以设置控件最大最小尺寸,控制在布局管理器内的缩放尺寸。笔者参考了该博主的解决方案https://blog.youkuaiyun.com/qq_51150032/article/details/110847666
- 7.设置资源文件
- 8.在QLabel中放照片,且设置为自动缩放
前言
本条博客用于记录本人学习QT过程遇到的常用语句和困惑点。
1.水平布局器QHBoxLayout的使用,垂直布局器QVBoxLayout类似。
代码如下(示例):
QLabel *portText=new QLabel;
QLineEdit *portEdit=new QLineEdit;
QPushButton *startServer=new QPushButton;
portText->setText("端口");//设置QLabel文本
startServer->setText("启动服务器");//设置QPushButton文本
QHBoxLayout *hlayout = new QHBoxLayout;//水平布局
hlayout->addWidget(portText);//向布局器添加portText
hlayout->addWidget(portEdit);//向布局器添加portEdit
hlayout->addWidget(startServer);//向布局器添加startServer
2.新建类添加横向布局后,布局器中的内容显示不了。笔者参考了该博主的解决方案https://www.cnblogs.com/meime7/p/6491604.html
代码如下(示例):
QWidget *newWidget = new QWidget(this);//添加(1) 布局才有效
QLabel *portText=new QLabel;
QLineEdit *portEdit=new QLineEdit;
QPushButton *startServer=new QPushButton;
portText->setText("端口");//设置QLabel文本
startServer->setText("启动服务器");//设置QPushButton文本
QHBoxLayout *hlayout = new QHBoxLayout;//水平布局
hlayout->addWidget(portText);//向布局器添加portText
hlayout->addWidget(portEdit);//向布局器添加portEdit
hlayout->addWidget(startServer);//向布局器添加startServer
newWidget->setLayout(hlayout);//添加(2) 布局才有效
3.QLbel添加边框和Wideget添加边框
代码如下(示例):
QLabel *portText=new QLabel;//QLabel添加边框
portText->setText("端口");//设置QLabel文本
portText->setFrameShape(QFrame::Box);//设置边框
QWidget::resize(500,500);//重新调整QWidget尺寸,可避免后面的newWidget 显示不完全
QWidget *newWidget = new QWidget(this);
newWidget->setStyleSheet(QString::fromUtf8("border:3px solid blue"));//Wideget添加边框
4.水平布局中添加竖直布局
代码如下(示例):
QHBoxLayout *hlayout = new QHBoxLayout;//水平布局
QVBoxLayout *vlayout = new QVBoxLayout;//竖直布局
hlayout ->addItem(vlayout);
5.按钮槽函数
在.h文件声明按钮,声明槽函数void startServerClick();
private slots:
void startServerClick();
private:
QPushButton *startServer;
选中函数名鼠标右击点Refactor选Add Definition in 选项
在.cpp文件构造函数做槽函数连接代码如下(示例):
connect(startServer, SIGNAL(clicked()), this,SLOT(startServerClick()));
5.调试打印
调试打印代码如下(示例):
#include<QDebug>
qDebug("startServerClick");
qDebug()<<"ip"<<ip;
6.在水平布局中的控件resize之后,尺寸没有发生变化。可以设置控件最大最小尺寸,控制在布局管理器内的缩放尺寸。笔者参考了该博主的解决方案https://blog.youkuaiyun.com/qq_51150032/article/details/110847666
代码如下(示例):
QHBoxLayout *hlayoutStatue = new QHBoxLayout;//水平布局
QLabel *statue=new QLabel;//控件设置最小尺寸
statue->setMaximumSize(40,40);//控件设置最大尺寸
7.设置资源文件
将照片放到项目文件夹中
在此处选择照片即可
8.在QLabel中放照片,且设置为自动缩放
代码如下(示例):
statue=new QLabel;
QImage *imgDis = new QImage;
imgDis->load(":/disconnect.png");//资源文件地址
statue->setScaledContents(true);//自动缩放
statue->setPixmap(QPixmap::fromImage(*imgDis));