Qt学习记录(十)项目:数字炸弹/猜数字游戏

该博客围绕QT数字炸弹游戏项目展开,介绍了游戏界面、配置文件,阐述了page1和page2的功能实现要点。还给出了源代码,展示了项目现象。最后总结了QTimer与进度条关联、QLineEdit文本框操作等知识点,作者虽对项目有不足感,但表示收获颇丰。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

前言

一,游戏界面

​编辑

二,配置文件

三,功能实现(待补充,捏麻麻,学了两天,感觉快不识字了)

1,page1

(1)点击“开始游戏”从page1转跳到page2

1,page1->page2

2,如果玩家未选择时间,则警告,并且不跳转

(2)点击“退出游戏”

(3)“设置时间”

1,获取玩家选择选项,转型QString->int,赋值给全局变量

2,绑定进度条,范围和初始值

2,page2

(1)返回

(5)0-9数字按键

(6)“清除”和“确认”

(7)键盘按下数字,按下回车键 

1,按下回车键,检查输入值是否范围内,超出则清空文本并提示

2,输入值在范围内,根据输入值和“炸弹”的关系,改变范围

四,源代码

1,mywidget.h

2,mywidget.cpp

五,现象展示

六,知识点总结 

1,QTimer定时器和QProgressBar进度条的关联

(1)创建定时器

(2)定时器启动和停止

2,QLineEdit文本框

(1)限制0-9,两位数。但是该代码只针对于键盘

(2) 文本框清除

(3)输入文本居中

 (4)光标在文本框上

(5)将按键文本内容追加到文本框中

(6)文本内容获取和转型

 3,QLabel标签

(1)show和hide

(2) 内容

4,整体样式

5,QCOmboBox下拉框

(1)读取玩家选择的数值

(2)样式表

 6,进度条

(1)样式表

(2)初始值和范围

 (3)进度条随着定时器减少

7,QMessageBox 对话框

(1)问题框

(2)警告

 (3)样式表

 8,QStackedWidget

(1)页数转跳

 9,QRandomGenerator随机数

(1)获取随机数字

 10,获取点击按键的文本

11,QFrame样式表,包含关系


前言

香蕉猫猫(meme开始哭了)

一,游戏界面

二,配置文件

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyWidget)
{
    //初始状态
    ui->setupUi(this);
    //创建定时器
    timer = new QTimer(this);
    //文本框不超过2位数
    ui->lineEdit->setValidator(new QRegExpValidator(QRegExp("[0-9]{2}")));
    //提示隐藏
    ui->label_6->hide();
    ui->label_7->hide();
    ui->label_7->clear();
    //计时器结束
    connect (timer,&QTimer::timeout, this,&MyWidget::updateCountdowm);
    //数字按键0-9
    connect (ui->play_0,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_1,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_2,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_3,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_4,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_5,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_6,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_7,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_8,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_9,&QPushButton::pressed,this,&MyWidget::play_0_9);
    //清除和确认
    connect (ui->play_x,&QPushButton::pressed,ui->lineEdit,&QLineEdit::clear);
    connect (ui->play_e,&QPushButton::pressed,this,&MyWidget::on_lineEdit_editingFinished);
    //样式表
    this->setWindowFlags(Qt::FramelessWindowHint);      //隐藏最大最小化等按键
    setAttribute(Qt::WA_TranslucentBackground);         //设置窗口透明化
    ui->lineEdit->setAlignment(Qt::AlignHCenter);       //输入框文本居中
    ui->comboBox->setStyleSheet("QComboBox{"
                                "border-width: 2px;"
                                "border-style: solid;"
                                "border-radius:10px;"
                                "border-color: rgba(176, 100, 213,0);"
                                "background-color: rgba(176, 100, 213,0.5);"
                                "color: rgb(255, 255, 255);"
                                "}"

                                "QComboBox::drop-down{"
                                "border-width:0px;"
                                "border-style: solid;"
                                "border-radius:5px;"
                                "border-color: rgba(176, 100, 213,0);"
                                "background-color: rgba(176, 100, 213,0.5);"
                                "color: rgb(255, 255, 255);"
                                "}"

                                "QComboBox QAbstractItemView{"
                                "border-width:0px;"
                                "border-style: solid;"
                                "border-radius:5px;"
                                "border-color: rgba(176, 100, 213,0);"
                                "background-color: rgb(195,142,215);"
                                "color: rgb(255, 255, 255);"
                                "}");

    ui->progressBar->setStyleSheet("QProgressBar{"
                                   "font:9pt;"
                                   "border-radius:5px;"
                                   "text-align:center;"
                                   "border:1px solid #E8EDF2;"
                                   "background-color: rgb(179,222,255);"
                                   "border-color: rgb(180, 180, 180);"
                                   "}"

                                   "QProgressBar:chunk{"
                                   "border-radius:5px;"
                                   "background-color:rgb(16,116,191);"
                                   "}");
}

三,功能实现(待补充,捏麻麻,学了两天,感觉快不识字了)

1,page1

(1)点击“开始游戏”从page1转跳到page2

要点:

1,page1->page2
2,如果玩家未选择时间,则警告,并且不跳转
//“开始游戏”从page转跳到page_2
void MyWidget::on_pushButton_clicked()
{
    if(t==0)//未选择时间
    {
        QMessageBox::StandardButton reply;//警告
        reply = QMessageBox::warning (this, "警告!", "未选择时间");
    }
    else//跳转page2
    {
        num_1=0;num_2=100;                                     //范围初始值
        ui->stackedWidget->setCurrentIndex(i=1);               //跳转page2
        randomInt = QRandomGenerator::global()->bounded(0,101);//获取一个随机数字作为炸弹
        timer->start(1000);                                    //计时器启动,1s为单位
        ui->lineEdit->setFocus();                              //一转跳,光标就在文本框上面
        ui->label_4->setText(QString("%1-%2").arg(num_1).arg(num_2));//显示数字范围“0-100”
        ui->progressBar->setValue(t);                          //进度条的初始值
        //提示隐藏
        ui->label_6->hide();
        ui->label_7->hide();
        ui->label_7->clear();
    }
}

(2)点击“退出游戏”

//退出游戏
void MyWidget::on_pushButton_2_clicked()
{
    int ret = QMessageBox::question(this,"提示","是否退出游戏");
    if(ret == QMessageBox::Yes)  QApplication::quit();//立即终止应用程序
}

(3)“设置时间”

要点:

1,获取玩家选择选项,转型QString->int,赋值给全局变量
2,绑定进度条,范围和初始值
//设置时间
void MyWidget::on_comboBox_activated(const QString &arg1)
{
    int value =arg1.toInt();           //获取选择选项,并转型
    t = value;
    ui->progressBar->setRange(0,t);    //进度条的范围
    ui->progressBar->setValue(0);      //进度条的初始值
}
//时间到了
void MyWidget::updateCountdowm()
{
    int value = ui->progressBar->value();                   //获取进度条的长度
    if (value > 0)  ui->progressBar->setValue(value - 1);   //进度条不断减少
    else
    {                                                       //倒计时结束
        timer->stop();                                      //时间停止
        QMessageBox::StandardButton reply;                  //提示
        reply = QMessageBox::question (this, "时间已到", "是否重开一局游戏");
        if (reply == QMessageBox::Yes)
        {
            on_pushButton_clicked();                        //调用按钮点击事件,恢复初始值
        }
        else ui->stackedWidget->setCurrentIndex(i=0);       //返回主页
    }
}

2,page2

(1)返回

//page_2
//返回主界面
void MyWidget::on_pushButton_16_clicked()
{
    ui->stackedWidget->setCurrentIndex(i=0);    //page2->page
    timer->stop();                              //停止倒计时
}

(5)0-9数字按键

//按下数字按键
void MyWidget::play_0_9()
{
    QObject*mySender=this->sender();                     //获取所点击的按钮
    QPushButton *Button=(QPushButton*)mySender;          //mySender指针->QPushButton指针,赋值给 Button
    if(Button)
    {
        QString Text=Button->text();                     //获取按钮中的内容
        ui->lineEdit->setText(ui->lineEdit->text()+Text);//将按钮文本内容追加到文本框中
        if (Text.size()>2)                               //超出范围,直接清空并提示
        {
            ui->lineEdit->clear();                       //清除文本框
            //提示
            ui->label_6->show();
            ui->label_7->show();
            ui->label_7->setText("数字超出范围了");
        }
    }
}

(6)“清除”和“确认”

//MyWidget::MyWidget(QWidget *parent): QWidget(parent), ui(new Ui::MyWidget)
connect (ui->play_x,&QPushButton::pressed,ui->lineEdit,&QLineEdit::clear);
//按下清除键,清空文本框
connect (ui->play_e,&QPushButton::pressed,this,&MyWidget::on_lineEdit_editingFinished);
//按下确认键,执行回车键的代码

(7)键盘按下数字,按下回车键 

要点

1,按下回车键,检查输入值是否范围内,超出则清空文本并提示
2,输入值在范围内,根据输入值和“炸弹”的关系,改变范围
//按下回车
void MyWidget::on_lineEdit_editingFinished ()
{
    text = ui->lineEdit->text();            //文本框内容赋值给全局变量text
    int num = text.toInt();                 //text 转型int
    if(num<num_2&&num>num_1)                //输入值在范围内
    {
        //隐藏提示
        ui->label_7->hide();
        ui->label_6->hide();
        ui->label_7->clear();
        ui->lineEdit->clear();              //清除文本框
        ui->progressBar->setValue(t);       //恢复进度条初始值
        if(num>randomInt) {num_2=num;}      //num比"炸弹"大,改变显示范围的max
        else if(num<randomInt) {num_1=num;} //num比"炸弹"小,改变显示范围的min
        else if(num == randomInt)           //猜中"炸弹"
        {
            ui->label_4->setText(QString("炸弹是:%1").arg(randomInt));//提示炸弹的数值
            timer->stop();                                            //停止倒计时
            QMessageBox::StandardButton reply;                        //提示对话框
            reply = QMessageBox::question (this, "这是个炸弹!!!","是否重开一局游戏");
            if (reply == QMessageBox::Yes) on_pushButton_clicked();   //"开始游戏",恢复初始定义
            else
            {
                ui->stackedWidget->setCurrentIndex(i=0);            //返回主界面
                timer->stop();                                      //停止计时器
            }
        }
    }
    else if(num<=num_1)         //输入值不在范围内
    {
        num_1=num_1;            //防止再次循环时,赋值num_1
        ui->lineEdit->clear();  //清除文本框
        //提示
        ui->label_6->show();
        ui->label_7->show();
        ui->label_7->setText("数字太小了");
    }
    else
    {
        num_2=num_2;           //防止再次循环时,赋值num_2
        ui->lineEdit->clear(); //清除文本框
        //提示
        ui->label_6->show();
        ui->label_7->show();
        ui->label_7->setText("数字太大了");
    }
    ui->label_4->setText(QString("%1-%2").arg(num_1).arg(num_2));   //改变数值的范围
}

四,源代码

1,mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
#include <QCloseEvent>
#include <QTimeEdit>

QT_BEGIN_NAMESPACE
namespace Ui { class MyWidget; }
QT_END_NAMESPACE

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    MyWidget(QWidget *parent = 0);
    ~MyWidget();

protected:
    void closeEvent(QCloseEvent *ev) override;
    QString text;
private slots:

//page
    void on_pushButton_clicked();//开始游戏
    void on_pushButton_2_clicked() ;//退出游戏
    void on_comboBox_activated(const QString &arg1);//设置时间
    void updateCountdowm();//倒计时结束处理

//page_2
    void on_pushButton_16_clicked();//返回主界面

    void on_lineEdit_editingFinished();
    void play_0_9();

private:
    Ui::MyWidget *ui;
    QTimer *timer;

};
#endif // MYWIDGET_H

2,mywidget.cpp

#include "mywidget.h"
#include "ui_mywidget.h"

#include <QStackedWidget>

#include <QCloseEvent>//退出游戏
#include <QMessageBox>

#include <QProgressBar>//进度条
#include <QStyleOptionProgressBar>

#include <QTimer>//设置时间
#include <QComboBox>

#include <QLineEdit>//输入数据
#include <QIntValidator>
#include <QRandomGenerator>

#include <QPushButton>
#include <QDebug>

#include <QKeyEvent>

#include<QStyle>

static int t=0;       //时间
static int randomInt ;//随机数
static int i=0;       //页数
static int num_1=0;   //显示的范围min
static int num_2=100; //显示的范围max

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyWidget)
{
    //初始状态
    ui->setupUi(this);
    //创建定时器
    timer = new QTimer(this);
    //文本框不超过2位数
    ui->lineEdit->setValidator(new QRegExpValidator(QRegExp("[0-9]{2}")));
    //提示隐藏
    ui->label_6->hide();
    ui->label_7->hide();
    ui->label_7->clear();
    //计时器结束
    connect (timer,&QTimer::timeout, this,&MyWidget::updateCountdowm);
    //数字按键0-9
    connect (ui->play_0,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_1,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_2,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_3,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_4,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_5,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_6,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_7,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_8,&QPushButton::pressed,this,&MyWidget::play_0_9);
    connect (ui->play_9,&QPushButton::pressed,this,&MyWidget::play_0_9);
    //清除和确认
    connect (ui->play_x,&QPushButton::pressed,ui->lineEdit,&QLineEdit::clear);
    connect (ui->play_e,&QPushButton::pressed,this,&MyWidget::on_lineEdit_editingFinished);
    //样式表
    this->setWindowFlags(Qt::FramelessWindowHint);      //隐藏最大最小化等按键
    setAttribute(Qt::WA_TranslucentBackground);         //设置窗口透明化
    ui->lineEdit->setAlignment(Qt::AlignHCenter);       //输入框文本居中
    ui->comboBox->setStyleSheet("QComboBox{"
                                "border-width: 2px;"
                                "border-style: solid;"
                                "border-radius:10px;"
                                "border-color: rgba(176, 100, 213,0);"
                                "background-color: rgba(176, 100, 213,0.5);"
                                "color: rgb(255, 255, 255);"
                                "}"

                                "QComboBox::drop-down{"
                                "border-width:0px;"
                                "border-style: solid;"
                                "border-radius:5px;"
                                "border-color: rgba(176, 100, 213,0);"
                                "background-color: rgba(176, 100, 213,0.5);"
                                "color: rgb(255, 255, 255);"
                                "}"

                                "QComboBox QAbstractItemView{"
                                "border-width:0px;"
                                "border-style: solid;"
                                "border-radius:5px;"
                                "border-color: rgba(176, 100, 213,0);"
                                "background-color: rgb(195,142,215);"
                                "color: rgb(255, 255, 255);"
                                "}");

    ui->progressBar->setStyleSheet("QProgressBar{"
                                   "font:9pt;"
                                   "border-radius:5px;"
                                   "text-align:center;"
                                   "border:1px solid #E8EDF2;"
                                   "background-color: rgb(179,222,255);"
                                   "border-color: rgb(180, 180, 180);"
                                   "}"

                                   "QProgressBar:chunk{"
                                   "border-radius:5px;"
                                   "background-color:rgb(16,116,191);"
                                   "}");
}

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

//右上角X,关闭游戏
void MyWidget ::closeEvent (QCloseEvent *ev)
{
    int ret = QMessageBox::question(this,"提示","是否关闭窗口");
    if(ret == QMessageBox::Yes)  this->close();
    else  ev->ignore();

}
//page
//“开始游戏”从page转跳到page_2
void MyWidget::on_pushButton_clicked()
{
    if(t==0)//未选择时间
    {
        QMessageBox::StandardButton reply;//警告
        reply = QMessageBox::warning (this, "警告!", "未选择时间");
    }
    else//跳转page2
    {
        num_1=0;num_2=100;                                     //范围初始值
        ui->stackedWidget->setCurrentIndex(i=1);               //跳转page2
        randomInt = QRandomGenerator::global()->bounded(0,101);//获取一个随机数字作为炸弹
        timer->start(1000);                                    //计时器启动,1s为单位
        ui->lineEdit->setFocus();                              //一转跳,光标就在文本框上面
        ui->label_4->setText(QString("%1-%2").arg(num_1).arg(num_2));//显示数字范围“0-100”
        ui->progressBar->setValue(t);                          //进度条的初始值
    }
}
//退出游戏
void MyWidget::on_pushButton_2_clicked()
{
    int ret = QMessageBox::question(this,"提示","是否退出游戏");
    if(ret == QMessageBox::Yes)  QApplication::quit();//立即终止应用程序
}


//设置时间
void MyWidget::on_comboBox_activated(const QString &arg1)
{
    int value =arg1.toInt();//获取选择选项,并转型
    t = value;
    ui->progressBar->setRange(0,t);//进度条的范围
    ui->progressBar->setValue(0);  //进度条的初始值
}
//时间到了
void MyWidget::updateCountdowm()
{
    int value = ui->progressBar->value();                   //获取进度条的长度
    if (value > 0)  ui->progressBar->setValue(value - 1);   //进度条不断减少
    else
    {                                                       //倒计时结束
        timer->stop();                                      //时间停止
        QMessageBox::StandardButton reply;                  //提示
        reply = QMessageBox::question (this, "时间已到", "是否重开一局游戏");
        if (reply == QMessageBox::Yes)
        {
            on_pushButton_clicked();                        //调用按钮点击事件,恢复初始值
        }
        else ui->stackedWidget->setCurrentIndex(i=0);       //返回主页
    }
}

//page_2
//返回主界面
void MyWidget::on_pushButton_16_clicked()
{
    ui->stackedWidget->setCurrentIndex(i=0);    //page2->page
    timer->stop();                              //停止倒计时
}

//按下数字按键
void MyWidget::play_0_9()
{
    QObject*mySender=this->sender();                     //获取所点击的按钮
    QPushButton *Button=(QPushButton*)mySender;          //mySender指针->QPushButton指针,赋值给 Button
    if(Button)
    {
        QString Text=Button->text();                     //获取按钮中的内容
        ui->lineEdit->setText(ui->lineEdit->text()+Text);//将按钮文本内容追加到文本框中
        if (Text.size()>2)                               //超出范围,直接清空并提示
        {
            ui->lineEdit->clear();                       //清除文本框
            //提示
            ui->label_6->show();
            ui->label_7->show();
            ui->label_7->setText("数字超出范围了");
        }
    }
}

//按下回车
void MyWidget::on_lineEdit_editingFinished ()
{
    text = ui->lineEdit->text();            //文本框内容赋值给全局变量text
    int num = text.toInt();                 //text 转型int
    if(num<num_2&&num>num_1)                //输入值在范围内
    {
        //隐藏提示
        ui->label_7->hide();
        ui->label_6->hide();
        ui->label_7->clear();
        ui->lineEdit->clear();              //清除文本框
        ui->progressBar->setValue(t);       //恢复进度条初始值
        if(num>randomInt) {num_2=num;}      //num比"炸弹"大,改变显示范围的max
        else if(num<randomInt) {num_1=num;} //num比"炸弹"小,改变显示范围的min
        else if(num == randomInt)           //猜中"炸弹"
        {
            ui->label_4->setText(QString("炸弹是:%1").arg(randomInt));//提示炸弹的数值
            timer->stop();                                            //停止倒计时
            QMessageBox::StandardButton reply;                        //提示对话框
            reply = QMessageBox::question (this, "这是个炸弹!!!","是否重开一局游戏");
            if (reply == QMessageBox::Yes) on_pushButton_clicked();   //"开始游戏",恢复初始定义
            else
            {
                ui->stackedWidget->setCurrentIndex(i=0);            //返回主界面
                timer->stop();                                      //停止计时器
            }
        }
    }
    else if(num<=num_1)         //输入值不在范围内
    {
        num_1=num_1;            //防止再次循环时,赋值num_1
        ui->lineEdit->clear();  //清除文本框
        //提示
        ui->label_6->show();
        ui->label_7->show();
        ui->label_7->setText("数字太小了");
    }
    else
    {
        num_2=num_2;           //防止再次循环时,赋值num_2
        ui->lineEdit->clear(); //清除文本框
        //提示
        ui->label_6->show();
        ui->label_7->show();
        ui->label_7->setText("数字太大了");
    }
    ui->label_4->setText(QString("%1-%2").arg(num_1).arg(num_2));   //改变数值的范围
}

五,现象展示

QT项目:数字炸弹

六,知识点总结 

1,QTimer定时器和QProgressBar进度条的关联

(1)创建定时器

timer = new QTimer(this);

(2)定时器启动和停止

timer->start(1000);  //计时器启动,1s为单位
timer->stop();       //时间停止

2,QLineEdit文本框

(1)限制0-9,两位数。但是该代码只针对于键盘

setValidator(new QRegExpValidator(QRegExp("[0-9]{2}")));

(2) 文本框清除

按键控制

connect (ui->play_x,&QPushButton::pressed,ui->lineEdit,&QLineEdit::clear);

 直接调用

 ui->label_7->show();

(3)输入文本居中

ui->lineEdit->setAlignment(Qt::AlignHCenter);       //输入框文本居中

 (4)光标在文本框上

 ui->lineEdit->setFocus();                              //一转跳,光标就在文本框上面

(5)将按键文本内容追加到文本框中

ui->lineEdit->setText(ui->lineEdit->text()+Text);//将按钮文本内容追加到文本框中

(6)文本内容获取和转型

text = ui->lineEdit->text();            //文本框内容赋值给全局变量text
int num = text.toInt();                 //text 转型int

 3,QLabel标签

(1)show和hide

//提示隐藏
    ui->label_6->hide();
    ui->label_7->hide();
    ui->label_7->clear();

(2) 内容

ui->label_4->setText(QString("%1-%2").arg(num_1).arg(num_2));//显示数字范围“0-100”
/
ui->label_4->setText(QString("炸弹是:%1").arg(randomInt));//提示炸弹的数值

4,整体样式

this->setWindowFlags(Qt::FramelessWindowHint);      //隐藏最大最小化等按键
setAttribute(Qt::WA_TranslucentBackground);         //设置窗口透明化

5,QCOmboBox下拉框

(1)读取玩家选择的数值

void MyWidget::on_comboBox_activated(const QString &arg1)
{
    int value =arg1.toInt();//获取选择选项,并转型
    t = value;
    ui->progressBar->setRange(0,t);//进度条的范围
    ui->progressBar->setValue(0);  //进度条的初始值
}

(2)样式表

    ui->comboBox->setStyleSheet("QComboBox{"
                                "border-width: 2px;"        
                                "border-style: solid;"
                                "border-radius:10px;"
                                "border-color: rgba(176, 100, 213,0);"
                                "background-color: rgba(176, 100, 213,0.5);"
                                "color: rgb(255, 255, 255);"
                                "}"

                                "QComboBox::drop-down{"
                                "border-width:0px;"
                                "border-style: solid;"
                                "border-radius:5px;"
                                "border-color: rgba(176, 100, 213,0);"
                                "background-color: rgba(176, 100, 213,0.5);"
                                "color: rgb(255, 255, 255);"
                                "}"

                                "QComboBox QAbstractItemView{"
                                "border-width:0px;"
                                "border-style: solid;"
                                "border-radius:5px;"
                                "border-color: rgba(176, 100, 213,0);"
                                "background-color: rgb(195,142,215);"
                                "color: rgb(255, 255, 255);"
                                "}");

 6,进度条

(1)样式表

    ui->progressBar->setStyleSheet("QProgressBar{"
                                   "font:9pt;"
                                   "border-radius:5px;"
                                   "text-align:center;"
                                   "border:1px solid #E8EDF2;"
                                   "background-color: rgb(179,222,255);"
                                   "border-color: rgb(180, 180, 180);"
                                   "}"

                                   "QProgressBar:chunk{"
                                   "border-radius:5px;"
                                   "background-color:rgb(16,116,191);"
                                   "}");

(2)初始值和范围

ui->progressBar->setValue(t); //进度条的初始值
ui->progressBar->setRange(0,t);//进度条的范围

 (3)进度条随着定时器减少

int value = ui->progressBar->value();                   //获取进度条的长度
if (value > 0)  ui->progressBar->setValue(value - 1);   //进度条不断减少

7,QMessageBox 对话框

(1)问题框

void MyWidget ::closeEvent (QCloseEvent *ev)
{
    int ret = QMessageBox::question(this,"提示","是否关闭窗口");
    if(ret == QMessageBox::Yes)  this->close();
    else  ev->ignore();

}
//关闭窗口
void MyWidget::on_pushButton_2_clicked()
{
    int ret = QMessageBox::question(this,"提示","是否退出游戏");
    if(ret == QMessageBox::Yes)  QApplication::quit();//立即终止应用程序
}
//时间结束//
        eply = QMessageBox::question (this, "时间已到", "是否重开一局游戏");
        if (reply == QMessageBox::Yes) on_pushButton_clicked();//调用按钮点击事件,恢复初始值
        else ui->stackedWidget->setCurrentIndex(i=0);       //返回主页
猜中炸弹///
        QMessageBox::StandardButton reply;                        //提示对话框
        reply = QMessageBox::question (this, "这是个炸弹!!!","是否重开一局游戏");
        if (reply == QMessageBox::Yes) on_pushButton_clicked();  //"开始游戏",恢复初始定义
        else
       {
            ui->stackedWidget->setCurrentIndex(i=0);            //返回主界面
            timer->stop();                                      //停止计时器
       }

(2)警告

QMessageBox::StandardButton reply;//警告
reply = QMessageBox::warning (this, "警告!", "未选择时间");

 (3)样式表

QMessageBox QLabel
{
border-width: 2px;
border-style: solid;
border-radius:10px;
border-color: rgba(176, 100, 213,0);
background-color: rgba(176, 100, 213,0.5);
color: rgb(255, 255, 255);
}
QMessageBox
{
border-style: solid;
border-radius: 0px;
border-color: rgba(255, 255, 255,0);
background-color: rgb(221,182,215);
}

QMessageBox QPushButton
{
background-color: rgba(176, 100, 213,0.5);
color: rgb(255, 255, 255);
border-width: 1px;
border-style: solid;
border-radius: top-left top-right bottom-right bottom-left;
border-radius:25px;
border-color: rgba(176, 100, 213,0);

}
QMessageBox QPushButton:hover
{
background-color: rgb(176, 100, 213);
color: rgba(255, 255, 255,0.8);
}

QMessageBox QPushButton:pressed
{
background-color:rgba(255, 190, 245,0.8);
color:rgb(191, 0, 3);
}

 8,QStackedWidget

(1)页数转跳

ui->stackedWidget->setCurrentIndex(i=1);               //跳转page2

 9,QRandomGenerator随机数

(1)获取随机数字

范围  [0,101)

andomInt = QRandomGenerator::global()->bounded(0,101);//获取一个随机数字作为炸弹

 10,获取点击按键的文本

QObject*mySender=this->sender();                     //获取所点击的按钮
QPushButton *Button=(QPushButton*)mySender;          //mySender指针->QPushButton指针,赋值给 Button
//
QString Text=Button->text();                     //获取按钮中的内容
ui->lineEdit->setText(ui->lineEdit->text()+Text);//将按钮文本内容追加到文本框中

11,QFrame样式表,包含关系

#frame
{
border-width: 5px;
border-style: solid;
border-color: rgba(255, 255, 255,0);
border-radius: top-left top-right bottom-right bottom-left;
border-radius:10px;
background-color: rgba(122, 122, 122,0.2)

}
QPushButton
{
background-color: rgba(176, 100, 213,0.5);
color: rgb(255, 255, 255);
border-width: 1px;
border-style: solid;
border-radius: top-left top-right bottom-right bottom-left;
border-radius:25px;
border-color: rgba(176, 100, 213,0);
	font: 87 20pt "锐字太空跑酷像素简-闪 超黑";
}
QLabel
{
border-width: 2px;
border-style: solid;
border-radius:10px;
border-color: rgba(176, 100, 213,0);
background-color: rgba(176, 100, 213,0.5);
color: rgb(255, 255, 255);
}

QFrame QPushButton:hover
{
background-color: rgb(176, 100, 213);
color: rgba(255, 255, 255,0.8);
}

QFrame QPushButton:pressed
{
background-color:rgba(255, 190, 245,0.8);
color:rgb(191, 0, 3);
}

废话ing:第一次写的小项目,还有好多想法没有实现,比如动态的背景;界面设计的不是很满意;可以选择2位数,3位数,4位数的下拉框;猜中数字和时间结束游戏失败的小动画没做;这次的项目笔记没写好等等。但是学到了很多东西,而且很有成就感,能够把之前学到的都应用起来,还有很多知识盲区待补充。一起加油吧。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值