day12 coding Qt案例: 上课代码

This commit is contained in:
2023-06-28 23:02:58 +08:00
parent fc4b1e799b
commit de36ac8b3c
18 changed files with 681 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
+34
View File
@@ -0,0 +1,34 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
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 \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
res.qrc
+5
View File
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/movies">
<file alias="yanhua">resources/yanhua1.gif</file>
</qresource>
</RCC>
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

+47
View File
@@ -0,0 +1,47 @@
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
// 加载动画资源
yanhuagif = new QMovie(":/movies/yanhua");
srand(time(NULL));
randNum = rand() % 10; // 0-9的数
// 开启定时器, 用于定时换随机数
timer = new QTimer();
connect(timer,&QTimer::timeout,[&](){
randNum = rand() % 10;
});
timer->start(5*1000); // 5秒
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
guessNum = ui->numEdit->text().toInt();
qDebug() << "随机数:" << randNum << "输入的数" << guessNum << endl;
QString resStr = "随机数: "+ QString::number(randNum)+ ", 你输入的数: "+QString::number(guessNum);
ui->tipesLable->setText("本轮结果: " + resStr);
if(guessNum == randNum){
ui->resaultShowLabel->setMovie(yanhuagif);
yanhuagif->start();
} else {
if(yanhuagif->state() == QMovie::Running)
yanhuagif->stop();
ui->resaultShowLabel->setText(" ");
}
}
+32
View File
@@ -0,0 +1,32 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMovie>
#include <QDebug>
#include <QTimer>
#include <cstdlib>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
int guessNum = 0;
int randNum = 0;
QMovie *yanhuagif;
QTimer *timer;
};
#endif // WIDGET_H
+133
View File
@@ -0,0 +1,133 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>500</width>
<height>400</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLineEdit" name="numEdit">
<property name="styleSheet">
<string notr="true">padding:5px;
font: 18pt &quot;Agency FB&quot;;</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="styleSheet">
<string notr="true">font: 18pt &quot;Agency FB&quot;;</string>
</property>
<property name="text">
<string>猜一猜</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="tipesLable">
<property name="styleSheet">
<string notr="true">font: 14pt &quot;宋体&quot;;
color: rgb(170, 170, 255);</string>
</property>
<property name="text">
<string>结果: </string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="resaultShowLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>