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 
#include 
#include 
#include 

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 

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()
{

}

你可能感兴趣的:(C++,qt,开发语言,c++,linux,蓝桥杯)