天天看點

Qt-寫一個模版出來

為啥要寫一個模版了,原因就是有到多需要用到了,哈哈哈哈。

是這樣滴,一套模拟器上面會有N多個裝置,這些裝置互相聯合又互相獨立工作。我呢,需要給每個裝置多寫一個配置工具,最後需要把這些工具整理成一套系統的配置工具。是以,每個配置工具除了内容不同,界面風格應該相同的,就有這個模版了。

萬惡的CSDN穿圖檔的大小還是不可以太大,隻能貼截圖了。

Qt-寫一個模版出來

第一個是系統的啟動動畫,這個有點大 ,沒法預覽,制作方式

1. 網上找好看的AE特效模版

2. 把自己的内容替換上去

3. 導出視屏

4. 用PR轉成GIF

這樣就可了

至于真麼加載這個動畫,看代碼

#include "mainwindow.h"
#include <QApplication>
#include <QSplashScreen>
#include <QPixmap>
#include <QLabel>
#include <QMovie>
#include <QThread>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPixmap pixmap("./images/login.gif");
    QSplashScreen splash(pixmap);
    QMovie *movie = new QMovie("./images/login.gif");
    QLabel *label = new QLabel(&splash);
    label->setMovie(movie);
    movie->start();
    splash.show();
    for(int i = 0;i<11000;i+=movie->speed())
    {
        QCoreApplication::processEvents();
        QThread::usleep(500*movie->speed());
    }
 
    a.setStyleSheet("QLineEdit {border-radius: 4px;height: 25px;border: 1px solid rgb(100, 100, 100);background: rgb(72, 72, 73);}"
                    "QLineEdit:enabled {color: rgb(175, 175, 175);}"
                    "QLineEdit:enabled:hover, QLineEdit:enabled:focus {color: rgb(230, 230, 230);}"
                    "QLineEdit:!enabled {color: rgb(155, 155, 155);}"
 
                    "QPushButton{border-radius: 4px;border: none;width: 75px;height: 25px;}"
                    "QPushButton:enabled {background: rgb(68, 69, 73);color: white;}"
                    "QPushButton:!enabled {background: rgb(100, 100, 100);color: rgb(200, 200, 200);}"
                    "QPushButton:enabled:hover{background: rgb(85, 85, 85);}"
                    "QPushButton:enabled:pressed{background: rgb(80, 80, 80);}"
                    //關閉按鈕樣式
                    "QPushButton#pushButton_close{border-radius: 0px;background: rgb(0,0,0,0,);border-image:url(./images/close_n.png);}"
                    "QPushButton::hover#pushButton_close{border-image:url(./images/close_p.png);}"
                    "QPushButton::pressed #pushButton_close{border-image:url(./images/close_n.png);}"
                    //最小化按鈕樣式
                    "QPushButton#pushButton_min{border-radius: 0px;background: rgb(0,0,0,0,);border-image:url(./images/min_n.png);}"
                    "QPushButton::hover#pushButton_min{border-image:url(./images/min_p.png);}"
                    "QPushButton::pressed #pushButton_min{border-image:url(./images/min_n.png);}"
                    //關于按鈕樣式
                    "QPushButton#pushButton_about{border-radius: 0px;background: rgb(0,0,0,0,);border-image:url(./images/about_n.png);}"
                    "QPushButton::hover#pushButton_about{border-image:url(./images/about_p.png);}"
                    "QPushButton::pressed #pushButton_about{border-image:url(./images/about_n.png);}"
 
                    "QLabel {color: rgb(175, 175, 175);}"
 
                    );
 
 
    MainWindow w;
    w.show();
    splash.finish(&w);
    return a.exec();
}      

中間有部分是樣式表,自動忽略就好

Qt-寫一個模版出來

上面的這個圖畫就是我的基本模版了,

做了一下工作

1. 隐藏了原有的标題欄

2. 左上角增加了圖示和标題

3. 右上角增加了關于、最小化和關閉按鈕

4. 限制了滑鼠點選點選區域

上下的看代碼

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkDatagram>
#include <QMessageBox>
#include <windows.h>
#include <QDebug>
/*
 * 設定标題欄寬度
 * 後面滑鼠事件會用到
 */
const int TITLE_HEIGHT = 30;
/*
 * 構造函數
 */
MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setWindowIcon(QIcon("./images/logo.ico"));
    ui->pushButton_about->setToolTip("關于我們");
    ui->pushButton_close->setToolTip("關閉");
    ui->pushButton_min->setToolTip("最小化");
    ui->label_ICON->setStyleSheet("QLabel{border-image:url(./images/logo.ico);}");
    ui->label_title->setStyleSheet("QLabel{color: rgb(255, 255, 255);border:0px;}");
    ui->label_title->setText("M1J1DNT701");
}
/*
 * 析構函數
 */
MainWindow::~MainWindow()
{
    delete ui;
}
/*
 * 繪制背景
 */
void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter_back(this);
    painter_back.drawPixmap(0,0,this->width(),this->height(),QPixmap("./images/mainback.png"));
}
//存放目前滑鼠位置函數
static QPoint last(0,0);
/*
 * 滑鼠按下函數
 */
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->y()< TITLE_HEIGHT)
    {
        last = event->globalPos();
    }
}
/*
 * 滑鼠移動函數
 */
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(event->y() < TITLE_HEIGHT)
    {
        int dx = event->globalX() - last.x();
        int dy = event->globalY() - last.y();
        last = event->globalPos();
        this->move(x()+dx,y()+dy);
    }
}
/*
 * 滑鼠釋放函數
 */
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->y() < TITLE_HEIGHT)
    {
        int dx = event->globalX() - last.x();
        int dy = event->globalY() - last.y();
        this->move(x()+dx,y()+dy);
    }
}
 
 
/*
 * 關閉按鈕槽函數
 */
void MainWindow::on_pushButton_close_clicked()
{
    this->close();
}      

好了,暫時就這些