Qt 控件简单实例应用

记录一个初学小白的学习过程,记录一些浅显的学习笔记


目录

QPushButton 命令按钮

QToolButton 工具按钮

RadioButton 单选按钮

CheckBox 复选按钮

CommandLinkButton 命令链接按钮

DialogButtonBox 按钮盒

QPushButton 命令按钮

实现两个按钮,在点击时切换对话框背景颜色
第一个是mainwindow.h代码第二个是mainwindow.cpp代码

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton> //引入Qpushbutton对应的头文件

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    //声明两个Qpushbutton对象
    QPushButton *pb1,*pb2;
private slots:
    //声明pb1和pb2的槽函数
    void pushbutton1_clicked();
    void pushbutton2_clicked();

};
#endif // MAINWINDOW_H
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //设置窗口大小 前两个参数是窗口出生点坐标(根据电脑的左上角是(0,0)),后两个是大小参数
    this->setGeometry(333,333,1500,900);

    //这里设置完按钮后,默认会堆叠在一起
    pb1=new QPushButton("命令按钮1",this);
    pb2=new QPushButton("命令按钮2",this);


    //设置两个QPushbutton的坐标,前两个参数是位置坐标,后两个是大小参数
    pb1->setGeometry(20,20,150,150);
    pb2->setGeometry(20,200,150,150);

    //信号与槽函数连接
    connect(pb1,SIGNAL(clicked()),this,SLOT(pushbutton1_clicked()));
    connect(pb2,SIGNAL(clicked()),this,SLOT(pushbutton2_clicked()));
}

MainWindow::~MainWindow()
{
}

void MainWindow::pushbutton1_clicked()
{
    this->setStyleSheet("QMainWindow {background-color:rgba(255,255,0,100%);}");
}
void MainWindow::pushbutton2_clicked()
{
    this->setStyleSheet("QMainWindow {background-color:rgba(255,0,0,100%);}");
}

QToolButton 工具按钮

实现工具按钮以及工具条

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QToolBar>  //引入QToolBar类
#include <QToolButton>  // 引入QToolButton类

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    //声明QToolButton对象和QToolBar对象
    QToolBar *tbar;
    QToolButton *tbutton;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"

#include <QApplication>
#include <QStyle>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //设置窗口运行位置
    this->setGeometry(333,333,1500,900);

    //将QToolBar对象实例化
    tbar=new QToolBar(this);
    tbar->setGeometry(20,20,400,100);

    //将QStyle类对象进行实例化,主要目的设置风格,图标是系统自带
    QStyle *sty=QApplication::style();
    QIcon ico=sty->standardIcon(QStyle::SP_TitleBarContextHelpButton);

    //将QToolButton对象进行实例化
    tbutton=new QToolButton();
    tbutton->setIcon(ico);
    tbutton->setText("系统帮助提示"); //设置要显示的文本

    //调用setToolButtonStyle函数设置tbutton样式,设置文本在图标下方
    tbutton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    //将tbutton添加到tbar里面
    tbar->addWidget(tbutton);
}
MainWindow::~MainWindow()
{
}

RadioButton 单选按钮

实现两个单选按钮,只能有一个被选中状态

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QRadioButton>
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    //声明两个radiobutton 对象
    QRadioButton *radb1,*radb2;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(300,300,400,400);
    //将QRadioButton类的两个对象进行实例化
    radb1 = new QRadioButton(this);
    radb2 = new QRadioButton(this);

    //设置两个对象位置
    radb1->setGeometry(20,20,150,40);
    radb2->setGeometry(20,80,150,40);

    //设置两个单选按钮文本
    radb1->setText("选择按钮1");
    radb2->setText("选择按钮2");

    //设置命令按钮默认值Checked false true
    radb1->setChecked(true);
    radb2->setChecked(false);
}

MainWindow::~MainWindow()
{
}

CheckBox 复选按钮

实现一个复选按钮的三种状态  选中、半选、未选中

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCheckBox>

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    QCheckBox *cb;
private slots:
    //声明QCheckBox槽函数,在操作过程中并且带参数传递,通过这个参数接收信号
    void checkboxstate(int );
};
#endif // MAINWINDOW_H
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(300,300,300,300);
    //实例化
    cb=new QCheckBox(this);
    cb->setGeometry(30,50,250,50);

    cb->setCheckState(Qt::Checked); //初始化三态复选框状态:checked
    cb->setText("初始化状态为:Chcked状态");

    cb->setTristate(); //开启三态模式,必须开启,否则只有两种状态(checked unchecked)

    connect(cb,SIGNAL(stateChanged(int)),this,SLOT(checkboxstate(int)));
}

MainWindow::~MainWindow()
{
}

void MainWindow::checkboxstate(int istate)
{
    //判断checkbox状态
    switch(istate)
    {
    case Qt::Checked: //选中状态
        cb->setText("选中状态OK");
        break;
    case Qt::Unchecked:  //未选中状态
        cb->setText("未选中状态NO");
        break;
    case Qt::PartiallyChecked: //半选中状态
        cb->setText("半选中状态");
        break;
    default:
        break;
    }
}

CommandLinkButton 命令链接按钮

实现一个链接按钮,这里链接到百度网址

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCommandLinkButton>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    QCommandLinkButton *clb;
private slots:
    void clbClicked();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include<QDesktopServices> //引入桌面服务
#include<QUrl>  //引入URL


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(300,300,400,400);

    clb=new QCommandLinkButton("testclb","clicked testclb",this);
    clb->setGeometry(50,100,250,100);
    connect(clb,SIGNAL(clicked()),this,SLOT(clbClicked()));
}
MainWindow::~MainWindow()
{

}
void MainWindow::clbClicked(){
    //调用系统服务打开操作
    QDesktopServices::openUrl(QUrl("www.baidu.com"));
}

DialogButtonBox 按钮盒

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDialogButtonBox>
#include <QPushButton>
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    QDialogButtonBox *dbb;
    QPushButton *pb;
private slots:
    void dbbpbClicked(QAbstractButton *);
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
     this->setGeometry(300,300,400,400);

     dbb=new QDialogButtonBox(this);
     dbb->setGeometry(100,100,200,30);

     dbb->addButton(QDialogButtonBox::Cancel);
     dbb->button(QDialogButtonBox::Cancel)->setText("取消");

     pb=new QPushButton("自定义");
     //将pb添加到dbb,并且设定ButtonRole为ActionRole
     dbb->addButton(pb,QDialogButtonBox::ActionRole);

    connect(dbb,SIGNAL(clicked(QAbstractButton*)),this,
            SLOT(dbbpbClicked(QAbstractButton*)));
}

MainWindow::~MainWindow()
{
}

void MainWindow::dbbpbClicked(QAbstractButton *bt){
    if(bt==dbb->button(QDialogButtonBox::Cancel)){
        qDebug()<<"点击的是取消按钮"<<endl;
    }
    else if(bt==pb){
        qDebug()<<"点击自定义按钮"<<endl;
    }

}


 gg,推荐课程:https://xxetb.xetslk.com/s/kNPSd

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值