QT开发实例之常用控件(上)
创始人
2024-04-11 21:37:52
0

目录

  • QT控件使用范例
    • 设置窗口属性
    • 字体形状窗体
    • QPushButton 按钮
    • QLabel
    • QLineEdit 单行文本
    • QComboBox 下拉列表框
    • QFontComboBox 字体下拉列表框
    • QSpinBox 控件
    • QTimeEdit 时间控件
    • QDateEdit 日期控件
    • QScrollBar 滑动条控件
    • QRadioButton 单选按钮
    • QCheckBox 复选框

QT控件使用范例

设置窗口属性

为防止通过setWindowTitle 设置的窗口标题出现中文乱码的问题,需要将设置的参数进行一个转换,可以通过fromLocal8Bit 函数转换后就不会出现中文乱码的问题了。

learn::learn(QWidget *parent): QMainWindow(parent), bnt(nullptr)
{ui.setupUi(this);QString iconpath = "../Resource Files/WIN_20221114_17_56_03_Pro.jpg";//设置窗口标题setWindowTitle(QString::fromLocal8Bit("QT5.1窗口"));//设置窗口固定大小setMinimumSize(300, 300);setMaximumSize(300, 300);//设置窗口的背景颜色为红色this->setStyleSheet("background:red");//修改窗口的图标this->setWindowIcon(QIcon(iconpath));
}

字体形状窗体

将窗体背景色透明,根据图片形状显示窗体
在这里插入图片描述

void learn::SetLucency()
{//去掉标题栏this->setWindowFlags(Qt::FramelessWindowHint);//设置透明this->setAttribute(Qt::WA_TranslucentBackground, true);/*qss语句解释:background-image :背景图片url(xxx)   填写文件路径background-repeat:no-repeat;  不平铺*/this->setStyleSheet("background-image:url(C:/Users/26961/Desktop/1.png); background-repeat:no-repeat;");
}

QPushButton 按钮

在窗体中创建按钮 A,点击按钮 A,改变文字为按钮 B。

#pragma once#include 
#include "ui_learn.h"
#include 
#include 
#include 
#include 
#include   class learn : public QMainWindow
{Q_OBJECTpublic:learn(QWidget *parent = nullptr);~learn();// 按钮移动void MovePushButton();//鼠标点击void PressMouseEvent(QMouseEvent* e);//鼠标松开void ReleaseMouseEvent(QMouseEvent* e);//鼠标移动void MoveMouseEvent(QMouseEvent* e);//设置透明void SetLucency();//修改按钮void ChangeButtonText();
private:Ui::learnClass ui;QPushButton* bnt;QPoint last; //记录鼠标最近一次的坐标点
};void learn::ChangeButtonText()
{if (!bnt){delete bnt;bnt = nullptr;}bnt = new QPushButton(QString::fromLocal8Bit("修改文本"), this);//设置按钮的原点和宽高bnt->setGeometry(QRect(100, 100, 100, 25));//注册按钮处理事件connect(bnt, &QPushButton::clicked, this, [=]() {bnt->setText(QString::fromLocal8Bit("按钮已经修改"));});
}

QLabel

在窗体中创建 QLabel 标签显示“我是 QLabel”字样,红色加粗倾斜字体。

在这里插入图片描述

void learn::ChangeLabel()
{if (!label){delete label;label = nullptr;}//指定父对象, 设置Qlable文本this->label = new QLabel(QString::fromLocal8Bit("我是QLabel"), this);this->setGeometry(QRect(100, 100, 100, 25));/*font-size:20px  字体大小color:red       设置颜色font-weight:bold 字宽font-style:italic 字体样式*///设置Qlable qss样式label->setStyleSheet("font-size:20px; font-weight:bold; font-style:italic");
}

QLineEdit 单行文本

对输入的密码会进行一个隐藏设置

在这里插入图片描述

void learn::ChangeLineEdit()
{if (!line){delete line;line = nullptr;}this->line = new QLineEdit(this);//设置单行坐标和宽高line->setGeometry(QRect(100, 100, 300, 25));//设置单行文本样式line->setStyleSheet("font-size:20px; font-weight:bold; font-style:italic");//限制最大长度为12line->setMaxLength(12);//不可写设置//line->setEchoMode(QLineEdit::NoEcho);//对密码进行隐藏设置line->setEchoMode(QLineEdit::Password);
}

QComboBox 下拉列表框

在这里插入图片描述

void learn::AddCharActer()
{if (this->box) {delete box;box = nullptr;}box = new QComboBox(this);box->setGeometry(QRect(100, 100, 300, 25));QStringList str;str << QString::fromLocal8Bit("数学")<< QString::fromLocal8Bit("语文")<< QString::fromLocal8Bit("地理");box->addItems(str);
}

QFontComboBox 字体下拉列表框

在这里插入图片描述

void learn::AddFontComboBox() 
{//创建字体下拉列表fontbox = new QFontComboBox(this);fontbox->setGeometry(QRect(100, 125, 300, 25));bnt = new QPushButton(QString::fromLocal8Bit("选择"), this);bnt->setGeometry(QRect(100, 100, 300, 25));line = new QLineEdit(this);line->setGeometry(QRect(100, 150, 300, 25));connect(bnt, &QPushButton::clicked, this, [=](){//当选择按钮被点击时,自动更换line 的信息line->setText(fontbox->currentText());});
}

QSpinBox 控件

在这里插入图片描述

void learn::AddSpinBox() 
{spin = new QSpinBox(this);spin->setGeometry(QRect(100, 125, 300, 25));//设置取值范围spin->setRange(0,100);//设置初始值spin->setValue(0);//除了初始值这些可以被修改,除此之外所有的文本都不允许被修改//后缀spin->setSuffix(QString::fromLocal8Bit("元/斤"));//前缀spin->setPrefix(QString::fromLocal8Bit("苹果"));
}

QTimeEdit 时间控件

在这里插入图片描述

void learn::AddTimeEdit() 
{time = new QTimeEdit(this);time->setGeometry(QRect(100, 125, 300, 25));//获取系统时间QDateTime systime = QDateTime::currentDateTime();//获取时分秒以“:”号拆分赋予 list 数组QStringList list = systime.toString("h:m:s").split(":");//打印系统时间qDebug() << list;//将时分秒绑定控件time->setTime(QTime(list[0].toInt(), list[1].toInt()));
}

QDateEdit 日期控件

在这里插入图片描述

void learn::AddDateEdit()
{date = new QDateEdit(this);date->setGeometry(QRect(100, 150, 300, 25));//获取系统时间QDateTime time = QDateTime::currentDateTime();QStringList list = time.toString("yyyy-MM-dd").split("-");date->setDate(QDate(list[0].toInt(), list[1].toInt(), list[2].toInt()));
}

QScrollBar 滑动条控件

在这里插入图片描述

void learn::AddScrollBar() 
{this->setWindowFlags(Qt::FramelessWindowHint);this->ScrollBar = new QScrollBar(this);this->spin = new QSpinBox(this);//横显/竖显ScrollBar->setOrientation(Qt::Horizontal);//指定大小ScrollBar->setGeometry(QRect(50, 50, 180, 20));spin->setGeometry(QRect(50, 90, 100, 25));//控制条宽度ScrollBar->setPageStep(10);//随着scrollBar 按钮在不断的移动的过程中,spin控件会及时更新 scrollBar 的现值connect(ScrollBar, &QScrollBar::valueChanged, spin, [=](){spin->setValue(ScrollBar->value());});
}

QRadioButton 单选按钮

在这里插入图片描述

void learn::AddRadioButton() 
{label = new QLabel(this);label->setText(QString::fromLocal8Bit("选择内容:"));line = new QLineEdit(this);this->radioW = new QRadioButton(QString::fromLocal8Bit("女人") , this);this->radioM = new QRadioButton(QString::fromLocal8Bit("男人"), this);//设置默认选择radioWradioW->setChecked(true);//位置line->setGeometry(QRect(130, 100, 100, 25));radioM->setGeometry(QRect(50, 50, 50, 50));radioW->setGeometry(QRect(100, 50, 50, 50));label->setGeometry(QRect(50, 100, 100, 25));connect(radioW, &QPushButton::clicked, this, [=](){//当radioW 被勾选之后,label设置为radioWif (sender() == radioW) {this->line->setText(radioW->text());}});connect(radioM, &QPushButton::clicked, this, [=](){//当radioW 被勾选之后,label设置为radioWif (sender() == radioM) {this->line->setText(radioM->text());}});
}

QCheckBox 复选框

在这里插入图片描述

void learn::AddCheckBox() 
{checkbox1 = new QCheckBox(QString::fromLocal8Bit("语文"), this);checkbox2 = new QCheckBox(QString::fromLocal8Bit("数学"), this);checkbox3 = new QCheckBox(QString::fromLocal8Bit("英语"), this);checkbox1->setGeometry(QRect(50, 50, 50, 50));checkbox2->setGeometry(QRect(100, 50, 50, 50));checkbox3->setGeometry(QRect(150, 50, 50, 50));label = new QLabel(this);label->setText(QString::fromLocal8Bit("选择内容:"));line = new QLineEdit(this);label->setGeometry(QRect(50, 100, 100, 25));line->setGeometry(QRect(130, 100, 100, 25));bnt = new QPushButton(QString::fromLocal8Bit("提交信息"), this);auto process = [&]() {auto interval = QString::fromLocal8Bit("、");if (sender() == checkbox1) {line->insert(checkbox1->text() + interval);}if (sender() == checkbox2) {line->insert(checkbox2->text() + interval);}if (sender() == checkbox3) {line->insert(checkbox3->text() + interval);}};connect(checkbox1, &QCheckBox::clicked, this, process);connect(checkbox2, &QCheckBox::clicked, this, process);connect(checkbox3, &QCheckBox::clicked, this, process);
}

相关内容

热门资讯

银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...