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;
}
 
相关文章
|
9月前
|
前端开发 程序员 API
【Qt】控件介绍
【Qt】控件介绍
|
9月前
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 空白项Spacer
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 空白项Spacer
385 2
|
9月前
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 表单布局Form Layout
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 表单布局Form Layout
292 2
|
9月前
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 网格布局Grid Layout
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 网格布局Grid Layout
1148 2
MFC基本控件4-列表框控件List Box
MFC基本控件4-列表框控件List Box
|
9月前
|
网络协议 Linux C++
【Qt】多种控件实现“hello world“
【Qt】多种控件实现“hello world“
|
10月前
|
数据安全/隐私保护 C++ 计算机视觉
Qt(C++)开发一款图片防盗用水印制作小工具
文本水印是一种常用的防盗用手段,可以将文本信息嵌入到图片、视频等文件中,用于识别和证明文件的版权归属。在数字化和网络化的时代,大量的原创作品容易被不法分子盗用或侵犯版权,因此加入文本水印成为了保护原创作品和维护知识产权的必要手段。 通常情况下,文本水印可以包含版权声明、制作者姓名、日期、网址等信息,以帮助识别文件的来源和版权归属。同时,为了增强防盗用效果,文本水印通常会采用字体、颜色、角度等多种组合方式,使得水印难以被删除或篡改,有效地降低了盗用意愿和风险。 开发人员可以使用图像处理技术和编程语言实现文本水印的功能,例如使用Qt的QPainter类进行文本绘制操作,将文本信息嵌入到图片中,
287 1
|
9月前
|
监控 C++ 容器
【qt】MDI多文档界面开发
【qt】MDI多文档界面开发
294 0
|
8月前
Qt开发
Qt开发
|
9月前
|
C++
C++ Qt开发:QUdpSocket网络通信组件
QUdpSocket是Qt网络编程中一个非常有用的组件,它提供了在UDP协议下进行数据发送和接收的能力。通过简单的方法和信号,可以轻松实现基于UDP的网络通信。不过,需要注意的是,UDP协议本身不保证数据的可靠传输,因此在使用QUdpSocket时,可能需要在应用层实现一些机制来保证数据的完整性和顺序,或者选择在适用的场景下使用UDP协议。
389 2