昆仑山:眼中无形心中有穴之穴人合一

夫君子之行,静以修身,俭以养德;非澹泊无以明志,非宁静无以致远。夫学须静也,才须学也;非学无以广才,非志无以成学。怠慢则不能励精,险躁则不能冶性。年与时驰,意与岁去,遂成枯落,多不接世。悲守穷庐,将复何及!

 

Qt编程艺术(打地鼠)

Qt 官网

https://download.qt.io/archive/qt/

Qt 加速编译操作技巧

打地鼠工程目录结构图

打地鼠添加mouse

打地鼠添加mythread

打地鼠图片导入工艺流程

打地鼠音频导入工艺流程

打地鼠背景图载入 工艺流程

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include<QIcon>
#include<QImage>
#include<QPainter>
#include<QRect>




namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    QImage BackImag;
    QPainter painter;
protected:
    void paintEvent(QPaintEvent *);
};

#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setMaximumSize(550,500);
    BackImag.load(":/images/000.jpg");
}

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

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QRect source(0,0,BackImag.width(),BackImag.height());
    QRect target(0,0,this->width(),this->height());
    painter.drawImage(target,BackImag,source);

}

6个控件互斥代码实现

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>



namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void StartEnable();
    void StopEnable();


private slots:
    void on_pushButton_clicked();

    void on_actionStop_triggered();

    void on_actionMode3_triggered();

    void on_actionMode2_triggered();

    void on_actionMode1_triggered();

    void on_actionMode4_triggered();

    void on_actionStart_triggered();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<QMessageBox>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

void MainWindow::StartEnable()
{
    ui->actionStart->setEnabled(false);
    ui->actionStop->setEnabled(true);
    ui->actionMode1->setEnabled(false);
    ui->actionMode2->setEnabled(false);
    ui->actionMode3->setEnabled(false);
    ui->actionMode4->setEnabled(false);
    ui->centralWidget->setEnabled(true);

}

void MainWindow::StopEnable()
{
    //按键是否可以按下
    if(ui->lcdNumber_2->intValue() == 0)
    {
        ui->actionStart->setEnabled(true);
    }
    ui->actionStop->setEnabled(false);
    ui->actionMode1->setEnabled(true);
    ui->actionMode2->setEnabled(true);
    ui->actionMode3->setEnabled(true);
    ui->actionMode4->setEnabled(true);
    ui->centralWidget->setEnabled(false);

}

void MainWindow::on_pushButton_clicked()
{

}

void MainWindow::on_actionStop_triggered()
{
    StopEnable();
    update();
    ui->statusBar->showMessage("游戏结束了,准备下局吧!");

}

void MainWindow::on_actionMode3_triggered()
{

}

void MainWindow::on_actionMode2_triggered()
{

}

void MainWindow::on_actionMode1_triggered()
{

}

void MainWindow::on_actionMode4_triggered()
{

}

void MainWindow::on_actionStart_triggered()
{
    StartEnable();
    ui->statusBar->showMessage("挥起你的锤子吧!哇哈哈");

}

背景音乐代码操作流程

#-------------------------------------------------
#
# Project created by QtCreator 2018-12-11T22:58:59
#
#-------------------------------------------------

QT       += core gui
QT       += multimedia

CONFIG += resources_big

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = CheckSongs
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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

CONFIG += c++11

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.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 += \
    images.qrc \
    audio.qrc


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QMediaPlayer>
#include <QMediaPlaylist>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_actionNextMusic_triggered();

    void on_actionmusic1_triggered();

    void on_actionmusic2_triggered();

    void on_actionmusic3_triggered();

    void on_actionmusic4_triggered();

    void on_actionmusic5_triggered();

private:
    Ui::MainWindow *ui;


    //播放音乐参数
    QMediaPlayer *player;

    QMediaPlaylist *playlist;
    int musicindex;
};

#endif // MAINWINDOW_H

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    //背景音乐
    musicindex=0;
    playlist=new QMediaPlaylist ;
    playlist->addMedia(QUrl("D:/project/qt_project/CheckSongs/audio/music1.mp3"));  //绝对路径一般每个人的都不一样
    playlist->addMedia(QUrl("D:/project/qt_project/CheckSongs/audio/music2.mp3"));
    playlist->addMedia(QUrl("D:/project/qt_project/CheckSongs/audio/music3.mp3"));
    playlist->addMedia(QUrl("D:/project/qt_project/CheckSongs/audio/music4.mp3"));
    playlist->addMedia(QUrl("D:/project/qt_project/CheckSongs/audio/music5.mp3"));

    playlist->setCurrentIndex(musicindex);
    player=new QMediaPlayer;
    player->setPlaylist(playlist);
    player->setVolume(100);
    player->play();




}

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

void MainWindow::on_actionNextMusic_triggered()
{
    playlist->setCurrentIndex(++musicindex);
        if(musicindex==4)
        {
            musicindex=-1;
        }
}

void MainWindow::on_actionmusic1_triggered()
{
    //这里注意路径的设置
        playlist->setCurrentIndex(0);
}

void MainWindow::on_actionmusic2_triggered()
{
    playlist->setCurrentIndex(1);
}

void MainWindow::on_actionmusic3_triggered()
{
    playlist->setCurrentIndex(2);
}

void MainWindow::on_actionmusic4_triggered()
{
    playlist->setCurrentIndex(3);
}

void MainWindow::on_actionmusic5_triggered()
{
    playlist->setCurrentIndex(4);
}

定时器代码实现

#-------------------------------------------------
#
# Project created by QtCreator 2018-12-12T00:25:47
#
#-------------------------------------------------

QT       += core gui
QT      += multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = CheckQTimer
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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

CONFIG += c++11

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.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 += \
    images.qrc

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QTimer>
#include <QSound>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void ChangeTimer();

    void on_actionStart_triggered();

private:
    Ui::MainWindow *ui;
    QTimer *timer;
};

#endif // MAINWINDOW_H

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<QLCDNumber>
#include<QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //开启定时功能
        timer = new QTimer(this);
        connect(timer,
                SIGNAL(timeout()),
                this,
                SLOT(ChangeTimer()));
}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::ChangeTimer()
{
    int val =ui->lcdNumber->intValue();

    ui->lcdNumber->display(--val);
    ui->statusBar->showMessage("挥起你的锤子吧!哇哈哈");
}

void MainWindow::on_actionStart_triggered()
{
    timer->start(1000);
}


打地鼠常见问题总结

#-------------------------------------------------
#
# Project created by QtCreator 2018-12-09T14:53:49
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Check
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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

CONFIG += c++11

CONFIG += resources_big

SOURCES += \
        main.cpp \
        mainwindow.cpp \
    mouse.cpp \
    mythread.cpp

HEADERS += \
        mainwindow.h \
    mouse.h \
    mythread.h

FORMS += \
        mainwindow.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 += \
    images.qrc \
    audio.qrc

posted on 2018-07-24 12:06  Indian_Mysore  阅读(1829)  评论(9编辑  收藏  举报

导航