QT学习过程中常用语句

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

本条博客用于记录本人学习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.csdn.net/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));

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值