day12 coding Qt案例练习

This commit is contained in:
flykhan 2023-06-28 22:57:56 +08:00
parent 554a5f12d0
commit 3d5f265596
19 changed files with 649 additions and 0 deletions

11
day12/qtdemo3/main.cpp Executable file
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
day12/qtdemo3/qtdemo3.pro Executable file
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

10
day12/qtdemo3/res.qrc Executable file
View File

@ -0,0 +1,10 @@
<RCC>
<qresource prefix="/images">
<file alias="add">resources/add.png</file>
<file alias="mario">resources/mario.gif</file>
<file alias="open">resources/open.png</file>
<file alias="save">resources/save.png</file>
<file alias="sunny">resources/Sunny.jpg</file>
</qresource>
<qresource prefix="/videos"/>
</RCC>

BIN
day12/qtdemo3/resources/Sunny.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
day12/qtdemo3/resources/add.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

BIN
day12/qtdemo3/resources/mario.gif Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
day12/qtdemo3/resources/open.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

BIN
day12/qtdemo3/resources/save.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

99
day12/qtdemo3/widget.cpp Executable file
View File

@ -0,0 +1,99 @@
#include "widget.h"
#include "ui_widget.h"
#include <QPixmap>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
n=0;
ui->setupUi(this);
// 设置 imageLable 的图片资源
QPixmap pixmap;
// : 开头表示引用 Qt 的资源文件
pixmap.load(":/images/mario"); // 加载像素图
ui->imageLable->resize(200,200);
ui->imageLable->setPixmap(pixmap); // 设置 QLable 显示图片
marioGif = new QMovie(":/images/mario");
ui->imageLable->setMovie(marioGif);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_hotBtn_clicked()
{
ui->whatLable->setText("巧乐兹");
}
void Widget::on_coldBtn_clicked()
{
ui->whatLable->setText("厚棉被");
}
void Widget::on_coldBtn_2_clicked()
{
}
void Widget::on_setTitleIconBtn_clicked()
{
n++;
if(n%2==0){
this->setWindowIcon(QIcon(":/images/save"));
this->setWindowTitle("标题被修改了");
showMaximized();
} else{
this->setWindowIcon(QIcon(""));
this->setWindowTitle("");
showNormal();
}
}
void Widget::on_showImg_pressed()
{
//ui->imageLable->hide();
marioGif->start();
}
void Widget::on_showImg_released()
{
marioGif->stop();
//ui->imageLable->show();
}
void Widget::on_setImgBtn_pressed()
{
QPixmap pixmap2;
pixmap2.load(":/images/sunny");
ui->showImg->setText("");
ui->showImg->setIconSize(QSize(200,200));
ui->showImg->setIcon(pixmap2);
}
void Widget::on_setImgBtn_released()
{
ui->showImg->setText("图片运输中");
ui->showImg->setIcon(QPixmap());
}
void Widget::on_setbgBtn_clicked()
{
// setStyleSheet("background-color: red; color: white; font-size: 16px;");
//setStyleSheet("background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #FF0000, stop:0.17 #FF7F00, stop:0.33 #FFFF00, stop:0.5 #00FF00, stop:0.67 #0000FF, stop:0.83 #4B0082, stop:1 #8B00FF);");
//QString styleSheet = "background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0.5 #000000, stop:0 #FF00F0,stop:1 #0F0F0F);";
//setStyleSheet(styleSheet);
showFullScreen();
}

49
day12/qtdemo3/widget.h Executable file
View File

@ -0,0 +1,49 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMovie>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
private:
int n;
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_hotBtn_clicked();
void on_coldBtn_clicked();
void on_coldBtn_2_clicked();
void on_setTitleIconBtn_clicked();
void on_setImgBtn_clicked();
void on_showImg_pressed();
void on_showImg_released();
void on_setImgBtn_pressed();
void on_setImgBtn_released();
void on_setbgBtn_clicked(bool checked);
void on_setbgBtn_clicked();
private:
Ui::Widget *ui;
QMovie *marioGif;
};
#endif // WIDGET_H

216
day12/qtdemo3/widget.ui Executable file
View File

@ -0,0 +1,216 @@
<?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>654</width>
<height>476</height>
</rect>
</property>
<property name="cursor">
<cursorShape>OpenHandCursor</cursorShape>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<property name="windowIcon">
<iconset resource="res.qrc">
<normaloff>:/images/open</normaloff>:/images/open</iconset>
</property>
<widget class="QPushButton" name="hotBtn">
<property name="geometry">
<rect>
<x>170</x>
<y>8</y>
<width>201</width>
<height>81</height>
</rect>
</property>
<property name="cursor">
<cursorShape>ClosedHandCursor</cursorShape>
</property>
<property name="styleSheet">
<string notr="true">font: 36pt &quot;华文隶书&quot;;
alternate-background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(9, 41, 4, 255), stop:0.085 rgba(2, 79, 0, 255), stop:0.19 rgba(50, 147, 22, 255), stop:0.275 rgba(236, 191, 49, 255), stop:0.39 rgba(243, 61, 34, 255), stop:0.555 rgba(135, 81, 60, 255), stop:0.667 rgba(121, 75, 255, 255), stop:0.825 rgba(164, 255, 244, 255), stop:0.885 rgba(104, 222, 71, 255), stop:1 rgba(93, 128, 0, 255));
color: rgb(255, 85, 255);</string>
</property>
<property name="text">
<string>热了</string>
</property>
<property name="iconSize">
<size>
<width>37</width>
<height>55</height>
</size>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>110</y>
<width>191</width>
<height>69</height>
</rect>
</property>
<property name="cursor">
<cursorShape>WhatsThisCursor</cursorShape>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 170, 255);
font: 18pt &quot;华文琥珀&quot;;</string>
</property>
<property name="text">
<string>出门买啥?</string>
</property>
</widget>
<widget class="QLabel" name="whatLable">
<property name="geometry">
<rect>
<x>230</x>
<y>120</y>
<width>361</width>
<height>141</height>
</rect>
</property>
<property name="cursor">
<cursorShape>SizeBDiagCursor</cursorShape>
</property>
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 85, 0);
color: rgb(255, 255, 255);
background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 0, 0, 255), stop:0.166 rgba(255, 255, 0, 255), stop:0.333 rgba(0, 255, 0, 255), stop:0.5 rgba(0, 255, 255, 255), stop:0.666 rgba(0, 0, 255, 255), stop:0.833 rgba(255, 0, 255, 255), stop:1 rgba(255, 0, 0, 255));
font: 72pt &quot;华文彩云&quot;;</string>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QPushButton" name="coldBtn">
<property name="geometry">
<rect>
<x>410</x>
<y>10</y>
<width>201</width>
<height>81</height>
</rect>
</property>
<property name="cursor">
<cursorShape>CrossCursor</cursorShape>
</property>
<property name="styleSheet">
<string notr="true">font: 36pt &quot;华文隶书&quot;;
selection-background-color: rgb(255, 170, 127);
color: rgb(170, 255, 255);</string>
</property>
<property name="text">
<string>冻着了</string>
</property>
</widget>
<widget class="QLabel" name="imageLable">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>20</x>
<y>260</y>
<width>121</width>
<height>121</height>
</rect>
</property>
<property name="cursor">
<cursorShape>WaitCursor</cursorShape>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QPushButton" name="setImgBtn">
<property name="geometry">
<rect>
<x>240</x>
<y>300</y>
<width>191</width>
<height>49</height>
</rect>
</property>
<property name="cursor">
<cursorShape>SizeHorCursor</cursorShape>
</property>
<property name="styleSheet">
<string notr="true">font: 28pt &quot;华文隶书&quot;;
</string>
</property>
<property name="text">
<string>设置图片</string>
</property>
</widget>
<widget class="QPushButton" name="setTitleIconBtn">
<property name="geometry">
<rect>
<x>240</x>
<y>400</y>
<width>191</width>
<height>49</height>
</rect>
</property>
<property name="cursor">
<cursorShape>UpArrowCursor</cursorShape>
</property>
<property name="styleSheet">
<string notr="true">font: 18pt &quot;华文隶书&quot;;
</string>
</property>
<property name="text">
<string>标题Icon修改</string>
</property>
</widget>
<widget class="QPushButton" name="showImg">
<property name="geometry">
<rect>
<x>440</x>
<y>280</y>
<width>201</width>
<height>181</height>
</rect>
</property>
<property name="cursor">
<cursorShape>ForbiddenCursor</cursorShape>
</property>
<property name="text">
<string>图片正在派送中</string>
</property>
</widget>
<widget class="QPushButton" name="setbgBtn">
<property name="geometry">
<rect>
<x>20</x>
<y>40</y>
<width>141</width>
<height>49</height>
</rect>
</property>
<property name="cursor">
<cursorShape>UpArrowCursor</cursorShape>
</property>
<property name="styleSheet">
<string notr="true">font: 18pt &quot;华文隶书&quot;;
</string>
</property>
<property name="text">
<string>背景修改</string>
</property>
</widget>
</widget>
<resources>
<include location="res.qrc"/>
</resources>
<connections/>
</ui>

11
day12/qtdemo4/main.cpp Executable file
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();
}

BIN
day12/qtdemo4/qtdemo4 Executable file

Binary file not shown.

34
day12/qtdemo4/qtdemo4.pro Executable file
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
day12/qtdemo4/res.qrc Executable file
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

45
day12/qtdemo4/widget.cpp Executable file
View File

@ -0,0 +1,45 @@
#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;
if(guessNum == randNum){
ui->resaultShowLabel->setMovie(yanhuagif);
yanhuagif->start();
} else {
if(yanhuagif->state() == QMovie::Running)
yanhuagif->stop();
ui->resaultShowLabel->setText("你猜错了, 正确的结果是:"+ QString::number(randNum));
}
}

32
day12/qtdemo4/widget.h Executable file
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

103
day12/qtdemo4/widget.ui Executable file
View File

@ -0,0 +1,103 @@
<?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>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="resaultShowLabel">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>