QT教程 休閒棋牌遊戲開發(1)
阿新 • • 發佈:2019-01-23
這是在一個網友的不段要求下要寫的一個小教程。如果你是Qt大鳥請繞道,如果你是Qt新手甚至都不會用QT來寫程式也請先百度一下,先入個門再回來。如果。。你連C++基礎都沒搞定,那請該幹嘛就幹嘛去!!
開發工具Qt Creator + qt 4.7 可到http://qt.nokia.com/downloads 下載LGPL和對應你所用的系統的那個版本。。
OK,費話完。那下面開始進入正題。先來兩張圖表達一下這教程的意圖,一圖勝於千言萬語。
這是小弟在某個專案的介面。本教程就教您怎麼實現這些介面效果。
開始了----------
先來實現一個好看的按鈕類。小二開始上菜了。
mybutton.h
- /*///////////////////////////////////////////////////////////////
- 2011年。寫於日本地震前幾天。。。。
- 作者CK。。。
- QQ:78961410..
- 老婆淘寶:yoyock.taobao.com - -....
- ///////////////////////////////////////////////////////////////*/
- #ifndef MYBUTTON_H
- #define MYBUTTON_H
- #include <QAbstractButton>
- #include <qpixmap>
- class MyButton : public QAbstractButton
- {
- Q_OBJECT
- public:
-
explicit MyButton(QWidget* parent,QString str1,QString str2=
- virtualvoid paintEvent(QPaintEvent * e);
- virtualvoid enterEvent(QEvent * e);
- virtualvoid leaveEvent(QEvent * e);
- private:
- //四張圖片代表按鈕的四個狀態,1,預設狀態。2,滑鼠移動狀態。3,滑鼠按下狀態,4,按鈕不可用狀態。
- QPixmap m_arrPixmap[4];
- int m_iTypeTotal;
-
int m_iType;
- };
- #endif // MYBUTTON_H
mybutton.cpp
- /*///////////////////////////////////////////////////////////////
- 2011年。寫於日本地震前幾天。。。。
- 作者CK。。。
- QQ:78961410..
- 老婆淘寶:yoyock.taobao.com - -....
- ///////////////////////////////////////////////////////////////*/
- #include "mybutton.h"
- #include <QPainter>
- MyButton::MyButton(QWidget* parent,QString str1,QString str2,QString str3,QString str4) :
- QAbstractButton(parent)
- ,m_iTypeTotal(0)
- ,m_iType(1)
- {
- if(str1 != "")
- {
- m_arrPixmap[0] = QPixmap(str1);
- m_iTypeTotal++;
- }
- if(str2 != "")
- {
- m_arrPixmap[1] = QPixmap(str2);
- m_iTypeTotal++;
- }
- if(str3 != "")
- {
- m_arrPixmap[2] = QPixmap(str3);
- m_iTypeTotal++;
- }
- if(str4 != "")
- {
- m_arrPixmap[3] = QPixmap(str4);
- m_iTypeTotal++;
- }
- this->setGeometry(0,0,m_arrPixmap[0].width(),m_arrPixmap[0].height());
- }
- void MyButton::paintEvent(QPaintEvent * e )
- {
- if(this->isDown())
- m_iType = 3;
- if(!this->isEnabled())
- m_iType = 4;
- QPainter painter(this);
- switch(m_iTypeTotal)
- {
- case 1:
- {
- painter.drawPixmap(0,0,m_arrPixmap[0]);
- }
- break;
- case 2:
- {
- if(m_iType == 2)
- painter.drawPixmap(0,0,m_arrPixmap[2]);
- else
- painter.drawPixmap(0,0,m_arrPixmap[0]);
- }
- break;
- case 3:
- {
- if(m_iType <=3 )
- painter.drawPixmap(0,0,m_arrPixmap[m_iType-1]);
- else
- painter.drawPixmap(0,0,m_arrPixmap[0]);
- }
- break;
- case 4:
- {
- painter.drawPixmap(0,0,m_arrPixmap[m_iType-1]);
- }
- break;
- }
- }
- void MyButton::enterEvent(QEvent * e)
- {
- m_iType = 2;
- update();
- }
- void MyButton::leaveEvent(QEvent * e)
- {
- m_iType = 1;
- update();
- }
mainwindow.h
- /*///////////////////////////////////////////////////////////////
- 2011年。寫於日本地震前幾天。。。。
- 作者CK。。。
- QQ:78961410..
- 老婆淘寶:yoyock.taobao.com - -....
- ///////////////////////////////////////////////////////////////*/
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
- };
- #endif // MAINWINDOW_H
mainwindow.cpp
- /*///////////////////////////////////////////////////////////////
- 2011年。寫於日本地震前幾天。。。。
- 作者CK。。。
- QQ:78961410..
- 老婆淘寶:yoyock.taobao.com - -....
- ///////////////////////////////////////////////////////////////*/
- #include "mainwindow.h"
- #include "mybutton.h"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent)
- {
- MyButton *b = new MyButton(this,QString("./syg_1.png"),
- QString("./syg_2.png"),QString("./syg_3.png"),
- QString("./syg_4.png"));
- b->move(50,50);
- b->setEnabled(false);
- b->show();
- MyButton *b1 = new MyButton(this,QString("./syg_1.png"),
- QString("./syg_2.png"),QString("./syg_3.png"),
- QString("./syg_4.png"));
- b1->move(50,100);
- b1->show();
- }
- MainWindow::~MainWindow()
- {
- }
main.cpp
- /*///////////////////////////////////////////////////////////////
- 2011年。寫於日本地震前幾天。。。。
- 作者CK。。。
- QQ:78961410..
- 老婆淘寶:yoyock.taobao.com - -....
- ///////////////////////////////////////////////////////////////*/
- #include <QtGui/QApplication>
- #include "mainwindow.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.showMaximized();
- return a.exec();
- }