【QT】控件的用法介绍

QLabel(很重要)

QPixmap在Qt中代表的就是一张图片

QPicture不是图片

如果图片不能完整显示,那就是没有布局

c 复制代码
   //添加静态图片
       如果构造的时候没有指定,可以在外面用load()指定图片路径
  ui->label->setPixmap(QPixmap(":/picture/86.jpg"));
//使得图片完整显示  
ui->label->setScaledContents(true);

添加动态图片

c 复制代码
 //添加动态图片
       QMovie *movie = new QMovie(":/picture/mario.gif");
       ui->label->setMovie(movie);
       movie->start();//启动播放

只能播放gif的图片

不能播放mp4

必须是指针,因为在看的时候movie,对象变量已经被析构了,就看不到了,而指针则需要手动去释放(这里是Qt的内存回收机制来释放)。

单选框

Group Box

一定要对放进去的控件布局,否则看不到

多选框

加入槽函数

c 复制代码
#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QCheckBox>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{  
       ui->setupUi(this);
       this->setWindowTitle("网易");
       //this->setWindowIcon(QIcon(":/picture/86.jpg"));
       this->setFixedSize(500,500);
              //单选按钮
       connect(ui->radioButton,&QRadioButton::released,this,[=](){

           QMessageBox::information(this,"radiobutton","haha+++");
       });
//多选按钮
       connect(ui->checkBox,&QCheckBox::stateChanged,this,[=](int state){

           QMessageBox::information(this,"checkBox",QString::number(state));
       });                                             //打印state的值

}

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

listWidget

c 复制代码
  ui->listWidget->addItem("你好,世界");
   QListWidgetItem *Item = new  QListWidgetItem(QIcon(":/picture/86.jpg"),"king");
   ui->listWidget->addItem(Item);
   //ui->listWidget->addItem(new QListWidgetItem(QIcon(":/picture/86.jpg"),"king"));

Table Widget

c 复制代码
        //1.指定行数
        ui->tableWidget->setRowCount(100);
        //2.指定列数
        ui->tableWidget->setColumnCount(3);
        
    
        QStringList list;
        list<<"姓名"<<"性别"<<"年龄";
        ui->tableWidget->setHorizontalHeaderLabels(list);
        QTableWidgetItem *Item1 = new QTableWidgetItem(QIcon(":/picture/86.jpg"),"king");
        ui->tableWidget->setItem(0,0,Item1);

滚轮

可以显示更多内容

ToolBox

StackedWidget

c 复制代码
    connect(ui->pushButton_10,&QPushButton::pressed,this,[=](){

        ui->stackedWidget->setCurrentIndex(0);
    });
    connect(ui->pushButton_11,&QPushButton::pressed,this,[=](){

        ui->stackedWidget->setCurrentIndex(1);
    });
    connect(ui->pushButton_8,&QPushButton::pressed,this,[=](){

        ui->stackedWidget->setCurrentIndex(2);
    });

Combo Box

相关推荐
Felix_One5 天前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
blasit8 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
郑州光合科技余经理13 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo12313 天前
matlab画图工具
开发语言·matlab
dustcell.13 天前
haproxy七层代理
java·开发语言·前端
norlan_jame13 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone13 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
修炼前端秘籍的小帅13 天前
Stitch——Google热门的免费AI UI设计工具
前端·人工智能·ui
QQ40220549613 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月13 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js