QT系列第6节 QT中常用控件

news2024/9/22 23:24:06

1. QComboBox

下拉列表

2. QPlainTextEdit

QPlainTextEdit的文字内容以QTextDocument类型存储,函数document返回这个文档
 对象的指针
 QTextDocument是内存中的文本对象,以文本块方式存储,每个段落以换行符结束。
 QTextDocument提供一些函数实现对文本内容的存取
 int blockCount():返回文本块个数
 QTextBlock finBlockByNumber(int) 读取一个文本块,下标从0开始

3. QToolBox

 增加QWidget作为子容器,QWidget再加入QToolButton

4. QListWidget

list容器,支持右键菜单

5. QToolButton

工具按钮,绑定action来工作

6. QTabWidget

tab页支持切换,里面可以放入其他Widget

7. QTreeWidget

树状面板,可以被拖动停靠在主窗口任何地方

8. QTableWidget

表格面板

9. QDockWidget

容器面板

10.代码举例

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QComboBox>
#include <QPlainTextEdit>
#include <QToolBox>
#include <QListWidget>
#include <QToolButton>
#include <QTreeWidget>
#include <QTabWidget>
#include <QTableWidget>
#include <QDockWidget>

/*
QComboBox:
下拉列表
*/

/*
 QPlainTextEdit的文字内容以QTextDocument类型存储,函数document返回这个文档
 对象的指针
 QTextDocument是内存中的文本对象,以文本块方式存储,每个段落以换行符结束。
 QTextDocument提供一些函数实现对文本内容的存取
 int blockCount():返回文本块个数
 QTextBlock finBlockByNumber(int) 读取一个文本块,下标从0开始
*/

/*
QToolBox: 增加QWidget作为子容器,QWidget再加入QToolButton
*/

/*
QListWidget: list容器,支持右键菜单
*/

/*
QToolButton: 工具按钮,绑定action来工作

*/

/*
QTabWidget
tab页支持切换,里面可以放入其他Widget;
*/

/*
QTreeWidget
树状面板,可以被拖动停靠在主窗口任何地方
*/

/*
QTableWidget
表格面板
*/

/*
QDockWidget
容器面板
*/

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_pushButton_clicked();

    void on_comboBox_currentIndexChanged(int index);

    void on_comboBoxCity_currentTextChanged(const QString &arg1);

    void on_pushButton_2_clicked();

    void on_actionListIni_triggered();

    void on_tabWidget_currentChanged(int index);

    void on_toolBox_currentChanged(int index);

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QTextBlock>
#include <QMessageBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QIcon icon(":/icon/tubiaoziyuan/20.png");
    ui->comboBox->clear();
    for (int i = 0; i < 10; i++) {
        ui->comboBox->addItem(icon, QString::asprintf("Item %d", i));
    }

    ui->comboBoxCity->clear();
    QIcon iconCity(":/icon/tubiaoziyuan/21.png");
    QMap<QString, int> mapCity;
    mapCity.insert("北京", 10);
    mapCity.insert("天津", 20);
    mapCity.insert("上海", 30);
    mapCity.insert("重庆", 40);
    foreach (auto key, mapCity.keys()) {
        ui->comboBoxCity->addItem(iconCity, key, mapCity.value(key));
    }

    ui->toolButton->setDefaultAction(ui->actionListIni);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
   QTextDocument *doc = ui->plainTextEdit->document();
   int blockNum = doc->blockCount();
   for (int i = 0; i < blockNum; i++) {
        qDebug() << doc->findBlockByNumber(i).text();
   }
}


void Widget::on_comboBox_currentIndexChanged(int index)
{
    QString strItem = ui->comboBox->itemText(index);
    ui->plainTextEdit->appendPlainText(strItem);
    //ui->plainTextEdit->appendPlainText(ui->comboBox->currentText());
}


void Widget::on_comboBoxCity_currentTextChanged(const QString &arg1)
{
    if (!arg1.isEmpty()) {
        ui->plainTextEdit->appendPlainText(arg1);
    }
}


void Widget::on_pushButton_2_clicked()
{

}


void Widget::on_actionListIni_triggered()
{
    QMessageBox::about(this, "123", "456");
}


void Widget::on_tabWidget_currentChanged(int index)
{
}


void Widget::on_toolBox_currentChanged(int index)
{
    ui->tabWidget->setCurrentIndex(index);
}

widget.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>719</width>
    <height>307</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QComboBox" name="comboBox">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>10</y>
     <width>201</width>
     <height>22</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">jhj
jkjk

</string>
   </property>
   <item>
    <property name="text">
     <string>北京</string>
    </property>
    <property name="icon">
     <iconset theme="address-book-new"/>
    </property>
   </item>
   <item>
    <property name="text">
     <string>上海</string>
    </property>
   </item>
  </widget>
  <widget class="QPlainTextEdit" name="plainTextEdit">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>80</y>
     <width>201</width>
     <height>191</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>50</x>
     <y>280</y>
     <width>75</width>
     <height>24</height>
    </rect>
   </property>
   <property name="text">
    <string>测试</string>
   </property>
  </widget>
  <widget class="QComboBox" name="comboBoxCity">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>40</y>
     <width>201</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton_2">
   <property name="geometry">
    <rect>
     <x>220</x>
     <y>280</y>
     <width>75</width>
     <height>24</height>
    </rect>
   </property>
   <property name="text">
    <string>PushButton</string>
   </property>
  </widget>
  <widget class="QSplitter" name="splitter">
   <property name="geometry">
    <rect>
     <x>220</x>
     <y>10</y>
     <width>491</width>
     <height>261</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <widget class="QToolBox" name="toolBox">
    <property name="currentIndex">
     <number>0</number>
    </property>
    <widget class="QWidget" name="page">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>0</y>
       <width>155</width>
       <height>171</height>
      </rect>
     </property>
     <attribute name="label">
      <string>QListWidget操作</string>
     </attribute>
     <widget class="QToolButton" name="toolButton">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>0</y>
        <width>121</width>
        <height>22</height>
       </rect>
      </property>
      <property name="text">
       <string>tBtnListIni</string>
      </property>
      <property name="toolButtonStyle">
       <enum>Qt::ToolButtonTextBesideIcon</enum>
      </property>
     </widget>
     <widget class="QToolButton" name="toolButton_2">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>30</y>
        <width>121</width>
        <height>22</height>
       </rect>
      </property>
      <property name="text">
       <string>tBtnListClear</string>
      </property>
     </widget>
     <widget class="QToolButton" name="toolButton_3">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>60</y>
        <width>121</width>
        <height>22</height>
       </rect>
      </property>
      <property name="text">
       <string>tBtnListInsert</string>
      </property>
     </widget>
     <widget class="QToolButton" name="toolButton_4">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>90</y>
        <width>121</width>
        <height>22</height>
       </rect>
      </property>
      <property name="text">
       <string>tBtnListAppend</string>
      </property>
     </widget>
     <widget class="QToolButton" name="toolButton_5">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>120</y>
        <width>121</width>
        <height>22</height>
       </rect>
      </property>
      <property name="text">
       <string>tBtnListDel</string>
      </property>
     </widget>
    </widget>
    <widget class="QWidget" name="page_2">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>0</y>
       <width>155</width>
       <height>171</height>
      </rect>
     </property>
     <attribute name="label">
      <string>QTreeWidget操作</string>
     </attribute>
    </widget>
    <widget class="QWidget" name="page_3">
     <attribute name="label">
      <string>QTableWidget操作</string>
     </attribute>
    </widget>
   </widget>
   <widget class="QTabWidget" name="tabWidget">
    <property name="currentIndex">
     <number>0</number>
    </property>
    <widget class="QWidget" name="tab">
     <attribute name="title">
      <string>QListWidget</string>
     </attribute>
     <widget class="QPushButton" name="pushButton_3">
      <property name="geometry">
       <rect>
        <x>20</x>
        <y>20</y>
        <width>75</width>
        <height>24</height>
       </rect>
      </property>
      <property name="text">
       <string>测试1</string>
      </property>
     </widget>
    </widget>
    <widget class="QWidget" name="">
     <attribute name="title">
      <string>QTreeWidget</string>
     </attribute>
     <widget class="QPushButton" name="pushButton_4">
      <property name="geometry">
       <rect>
        <x>20</x>
        <y>10</y>
        <width>75</width>
        <height>24</height>
       </rect>
      </property>
      <property name="text">
       <string>测试2</string>
      </property>
     </widget>
    </widget>
    <widget class="QWidget" name="tab_2">
     <attribute name="title">
      <string>QTableWidget</string>
     </attribute>
     <widget class="QPushButton" name="pushButton_5">
      <property name="geometry">
       <rect>
        <x>20</x>
        <y>10</y>
        <width>75</width>
        <height>24</height>
       </rect>
      </property>
      <property name="text">
       <string>测试3</string>
      </property>
     </widget>
    </widget>
   </widget>
  </widget>
  <action name="actionListIni">
   <property name="icon">
    <iconset resource="icon.qrc">
     <normaloff>:/icon/tubiaoziyuan/32.png</normaloff>:/icon/tubiaoziyuan/32.png</iconset>
   </property>
   <property name="text">
    <string>初始化列表</string>
   </property>
  </action>
 </widget>
 <resources>
  <include location="icon.qrc"/>
 </resources>
 <connections/>
</ui>

运行效果:

 

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

常用日期类

第一代日期类 Date类 Date&#xff1a;精确到毫秒&#xff0c;代表特定的瞬间 当需要获取当前时间时可以直接new一个Date对象&#xff0c;使用无参构造器 Date date new Date();日期转文本 但是直接使用的话&#xff0c;它是国外的时间格式&#xff1a;Sat Dec 17 17:02:3…

Apache IoTDB v1.0.0安装试用小结( linux环境)

1 简介 Apache IoTDB 是一个开源物联网时序数据库。 github地址&#xff1a;https://github.com/apache/iotdb 2 下载 下载地址&#xff1a;https://iotdb.apache.org/Download/ 下载&#xff0c;输入命令&#xff1a; wget https://archive.apache.org/dist/iotdb/1.0.…

【简单工厂模式-2】简单工厂模式的代码实现及使用场景

简单工厂模式并属于Gof 23个经典设计模式其中之一&#xff0c;只是通常会将它作为学习其他工厂设计模式的入门&#xff0c;而且在开发中&#xff0c;简单工厂模式也是比较常见的&#xff0c;设计思想比较简单。 1. 定义 简单工厂模式定义一个工厂类&#xff0c;提供获取对象的…

Makefile、.mk、.bp、Blueprint、Soong、kati工具链的关系

一、Android版本相应的发展演变过程: Android 7.0引入ninja和kati Android 8.0使用Android.bp来替换Android.mk&#xff0c;引入Soong Android 9.0强制使用Android.bp 二、生成.ninja工具链关系&#xff1a;Android.bp、Blueprint、Soong、kati&#xff1a;总之都是用来把Make…

数据库拆分4--使用sharding-jdbc来实现水平拆分

有三张表 user log order表&#xff0c;先将user log 和order垂直分库&#xff0c;然后将user表水平拆分 配置文件 spring.shardingsphere.enabledtruespring.shardingsphere.datasource.nameswim-user,wim-orderspring.shardingsphere.datasource.wim-user.typecom.alibaba.…

vue入门--2

1.计算属性和侦听器 计算属性 VS 方法 如果不使用计算属性&#xff0c;在 methods 里定义了一个方法&#xff0c;也可以实现相同的效果&#xff0c;甚至该方法还可以接受参数&#xff0c;使用起来 更灵活。 既然 methods 同样可以解决模板中复杂逻辑计算的问题&#xff0c;那么…

2266. 统计打字方案数-动态规划

2266. 统计打字方案数-动态规划 Alice 在给 Bob 用手机打字。数字到字母的 对应 如下图所示。 为了 打出 一个字母&#xff0c;Alice 需要 按 对应字母 i 次&#xff0c;i 是该字母在这个按键上所处的位置。 比方说&#xff0c;为了按出字母 s &#xff0c;Alice 需要按 7 …

语雀导出markdown的图片外链问题

本文节选自本人博客&#xff1a;https://www.blog.zeeland.cn/archives/rgoioiabeoi32 Introduction 本人因为经常使用语雀写博客&#xff0c;但是因为语雀转markdown的时候图片存在防外链行为&#xff0c;如果想要把转出的markdown发表在其他平台&#xff0c;就需要把md中所有…

计算机毕设Python+Vue邢台市公寓式月亮酒店管理系统(程序+LW+部署)

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

jsp+ssm计算机毕业设计大学生心理咨询网站【附源码】

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; JSPSSM mybatis Maven等等组成&#xff0c;B/S模式 Mave…

java计算机毕业设计springboot+vue地铁站自动售票系统-火车票售票系统

项目介绍 本系统是针对目前地铁站自动售票的实际需求,从实际工作出发,对过去的地铁站自动售票管理系统存在的问题进行分析,完善用户的使用体会。采用计算机系统来管理信息,取代人工管理模式,查询便利,信息准确率高,节省了开支,提高了工作的效率。 本系统结合计算机系统的结构…

Nacos系列——配置的创建与获取

Nacos系列——配置的创建与获取配置的创建与获取本文资源官方文档创建配置获取Nacos配置程序目录1.引入依赖完整pom2.设置yaml3.配置读取类NacosBasedProperties4.构建日志打印工具LoggerUtil5.构建Nacos基础服务类实现6.构建自动化任务实现结果配置的创建与获取 本文资源 ht…

非零基础自学Golang 第11章 文件操作 11.3 处理JSON文件 11.3.1 编码JSON

非零基础自学Golang 文章目录非零基础自学Golang第11章 文件操作11.3 处理JSON文件11.3.1 编码JSON第11章 文件操作 11.3 处理JSON文件 JSON&#xff08;JavaScript Object Notation&#xff0c;JS对象简谱&#xff09;是一种轻量级的数据交换格式。 JSON最初是属于JavaScri…

C++基础学习笔记(二)——基础入门PART2

一、数组 一个集合中&#xff0c;里面存放了相同类型的数据元素 特点1&#xff1a;数组中的每个数据元素都是相同的数据类型 特点2&#xff1a;数组是由连续的内存位置组成的 1.1 一维数组 一维数组定义的三种方式&#xff1a; 数据类型 数组名[ 数组长度 ];数据类型 数组…

高通平台开发系列讲解(充电篇)充电底层驱动 power_supply 子系统

文章目录 一、Power Supply组成二、power_supply结构体说明三、驱动接口沉淀、分享、成长,让自己和他人都能有所收获!😄 📢 电池驱动采取的是 linux 内核驱动中的power_supply子系统框架进行上报电池状态。 一、Power Supply组成 power supply framework在kernel/driver…

微服务真的是万能解药吗?

程序员宝藏库&#xff1a;https://gitee.com/sharetech_lee/CS-Books-Store DevWeekly收集整理每周优质开发者内容&#xff0c;包括开源项目、资源工具、技术文章等方面。 每周五定期发布&#xff0c;同步更新到 知乎&#xff1a;Jackpop。 欢迎大家投稿&#xff0c;提交issu…

Spring MVC学习 | 拦截器异常处理器

文章目录一、拦截器1.1 简介1.2 拦截器的使用1.2.1 创建1.2.2 配置1.2.3 测试1.3 多个拦截器的执行顺序1.3.1 preHandle()方法返回true1.3.2 preHandle()方法返回false二、异常处理器2.1 简介2.2 配置2.2.1 springmvc.xml中配置2.2.2 注解配置学习视频&#x1f3a5;&#xff1a…

编码与解码总结

标准ASC||字符集&#xff1a; ASC||&#xff1a;美国信息交换标准代码&#xff0c;包括了英文、数字等标准ASC||使用一个字节存储一个字符&#xff0c;首位是0&#xff0c;总供可以表示128个字符 GBK&#xff08;汉字内码扩展规范&#xff0c;国标&#xff09; 汉字编码字符集…

Apache Shiro,这一篇就够了

Apache Shiro&#xff0c;这一篇就够了1.Shiro实现登录拦截2.登录认证操作3.Shiro整合Mybatis4.用户授权操作5.Shiro授权6.Shiro整合Thymeleaf1.Shiro实现登录拦截 前期环境准备 准备添加Shiro的内置过滤器&#xff1a; Bean public ShiroFilterFactoryBean shiroFilterFact…

《美国职业橄榄球大联盟》:NFL·橄榄1号位

基本装备 NFL橄榄球是一项过程极为激烈的比赛&#xff0c;阻挡、拦截与冲撞都是比赛不可或缺的一部分&#xff0c;这也可以说是橄榄球的一大特色。为了保护球员的安全&#xff0c;避免因为球员受伤而耽误球赛&#xff0c; NFL与NCAA都要求所有球员必须“穿戴合适且合法的护具”…