Qt控件(按钮、单选、复选、list、tree、table)

简介: Qt控件(按钮、单选、复选、list、tree、table)

一、布局

工具栏图标文字一起显示,背景透明

 二、代码

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>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>10</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>登录</string>
   </property>
  </widget>
  <widget class="QToolButton" name="toolButton">
   <property name="geometry">
    <rect>
     <x>100</x>
     <y>10</y>
     <width>91</width>
     <height>21</height>
    </rect>
   </property>
   <property name="text">
    <string>工具</string>
   </property>
   <property name="icon">
    <iconset resource="res.qrc">
     <normaloff>:/icon/demo01.png</normaloff>:/icon/demo01.png</iconset>
   </property>
   <property name="toolButtonStyle">
    <enum>Qt::ToolButtonTextBesideIcon</enum>
   </property>
   <property name="autoRaise">
    <bool>true</bool>
   </property>
  </widget>
  <widget class="QGroupBox" name="groupBox">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>50</y>
     <width>120</width>
     <height>80</height>
    </rect>
   </property>
   <property name="title">
    <string>性别</string>
   </property>
   <widget class="QRadioButton" name="rBtnMan">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>20</y>
      <width>95</width>
      <height>19</height>
     </rect>
    </property>
    <property name="text">
     <string>男</string>
    </property>
   </widget>
   <widget class="QRadioButton" name="rBtnWoman">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>50</y>
      <width>95</width>
      <height>19</height>
     </rect>
    </property>
    <property name="text">
     <string>女</string>
    </property>
   </widget>
  </widget>
  <widget class="QGroupBox" name="groupBox_2">
   <property name="geometry">
    <rect>
     <x>170</x>
     <y>50</y>
     <width>120</width>
     <height>54</height>
    </rect>
   </property>
   <property name="title">
    <string>婚否</string>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QRadioButton" name="radioButton_4">
      <property name="text">
       <string>未婚</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QRadioButton" name="radioButton_3">
      <property name="text">
       <string>已婚</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QGroupBox" name="groupBox_3">
   <property name="geometry">
    <rect>
     <x>320</x>
     <y>20</y>
     <width>100</width>
     <height>141</height>
    </rect>
   </property>
   <property name="title">
    <string>问卷调查</string>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QCheckBox" name="checkBox_4">
      <property name="text">
       <string>价格实惠</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QCheckBox" name="checkBox">
      <property name="text">
       <string>口感好</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QCheckBox" name="checkBox_3">
      <property name="text">
       <string>服务到位</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QCheckBox" name="cBox">
      <property name="text">
       <string>老板好</string>
      </property>
      <property name="tristate">
       <bool>true</bool>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QListWidget" name="listWidget">
   <property name="geometry">
    <rect>
     <x>480</x>
     <y>20</y>
     <width>256</width>
     <height>171</height>
    </rect>
   </property>
  </widget>
  <widget class="QTreeWidget" name="treeWidget">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>200</y>
     <width>256</width>
     <height>192</height>
    </rect>
   </property>
   <column>
    <property name="text">
     <string notr="true">1</string>
    </property>
   </column>
  </widget>
  <widget class="QTableWidget" name="tableWidget">
   <property name="geometry">
    <rect>
     <x>290</x>
     <y>200</y>
     <width>451</width>
     <height>192</height>
    </rect>
   </property>
  </widget>
 </widget>
 <resources>
  <include location="res.qrc"/>
 </resources>
 <connections/>
</ui>

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include<QRadioButton>
#include<QCheckBox>
 
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //设置单选按钮 男默认选中
    ui->rBtnMan->setCheckable(true);
    //选中女后 打印信息
    connect(ui->rBtnWoman,&QRadioButton::clicked,[=](){
        qDebug()<<"选中了女!";
    });
    //多选按钮
    connect(ui->cBox,&QCheckBox::stateChanged,[=](int state){
        qDebug()<<state;
    });
 
    //利用listWidget写诗
    QListWidgetItem * item=new QListWidgetItem("锄禾日当午");
    //将第一行放到listWidget控件中
    ui->listWidget->addItem(item);
    //设置居中
    item->setTextAlignment(Qt::AlignHCenter);
 
    //QStringList
    QStringList list;
    list<<"锄禾日当午"<<"汗滴禾下土"<<"谁知盘中餐"<<"粒粒皆辛苦?";
            ui->listWidget->addItems(list);
 
    //treeWidget树控件使用
 
    //设置水平头
    ui->treeWidget->setHeaderLabels(QStringList()<<"英雄"<<"英雄介绍");
 
    QTreeWidgetItem * liItem=new QTreeWidgetItem(QStringList()<<"力量");
    QTreeWidgetItem * minItem=new QTreeWidgetItem(QStringList()<<"敏捷");
    QTreeWidgetItem * zhiItem=new QTreeWidgetItem(QStringList()<<"智力");
    //追加顶层节点
    ui->treeWidget->addTopLevelItem(liItem);
    ui->treeWidget->addTopLevelItem(minItem);
    ui->treeWidget->addTopLevelItem(zhiItem);
 
    //追加自己点
    QStringList heroL1;
    heroL1<<"刚被猪"<<"刚被猪刚被猪";
    QTreeWidgetItem *ll=new QTreeWidgetItem(heroL1);
    liItem->addChild(ll);
 
    //TableWidget控件
    //设置列数
    ui->tableWidget->setColumnCount(3);
 
    //设置水平表头
    ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"姓名"<<"性别"<<"年龄");
 
    //设置行数
    ui->tableWidget->setRowCount(5);
    //设置正文
    QStringList nameList;
    nameList<<"亚瑟"<<"赵云"<<"张飞"<<"关羽"<<"花木兰";
    QStringList sexList;
    sexList<<"男"<<"男"<<"男"<<"男"<<"女";
 
        for(int i=0;i<5;i++){
        int col=0;
        ui->tableWidget->setItem(i,col++,new QTableWidgetItem(nameList[i]));
        ui->tableWidget->setItem(i,col++,new QTableWidgetItem(sexList.at(i)));
        ui->tableWidget->setItem(i,col++,new QTableWidgetItem(QString::number(i+18)));
    }
}
 
Widget::~Widget()
{
    delete ui;
}
 
目录
相关文章
|
4天前
Qt之按钮操作
Qt之按钮操作
8 2
|
27天前
|
BI
Power BI获取SharePoint List列表后,如何展开List/Table中的字段,以及使用逗号拼接为一个字符串
在Power BI中,从SharePoint List获取数据时遇到Table和List混合的数据源,直接展开会导致“笛卡尔积”效应,生成过多行。目标是保持行数不变,将Table中的字段与List值用逗号分隔显示在同一行。解决方法包括:1) 添加新列,从Table中提取List的Column2值;2) 使用Text.Combine函数合并List中的值。具体操作步骤包括选择列并自定义新列,然后展开List并以逗号分隔。通过这些步骤,可以将Table转换为所需的字符串格式。完整的Power BI Query代码展示了这一过程。参考链接提供了更多详情。
|
1月前
|
容器 内存技术
Qt中常用容器组控件介绍和实操-1
Qt中常用容器组控件介绍和实操
|
1月前
|
数据安全/隐私保护 图形学
Qt 输入组控件(Input Widgets)& 显示组控件(Display Widgets)详解
Qt 输入组控件(Input Widgets)& 显示组控件(Display Widgets)详解
|
1月前
|
API 容器
Qt中常用容器组控件介绍和实操-2
Qt中常用容器组控件介绍和实操
|
5天前
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
11 1
|
5天前
|
存储 消息中间件 算法
Java中的集合框架详解:List、Set、Map的使用场景
Java中的集合框架详解:List、Set、Map的使用场景
|
10天前
|
存储 安全 Java
Java List详解
Java List详解
|
11天前
|
Java API
使用 Java 来实现两个 List 的差集操作
使用 Java 来实现两个 List 的差集操作
15 3
|
2天前
|
存储 缓存 安全
Java List操作详解及常用方法
Java List操作详解及常用方法