QtQuick 学习笔记(二)按钮组件

1. QPushButton

功能

按压按钮,用于接受用户点击事件,可显示设定的字符串提示信息,但需要父组件作为容器,多用以执行命令或触发事件

常用函数

  • QPushButton::QPushButton(const QString &text,QWidget *parent,const char *name = 0);
    构造一个名称为name,父对象为parent并且文本为text的按压按钮。
  • void QAbstractButton::setText(const QString &)
    设置按钮上显示的文本。
  • QString QAbstractButton::text()const
    返回按钮上显示的文本。
  • void QAbstractButton::pressed()[signal]
    当按下按钮时,发射信号。
  • void QAbstractButton::clicked()[signal]
    当单击按钮时,发射信号。
  • void QAbstractButton::released()[signal]
    当释放按钮时,发射信号。

2. QRadioButton

功能

单选,用于提供两个或多个互斥选项

常用函数

  • QRaidoButton::QRadioButton(const QString &text,QWidget *parent,const char *name = 0)
    构造一个名称为name、父对象为parent并且文本为text的单选按钮。
  • bool QRadioButton::isChecked()const
    返回是否选中单选按钮,选中时返回true,没有选中时返回false。
  • void QAbstractButton ::setText(const QString &)
    设置组件上显示的文本。
  • QString QAbstractButton ::text()const
    返回该按钮上显示的文本。
  • void QAbstractButton ::stateChanged(int state)[signal]
    当更改checked属性值时,将发射信号。
  • void QRadioButton::setChecked(bool check)[virtual slot]
    设置单选按钮是否被选中为checked。

效果与实例

在这里插入图片描述

widget.cpp
      
#include "widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    group = new QButtonGroup(this);
    appleradio = new QRadioButton("apple",this);
    appleradio->move(5, 5);
    bananaradio = new QRadioButton("banana",this);
    bananaradio->move(5, 25);
    pearradio = new QRadioButton("pear",this);
    pearradio->move(5,50);
    group->addButton(appleradio,0);
    group->addButton(bananaradio,1);
    group->addButton(pearradio,2);
    appleradio->setChecked(true);

    connect(appleradio,SIGNAL(clicked()),this,SLOT(onRadioClick()));
    connect(bananaradio,SIGNAL(clicked()),this,SLOT(onRadioClick()));
    connect(pearradio,SIGNAL(clicked()),this,SLOT(onRadioClick()));
}

Widget::~Widget()
{

}

void Widget::onRadioClick()
{
    switch (group->checkedId()) {
    case 0:
        qDebug() << "apple";
        break;
    case 1:
        qDebug() << "banana";
        break;
    case 2:
        qDebug() << "pear";
        break;
    }
}
widget.h
      
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QButtonGroup>
#include <QRadioButton>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

private:
    QButtonGroup *group;
    QRadioButton *appleradio;
    QRadioButton *bananaradio;
    QRadioButton *pearradio;
public slots:
    void onRadioClick();
};
#endif // WIDGET_H
main.cpp
      
#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QButtonGroup group;
    Widget w;
    w.show();
    return a.exec();
}

3. QCheckBox

功能

复选框,可提供多选多功能,具有以下状态:checked、unchecked和PartiallyChecked

常用函数

  • QCheckBox::QCheckBox(const QString &text,QWidget *parent,const char *name = 0)
    构造一个名称为name、父对象为parent并且文本为text的复选框。
  • bool QCheckBox::isChecked()const
    选中复选框,返回true,否则返回false。
  • void QAbstractButton ::setText(const QString &)
    设置组件上显示的文本。
  • QString QAbstractButton ::text()const
    返回组件上显示的文本。
  • void QAbstractButton ::stateChange(int state)[signal]
    当更改checked属性时,将发射这个信号。
  • void QCheckBox::setChecked(bool check)[slot]
    设置复选框是否选中,状态为check的值。

效果与实例

在这里插入图片描述

main.cpp
      
#include <QApplication>
#include <QWidget>
#include <QCheckBox>
#include <QVBoxLayout>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;
    QVBoxLayout *vlayout = new QVBoxLayout(&w);
    QCheckBox *check1 = new QCheckBox("check1", &w);
    QCheckBox *check2 = new QCheckBox("check2", &w);
    QCheckBox *check3 = new QCheckBox("check3", &w);
    vlayout->addWidget(check1);
    vlayout->addWidget(check2);
    vlayout->addWidget(check3);
    w.setLayout(vlayout);
    w.show();
    return a.exec();
}

4. QToolButton

功能

一种用于命令或者选项的可以快速访问的按钮,通常显示的是图标,而不是文本标签

常用函数

  • QToolButton::QToolButton(QWidget *parent,const char *name = 0)
    构造一个名字为name,父对象为parent的ToolButton。
  • QToolButton::QToolButton(const QIconset &iconSet,const QString &textLabel,const QString &grouptext,QObject *receiver,const char *slot,QToolBar *parent,const char *name = 0)
    构造一个名称为name,父对象为parent(必须为QToolBar)的工具按钮。工具按钮将显示iconSet,工具提示为textLabel,状态条信息为grouptext,同时会将工具按钮链接到receiver对象的槽函数。
  • QToolBButton::QToolButton(ArrowType type,QWidget *parent,const char *name = 0)
    把工具按钮构造成箭头按钮,type定义了箭头的方向,可用的值有LeftArrow、RightArrow、UpArrow、DownArrow。
  • void QToolButton::setAutoRaise(bool enable)
    根据参数enable值设置按钮是否可自动浮起。
  • void QToolButton::setIcon(const QIconSet &)
    设置显示在工具按钮上的图标。
  • void QToolButton::setOn(bool enable)[virtual slot]
    设置按钮是否为开,enable等于true则设置为开,否则设置为关。
  • void QToolButton::setTextLabel(const QString &)[slot]
    设置按钮的提示标签。
  • QString QToolButton::textLabel()const
    返回按钮的提示标签。
  • void setToolButtonStyle ( Qt::ToolButtonStyle style )
    设置ToolButton的样式,有下列样式:
    • Qt::ToolButtonIconOnly
      只显示图标
    • Qt::ToolButtonTextOnly
      只显示文字
    • Qt::ToolButtonTextBesideIcon
      文字显示在图标旁
    • Qt::ToolButtonTextUnderIcon
      文字显示在图标下
    • Qt::ToolButtonFollowStyle
      根据QStyle::StyleHint进行设置
  • void setPopupMode ( ToolButtonPopupMode mode )
    设置ToolButton的菜单弹出方式ToolButtonPopupMode,弹出方式如下:
    • QToolButton::DelayedPopup延迟弹出
    • QToolButton::MenuButtonPopup
      菜单弹出
    • QToolButton::InstantPopup
      点击立即弹出

效果与实例

在这里插入图片描述

main.cpp
      
#include <QApplication>
#include <QWidget>
#include <QToolButton>
#include <QMenu>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;
    QToolButton *toolbutton = new QToolButton(&w);
    toolbutton->setText("ToolButton");
    QMenu *menu = new QMenu();
    menu->addMenu("1");
    menu->addMenu("2");
    menu->addMenu("3");
    toolbutton->setToolButtonStyle(Qt::ToolButtonTextOnly);
    toolbutton->setPopupMode(QToolButton::InstantPopup);
    toolbutton->setMenu(menu);
    w.show();
    return a.exec();
}

5. QCommandLinkButton

功能

用于在互斥选项中选择其中一项。默认情况下,除描述文本外,还会携带一个箭头图标,用以打开另一个窗口或界面。

常用函数

  • QCommandLinkButton::QCommandLinkButton(QWidget *parent = 0)
    构造一个父对象为parent的命令链接按钮。
  • QCommandLinkButton::QCommandLinkButton(const QString &text,QWidget *parent = 0)
    构造一个父对象为parent、文本为text的命令链接按钮。
  • QCommandLinkButton::QCommandLinkButton(const QString &text,const QString &description,QWidget *parent = 0)
    构造一个父对象为parent、文本为text和描述文本为description的命令链接按钮。
  • void QButton::clicked( ) [signal]
    当单击该按钮时,发射信号。
  • void QButton::pressed( ) [signal]
    当按下该按钮时,发射这个信号。
  • void QButton::released( ) [signal]
    当释放该按钮时,发射这个信号。
  • void QButton::setText(const QString &)
    设置按钮上显示的文本。
  • QString QButton::text( ) cosnt
    返回按钮上显示的文本。

6. QDialogButtonBox

功能

快速地布置一组按钮,有水平和垂直样式

常用函数

  • QDialogButtonBox::QDialogButtonBox(QWidget *parent = 0)
    构造一个按钮盒,父对象为parent。
  • QDialogButtonBox::QDialogButtonBox(QT::Orientation orientation,QWidget *parent = 0)
    构造一个按钮盒,父对象为parent,排列方向为orientation,并且包含buttons。
  • QDialogButtonBox::QDialogButtonBox(StandardButton buttons,QT::Orientation orientation = QT::Horizontal,QWidget *parent = 0)
    构造一个按钮盒,父对象为parent,排列方向为orientation。
  • void QDialogButtonBox::accepted()[signal]
    当单击按钮盒里的定义为AcceptRole和YesRole的按钮时,发射信号。
  • void QDialogButtonBox::addButton(QAbstractButton *button,ButtonRole role)
    向按钮盒里添加按钮button,定义按钮button的角色为role,如果role是无效的,则不添加按钮,如果按钮已添加,移除并在次添加为新角色。
  • QPushButton *QDialogButtonBox::addButton(StandarButton button)
    向按钮盒中添加一个标准按钮button,并返回标准按钮。如果按钮无效,不添加,返回0.
  • QPushButton *QDialogButtonBox::addButton(const QString &text,ButtonRole role)
    创建一个按钮的文本为text,以指定角色添加到按钮盒,并返回相应的按钮,如果role是无效的,则不创建,返回0.
  • void QDialogButtonBox::clear()
    清空按钮盒里的所有按钮。
  • void QDialogButtonBox::clicked(QAbstractButton *button)[signal]
    当单击按钮盒里的按钮button时,发射这个信号。
  • void QDialogButtonBox::helpRequested()[signal]
    当单击按钮盒里的定义为HelpRole的按钮时,发射这个信号。
  • void QDialogButtonBox::rejected()[signal]
    当单击按钮盒里定义为RejectRole和NoRole的按钮时,发射这个信号。
  • void QDialogButtonBox::removeButton(QAbstractButton *button)
    移除按钮盒里的按钮Button,但是不删除,设置它的父母为0
  • void setStandardButtons ( StandardButtons buttons )
    设置按钮盒中的按钮,使用|设置多个按钮。
  • void setOrientation ( Qt::Orientation orientation )
    设置按钮盒的样式,分为垂直和水平样式
  • QPushButton * button ( StandardButton which ) const
    根据StandardButton返回按钮盒中的按钮

效果与实例

在这里插入图片描述

main.cpp
      
#include <QApplication>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QWidget>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;
    QDialogButtonBox *button = new QDialogButtonBox(&w);
    button->setStandardButtons(QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
    button->button(QDialogButtonBox::Apply)->setText("apply");
    w.show();

    return a.exec();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/553710.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

RHCE1

unit1.定时任务和延迟任务项目 1.在系统中设定延迟任务要求如下: 在系统中建立easylee用户&#xff0c;设定其密码为easylee 延迟任务由root用户建立 要求在5小时后备份系统中的用户信息文件到/backup中确保延迟任务是使用非交互模式建立 再使用chmod修改权限&#xff1a; 确保…

HarmonyOS真机调试页面运行卡顿/黑屏解决方法,亲测有效

项目场景&#xff1a; 提示&#xff1a;这里简述项目相关背景&#xff1a; 用mate40等发行时间相对较早但系统是HarmonyOS4.0的真机调试 问题描述 提示&#xff1a;这里描述项目中遇到的问题&#xff1a; 程序点击容易卡顿或黑屏 原因分析&#xff1a; CPU兼容问题导致屏幕…

Text2sql的一些技巧

最近看到了一篇关于text2sql的文章&#xff0c;以及一些论文。对使用模型做text2sql给了一些不错的建议。 参考文章&#xff1a;24年大模型潜力方向&#xff1a;大浪淘沙后的Text-to-SQL和Agent - 知乎 论文&#xff1a;https://arxiv.org/pdf/2403.09732.pdf 关于模型的建议 …

STM32H7定时器TIM1-TIM17中断、PWM实现

STM32H7定时器TIM1-TIM17中断、PWM实现 高级定时器硬件框图定时器模式时基输出PWM定时器输入捕获 TIM1-TIM17的中断配置TIM1-TIM17的PWM输出 STM32H7 支持的定时器有点多&#xff0c;要简单的区分下。STM32H7 支持 TIM1-TIM8&#xff0c;TIM12-TIM17 共14 个定时器&#xff0c;…

使用API有效率地管理Dynadot域名,锁定账户中的域名

关于Dynadot Dynadot是通过ICANN认证的域名注册商&#xff0c;自2002年成立以来&#xff0c;服务于全球108个国家和地区的客户&#xff0c;为数以万计的客户提供简洁&#xff0c;优惠&#xff0c;安全的域名注册以及管理服务。 Dynadot平台操作教程索引&#xff08;包括域名邮…

Zynq7000系列中的时钟管理

PS&#xff08;处理系统&#xff09;时钟子系统生成的所有时钟都源自三个可编程PLL&#xff08;锁相环&#xff09;中的一个&#xff1a;CPU、DDR和I/O。时钟子系统的主要组件如图25-1所示。 在正常工作期间&#xff0c;PLL被启用&#xff0c;并由PS_CLK时钟引脚驱动。在启用P…

6.6Python之集合的基本语法和特性

集合&#xff08;Set&#xff09;是Python中的一种无序、不重复的数据结构。集合是由一组元素组成的&#xff0c;这些元素必须是不可变数据类型&#xff0c;但在集合中每个元素都是唯一的&#xff0c;即集合中不存在重复的元素。 集合的基本语法&#xff1a; 1、元素值必须是…

24卫生高级职称报名时间汇总⏰报名全流程

⏰卫生高级职称&#xff08;网上报名&#xff09;时间汇总 ✔️陕西&#xff1a;4月23日-5月24日 ✔️上海&#xff1a;4月23日-5月24日 ✔️重庆&#xff1a;4月23日—5月24日 ✔️黑龙江&#xff1a;4月23日-5月24日 ✔️浙江&#xff1a;4月23日-5月24日 ✔️云南&#xff1…

面试自救指南:女生如何巧答私密问题

在面试过程中&#xff0c;女性应聘者可能会遇到一些私人问题&#xff0c;这些问题可能涉及婚姻、家庭、生育等方面。面对这些问题&#xff0c;如何回答才能既保持真实又不失礼节呢&#xff1f; 当遇到关于婚姻状况的问题时&#xff0c;您可以选择回答&#xff1a;“我目前的婚姻…

【Python深度学习系列】网格搜索神经网络超参数:权重初始化方法(案例+源码)

这是我的第262篇原创文章。 一、引言 在深度学习中&#xff0c;超参数是指在训练模型时需要手动设置的参数&#xff0c;它们通常不能通过训练数据自动学习得到。超参数的选择对于模型的性能至关重要&#xff0c;因此在进行深度学习实验时&#xff0c;超参数调优通常是一个重要的…

2024全新快递平台系统独立版小程序源码|带cps推广营销流量主+前端

本文来自&#xff1a;2024全新快递平台系统独立版小程序源码|带cps推广营销流量主前端 - 源码1688​​​​​ 应用介绍 快递代发快递代寄寄件小程序可以对接易达云洋一级总代快递小程序&#xff0c;接入云洋/易达物流接口&#xff0c;支持选择快递公司&#xff0c;三通一达&am…

【leetcode面试经典150题】57. 环形链表(C++)

【leetcode面试经典150题】专栏系列将为准备暑期实习生以及秋招的同学们提高在面试时的经典面试算法题的思路和想法。本专栏将以一题多解和精简算法思路为主&#xff0c;题解使用C语言。&#xff08;若有使用其他语言的同学也可了解题解思路&#xff0c;本质上语法内容一致&…

电动车违停智能监测摄像机

电动车的普及带来了便利&#xff0c;但也衍生了一些问题&#xff0c;其中最常见的之一就是电动车的违停。电动车的违停不仅会影响交通秩序&#xff0c;还可能对周围环境和行人安全造成影响。为了监测和管理电动车的违停情况&#xff0c;可以使用电动车违停智能监测摄像机。这种…

退市危机袭来,环保行业能否逆境崛起?|中联环保圈

近年来&#xff0c;环保行业风波持续不断&#xff0c;众多环保大公司风险频出。博天环境的退市危机令人感慨&#xff0c;深圳星源因涉嫌信息披露违法违规而被警告退市&#xff0c;更是引发业界震动。 最近三年&#xff0c;证监会办理的上市公司信息披露违法案件多达 397 件&…

Linux内核之virt_to_page实现与用法实例(五十)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

Python 使用 pip 安装 matplotlib 模块(精华版)

pip 安装 matplotlib 模块 1.使用pip安装matplotlib(五步实现):2.使用下载的matplotlib画图: 1.使用pip安装matplotlib(五步实现): 长话短说&#xff1a;本人下载 matplotlib 花了大概三个半小时屡屡碰壁&#xff0c;险些暴走。为了不让新来的小伙伴走我的弯路&#xff0c;特意…

IPAguard--iOS代码混淆工具(免费)

IPAguard是一款为iOS开发者设计的代码混淆工具&#xff0c;旨在为开发者提供方便制作和分析马甲包的解决方案。通过高效的匹配算法&#xff0c;IPAguard可以在保证代码混淆的同时&#xff0c;保证编译后的代码质量&#xff0c;减少了因混淆引起的bug&#xff0c;使得开发者能够…

写后端项目的分页查询时,解决分页不更新

写基于VueSpringBoot项目&#xff0c;实现分页查询功能时&#xff0c;改完代码后&#xff0c;发现页数不更新&#xff1a; 更改处如下&#xff1a; 显示如图&#xff1a; 发现页数没有变化&#xff0c;两条数据还是显示在同一页&#xff0c;而且每页都10条。且重启项目也没有更…

代码随想录算法训练营第一天 | 704. 二分查找 | 27. 移除元素

704. 二分查找 int search(int* nums, int numsSize, int target) {int left 0, right numsSize, mid;while (left < right) {mid left (right -left) / 2;if (nums[mid] < target) {left mid 1;} else if (nums[mid] > target) {right mid;} else {return mid…

民兵档案管理系统-退伍军人档案管理全流程追踪

民兵档案管理系统&#xff08;智档案DW-S403&#xff09;是依托互3D技术、云计算、大数据、RFID技术、数据库技术、AI、视频分析技术对RFID智能仓库进行统一管理、分析的信息化、智能化、规范化的系统。 RFID档案管理系统是以先进的RFID技术为基础&#xff0c;结合数据库技术、…
最新文章