当前位置:网站首页>常用控件及自定义控件
常用控件及自定义控件
2022-06-26 13:17:00 【風清掦】
QLabel
QLabel可以用来显示文本,图片和动画等。
显示文本(普通文本、HTML)
通过QLabel类的setText函数设置显示的内容:
void setText(const QString &)显示普通文本字符串
QLabel *label = new QLabel(this);
label->setText("hellow,world!");显示HTML格式的字符串
比如显示一个链接:
QLabel * label2 = new QLabel(this);
label2 ->setText("<h1><a href=\"https://www.baidu.com\">百度一下</a></h1>");
label2 ->setOpenExternalLinks(true);其中setOpenExternalLinks()函数是用来设置用户点击链接之后是否自动打开链接,如果参数指定为true则会自动打开。
如图所示:

显示图片
可以使用QLabel的成员函数setPixmap()设置图片:
void setPixmap(const QPixmap &)
实现如下:
//定义QPixmap对象
QPixmap pixmap;
//加载图片
pixmap.load(":/QtSourcePicture/c_c.png");
//将图片设置到QLabel中,并显示图片
QLabel *label3 = new QLabel(this);
label3->setPixmap(pixmap);如图所示:

显示动画
可以使用QLabel 的成员函数setMovie()加载动画,可以播放gif格式的文件
void setMovie(QMovie * movie)实现如下:
//定义QMovied对象,并初始化:
QMovie * movie = new QMovie(":/new/Image/mario.gif");
//将动图设置到QLabel中:
QLabel *label4 = new QLabel(this);
label4->setMovie(movie);
//播放加载的动画:
movie->start();如图所示:

QLineEdit
QLineEdit提供单行文本编辑框。
设置/获取内容
获取编辑框内容使用text(),函数声明如下:
QString text() const设置编辑框内容:
void setText(const QString &)设置显示模式
使用QLineEdit类的setEchoMode () 函数设置文本的显示模式,函数声明:
void setEchoMode(EchoMode mode)EchoMode是一个枚举类型,共定义了四种显示模式:
- QLineEdit::Normal 模式显示方式,按照输入的内容显示。
- QLineEdit::NoEcho 不显示任何内容,此模式下无法看到用户的输入。
- QLineEdit::Password 密码模式,输入的字符会根据平台转换为特殊字符。
- QLineEdit::PasswordEchoOnEdit 编辑时显示字符否则显示字符作为密码。
若使用QLineEdit显示文本的时候,希望在左侧留出一段空白的区域,可使用QLineEdit类的setTextMargins()函数:
void setTextMargins(int left, int top, int right, int bottom)用此函数可以指定显示的文本与输入框上下左右边界的间隔的像素数。
实例
显示模式为密码模式下输入"Hello,world!":
实现如下:
QLineEdit*ledit=new QLineEdit(this);
//获取编辑框
ledit->text();
//设置文本的显示模式为密码模式
ledit->setEchoMode(QLineEdit::Password);
//设置编辑框内容为"Hello,world!"
ledit->setText("Hello,world!");
//指定显示的文本与输入框上下左右边界的间隔
ledit->setTextMargins(10,10,10,10);如图所示:

自定义控件
在搭建Qt窗口界面的时候,在一个项目中很多窗口,或者是窗口中的某个模块会被经常性的重复使用。因此将这个窗口或者模块做成一个独立的窗口类,以备以后重复使用也是很合理的。
在使用Qt的ui文件搭建界面的时候,工具栏中只提供了标准的窗口控件,有时需要自定义控件,那么该如何实现呢?以一个例子为例说明:自定义如图所示控件:

使之能够实现拉动滑条与设置数值相互同步,并且能够实现点击下方两个按钮,实行对应的操作。需要从QWidget派生出一个类SmallWidget,实现了一个自定窗口,步骤如下:
新建一个widget工程,然后选择已建好的工程,右键新建文件:

点击widget,然后下一步:




在工具栏提供的标准的窗口控件中,点击添加spinBox和horizontalSlider,并设置SmallWidget窗口为水平布局,再调节窗口大小到合适的大小:

回到widget.ui文件,添加一个widget控件,选中并右键点击“提升为”,将之前新建的类的类名SmallWidget输入其中,勾选“全局包含”,再点击“添加”和“提升”。

运行一下,就能看到如图所示的自定义控件

接下来,设置连接,信号和槽,在SamllWideget.h文件中添加setNum()和getNum()声明:
//设置数字
void setNum(int num);
//获取数值
int getNum();
widget.ui文件中添加两个按钮,并分别命名为btn_get和btn_set

SamllWideget.cpp中 添加添加setNum()和getNum()的定义和实现QSPinBox与QSlider值的同步变化:
#include "samllwideget.h"
#include "ui_samllwideget.h"
SamllWideget::SamllWideget(QWidget *parent) :
QWidget(parent),
ui(new Ui::SamllWideget)
{
ui->setupUi(this);
//QSpinBox移动 QSlider跟着动
void(QSpinBox::*SpSingnal)(int)=&QSpinBox::valueChanged;
connect(ui->spinBox,SpSingnal,ui->horizontalSlider,&QSlider::setValue);
//QSlider动 QSpinBox跟着移动
connect(ui->horizontalSlider,&QSlider::valueChanged,ui->spinBox,&QSpinBox::setValue);
}
SamllWideget::~SamllWideget()
{
delete ui;
}
//设置数字
void SamllWideget::setNum(int num)
{
ui->spinBox->setValue(num);
}
//获取数值
int SamllWideget::getNum()
{
return ui->spinBox->value();
}
最终实现拉动滑条与设置数值相互同步,点击两个按钮都能够分别实现各自设定的功能:

源代码
SamllWideget.h
#ifndef SAMLLWIDEGET_H
#define SAMLLWIDEGET_H
#include <QWidget>
namespace Ui {
class SamllWideget;
}
class SamllWideget : public QWidget
{
Q_OBJECT
public:
explicit SamllWideget(QWidget *parent = nullptr);
~SamllWideget();
//设置数字
void setNum(int num);
//获取数值
int getNum();
private:
Ui::SamllWideget *ui;
};
#endif // SAMLLWIDEGET_H
Widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
SamllWideget.cpp
#include "samllwideget.h"
#include "ui_samllwideget.h"
SamllWideget::SamllWideget(QWidget *parent) :
QWidget(parent),
ui(new Ui::SamllWideget)
{
ui->setupUi(this);
//QSpinBox移动 QSlider跟着动
void(QSpinBox::*SpSingnal)(int)=&QSpinBox::valueChanged;
connect(ui->spinBox,SpSingnal,ui->horizontalSlider,&QSlider::setValue);
//QSlider动 QSpinBox跟着移动
connect(ui->horizontalSlider,&QSlider::valueChanged,ui->spinBox,&QSpinBox::setValue);
}
SamllWideget::~SamllWideget()
{
delete ui;
}
//设置数字
void SamllWideget::setNum(int num)
{
ui->spinBox->setValue(num);
}
//获取数值
int SamllWideget::getNum()
{
return ui->spinBox->value();
}
Widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//点击获取当前控件的值
connect(ui->btn_get,&QPushButton::clicked,[=](){
qDebug()<<ui->diy_widget->getNum();
});
//设置到一半
connect(ui->btn_set,&QPushButton::clicked,[=](){
ui->diy_widget->setNum(50);
});
}
Widget::~Widget()
{
delete ui;
}
边栏推荐
- Lucky numbers in the matrix
- Gurivat sprint Harbour Exchange listed: created “multiple first”, received 900 million yuan Investment from IDG capital
- Research on balloon problem
- In insect classes and objects
- RISC-V 芯片架构新规范
- Pointer
- Exercise set 1
- A must for programmers, an artifact utools that can improve your work efficiency n times
- Detailed sorting of HW blue team traceability process
- How to check if a text field is empty or not in swift
猜你喜欢

Variable declaration of typescript

永远不要使用Redis过期监听实现定时任务!

7.consul service registration and discovery

Wechat applet -picker component is repackaged and the disabled attribute is added -- below

Freefilesync folder comparison and synchronization software

网络远程访问的方式使用树莓派

ICML 2022 | LIMO: 一种快速生成靶向分子的新方法

Common operation and Principle Exploration of stream

C language | file operation and error prone points

ThreadLocal巨坑!内存泄露只是小儿科...
随机推荐
【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
Luogu p4513 xiaobaiguang Park
Free machine learning dataset website (6300+ dataset)
CVPR 2022文档图像分析与识别相关论文26篇汇集简介
9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》
Applicable and inapplicable scenarios of mongodb series
Lucky numbers in the matrix
C language | Consortium
Exercise set 1
Wechat applet -picker component is repackaged and the disabled attribute is added -- below
Linear basis
Guruiwat rushed to the Hong Kong stock exchange for listing: set "multiple firsts" and obtained an investment of 900million yuan from IDG capital
Linear basis count (k large XOR sum)
[cqoi2015] task query system
Pytorch based generation countermeasure Network Practice (7) -- using pytorch to build SGAN (semi supervised GaN) to generate handwritten digits and classify them
ICML 2022 | limo: a new method for rapid generation of targeted molecules
服务器创建虚拟环境跑代码
C language | file operation and error prone points
9 regulations and 6 prohibitions! The Ministry of education and the emergency management department jointly issued the nine provisions on fire safety management of off campus training institutions
Hands on data analysis unit 3 model building and evaluation