Qt 基础组件速学 按钮篇

学习目标: Qt 基础组件复习/速学  按钮篇

前置环境

运行环境:qt creator 4.12

gif录制软件:ScreenToGif

NickeManarin/ScreenToGif: 🎬 ScreenToGif allows you to record a selected area of your screen, edit and save it as a gif or video. (github.com)


学习内容:

    setQPushButton();     //输出按钮
    setQToolButton();           //系统按钮
    setQRadioButton();                //多选1按钮
    setQcheckBox();                        //三态按钮
    setQCommandLinkButton();         //超链接按钮
    setQDialogButtonBox();    //对话框按钮

以图片的形式表示,各个按钮的实现功能区别


详细代码和运行测试

最常用的输出按钮

void MainWindow::setQPushButton(){
    setGeometry(50,50,500,300);
    pb1 = new QPushButton("变色1",this);
    pb2 = new QPushButton("变色2",this);

    pb1->setGeometry(20,20,100,100);
    pb2->setGeometry(20,150,100,100);

    connect(pb1,SIGNAL(clicked()),this,SLOT(qpbColor()));
    connect(pb2,SIGNAL(clicked()),this,SLOT(qpb2Color()));
}
void MainWindow::qpbColor(){
    // 在 Qt 构造函数中设置样式表 this->setStyleSheet(QString::fromUtf8(":/path/to/your/stylesheet.css"));
    this->setStyleSheet("QMainWindow { background-color: rgba(255, 255, 0, 100%); }");

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

按钮工具包

void MainWindow::setQToolButton(){
    tbar=new QToolBar("工具",this);
    tbar->setGeometry(0,0,1000,20);
    tbt = new QToolButton(this);
    //获取系统的icon
    QStyle* sty = QApplication::style();
    QIcon ico = sty->standardIcon(QStyle::SP_TitleBarContextHelpButton);

    tbt->setIcon(ico);
    QSize sz(10,10);
    tbt->setIconSize(sz);

    tbt->setText("系统帮助提示"); // 设置将要显示文本
    tbt->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    tbt->setGeometry(0,0,100,20);
}

多选一按钮

void MainWindow::setQRadioButton(){
    rdb1 = new QRadioButton("动态",this);
    rdb2 = new QRadioButton("静态",this);
    rdb1->setGeometry(140,120,40,40);
    rdb2->setGeometry(180,120,40,40);
    rdb2->setChecked(true);

    m_color={Qt::red,Qt::green,Qt::yellow,Qt::cyan};
    m_colorCurrent_index=0;
    m_switchQtimes = new QTimer(this);
    connect(m_switchQtimes,&QTimer::timeout,this,&MainWindow::setColorDynamic);
    connect(rdb1,&QRadioButton::toggled,this,&MainWindow::handleRadioButtonToggled);
    connect(rdb2,&QRadioButton::toggled,this,&MainWindow::handleRadioButtonToggled);


}
void MainWindow::handleRadioButtonToggled(bool checked){
    if(checked){
        QRadioButton* radio=qobject_cast<QRadioButton*>(sender());
        if (radio == rdb1) {
              // 动态模式
              m_switchQtimes->start(800);
          } else if (radio == rdb2) {
              // 静态模式
              m_switchQtimes->stop();
              setColorStatic();
          }
    }
}
void MainWindow::setColorDynamic(){
    QColor cColor = m_color[m_colorCurrent_index];
    //设置背景颜色
    QPalette palette = this->palette();
    palette.setColor(QPalette::Window,cColor);
    this->setPalette(palette);
    this->update();

    m_colorCurrent_index=(m_colorCurrent_index+1) % m_color.size();
}
void MainWindow::setColorStatic(){

    QPalette palette = this->palette();
    palette.setColor(QPalette::Window,Qt::white);
    this->setPalette(palette);
    this->update();

}

 

 三态按钮

 

void MainWindow::setQcheckBox(){
    qcb = new QCheckBox("checkBox特性",this);
    qcb->setGeometry(200,120,150,100);
    qcb->setCheckState(Qt::Checked);

    qcb->setTristate(true); //三态模式
    connect(qcb,SIGNAL(stateChanged(int)),this,SLOT(setQcheckBoxSlots(int)));
}
void MainWindow::setQcheckBoxSlots(int s){
    switch (s) {
    case Qt::Checked:
        qcb->setText("选中状态");
        break;
    case Qt::Unchecked:
        qcb->setText("未选择");
        break;
    case Qt::PartiallyChecked:
        qcb->setText("待定");
        break;

    default:
        break;
    }
}

 

超链接按钮

void MainWindow::setQCommandLinkButton(){
    clb=new QCommandLinkButton("进入百度",this);
    clb->setGeometry(300,120,150,50);
    connect(clb,SIGNAL(clicked()),this,SLOT(setQCommandLinkButtonLots()));
}

void MainWindow::setQCommandLinkButtonLots(){
    QDesktopServices::openUrl(QUrl("www.baidu.com"));
}

 对话框按钮

void MainWindow::setQDialogButtonBox(){
    dbb=new QDialogButtonBox(this);
    dbb->setGeometry(120,150,150,150);

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

    pb= new QPushButton("确认");
    dbb->addButton(pb,QDialogButtonBox::ActionRole);

    connect(dbb, &QDialogButtonBox::clicked, this, &MainWindow::setQDialogButtonBoxLots);

}

void MainWindow::setQDialogButtonBoxLots(QAbstractButton* s){
        if(s == dbb->button(QDialogButtonBox::Cancel))  qDebug()<<"取消";
            else if (s == pb)  qDebug()<<"确认";
}

 

最后附上源代码链接

对您有帮助的话,帮忙点个star

 9-button-set · jbjnb/Qt demo - 码云 - 开源中国 (gitee.com)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值