QT - 按钮功能的使用

工程文件.pro:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets texttospeech

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 头文件.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QPushButton>
#include <QMainWindow>
#include <QTextToSpeech>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    //定义三个按钮
    QPushButton *button1;
    QPushButton *button2;
    QPushButton *button3;

    //定义一个播报者
    QTextToSpeech speech;

};
#endif // MAINWINDOW_H

 主函数.cpp:

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

 类.cpp:

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //设置界面大小
    this->setFixedSize(430,300);
    //设置界面名
    this->setWindowTitle("🐔IKUN");
    //设置允许修改背景色
    this->setAutoFillBackground(true);
    //设置背景色
    this->setBackgroundRole(QPalette::ColorRole::HighlightedText);
    //设置位置
    this->move(800,300);
    //设置三个按钮位置
    button1 = new QPushButton(this);
    button1->setParent(this);
    int butt_size_w = 100;
    int butt_size_h = 50;
    button1->resize(butt_size_w,butt_size_h);
    button1->move(this->width()/2-butt_size_w/2,this->height()/2-butt_size_h/2-butt_size_h);
    button1->setText("1");

    button2 = new QPushButton(this);
    button2->setParent(this);
    button2->resize(butt_size_w,butt_size_h);
    button2->move(this->width()/2-butt_size_w/2,this->height()/2-butt_size_h/2);
    button2->setText("2");

    button3 = new QPushButton(this);
    button3->setParent(this);
    button3->resize(butt_size_w,butt_size_h);
    button3->move(this->width()/2-butt_size_w/2,this->height()/2-butt_size_h/2+butt_size_h);
    button3->setText("3");

    //第一个按钮实现播报第二个按钮的内容,播报结束后,设置自己不可用
    //第二个按钮的内容是关闭,实现功能是关掉整个项目
    //第三个按钮功能是将第一个按钮设置为可以状态
    connect(button1,&QPushButton::clicked,[=](){
            speech.say(button2->text());
            button1->setDisabled(true);
    });
    connect(button2,&QPushButton::clicked,[=](){
            this->close();
    });
    connect(button3,&QPushButton::clicked,[=](){
            button1->setEnabled(true);
    });
}

MainWindow::~MainWindow()
{

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding Peasant

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值