1. 程式人生 > >QT教程 休閒棋牌遊戲開發(1)

QT教程 休閒棋牌遊戲開發(1)

      這是在一個網友的不段要求下要寫的一個小教程。如果你是Qt大鳥請繞道,如果你是Qt新手甚至都不會用QT來寫程式也請先百度一下,先入個門再回來。如果。。你連C++基礎都沒搞定,那請該幹嘛就幹嘛去!!

 開發工具Qt Creator + qt 4.7 可到http://qt.nokia.com/downloads 下載LGPL和對應你所用的系統的那個版本。。

     OK,費話完。那下面開始進入正題。先來兩張圖表達一下這教程的意圖,一圖勝於千言萬語。

這是小弟在某個專案的介面。本教程就教您怎麼實現這些介面效果。

       開始了----------

先來實現一個好看的按鈕類。小二開始上菜了。

mybutton.h

  1. /*/////////////////////////////////////////////////////////////// 
  2. 2011年。寫於日本地震前幾天。。。。 
  3. 作者CK。。。 
  4. QQ:78961410.. 
  5. 老婆淘寶:yoyock.taobao.com - -.... 
  6. ///////////////////////////////////////////////////////////////*/
  7. #ifndef MYBUTTON_H
  8. #define MYBUTTON_H
  9. #include <QAbstractButton>
  10. #include <qpixmap>
  11. class MyButton : public QAbstractButton  
  12. {  
  13.     Q_OBJECT  
  14. public:  
  15.     explicit MyButton(QWidget* parent,QString str1,QString str2=
    "",QString str3="",QString str4="");  
  16.     virtualvoid paintEvent(QPaintEvent * e);  
  17.     virtualvoid enterEvent(QEvent * e);  
  18.     virtualvoid leaveEvent(QEvent * e);  
  19. private:  
  20.     //四張圖片代表按鈕的四個狀態,1,預設狀態。2,滑鼠移動狀態。3,滑鼠按下狀態,4,按鈕不可用狀態。
  21.     QPixmap m_arrPixmap[4];  
  22.     int m_iTypeTotal;  
  23.     int m_iType;  
  24. };  
  25. #endif // MYBUTTON_H

mybutton.cpp

  1. /*/////////////////////////////////////////////////////////////// 
  2. 2011年。寫於日本地震前幾天。。。。 
  3. 作者CK。。。 
  4. QQ:78961410.. 
  5. 老婆淘寶:yoyock.taobao.com - -.... 
  6. ///////////////////////////////////////////////////////////////*/
  7. #include "mybutton.h"
  8. #include <QPainter>
  9. MyButton::MyButton(QWidget* parent,QString str1,QString str2,QString str3,QString str4) :  
  10.     QAbstractButton(parent)  
  11.     ,m_iTypeTotal(0)  
  12.     ,m_iType(1)  
  13. {  
  14.     if(str1 != "")  
  15.     {  
  16.         m_arrPixmap[0] = QPixmap(str1);  
  17.         m_iTypeTotal++;  
  18.     }  
  19.     if(str2 != "")  
  20.     {  
  21.         m_arrPixmap[1] = QPixmap(str2);  
  22.         m_iTypeTotal++;  
  23.     }  
  24.     if(str3 != "")  
  25.     {  
  26.         m_arrPixmap[2] = QPixmap(str3);  
  27.         m_iTypeTotal++;  
  28.     }  
  29.     if(str4 != "")  
  30.     {  
  31.         m_arrPixmap[3] = QPixmap(str4);  
  32.         m_iTypeTotal++;  
  33.     }  
  34.     this->setGeometry(0,0,m_arrPixmap[0].width(),m_arrPixmap[0].height());  
  35. }  
  36. void MyButton::paintEvent(QPaintEvent * e )  
  37. {  
  38.     if(this->isDown())  
  39.         m_iType = 3;  
  40.     if(!this->isEnabled())  
  41.         m_iType = 4;  
  42.     QPainter painter(this);  
  43.     switch(m_iTypeTotal)  
  44.     {  
  45.     case 1:  
  46.         {  
  47.             painter.drawPixmap(0,0,m_arrPixmap[0]);  
  48.         }  
  49.         break;  
  50.     case 2:  
  51.         {  
  52.             if(m_iType == 2)  
  53.                 painter.drawPixmap(0,0,m_arrPixmap[2]);  
  54.             else
  55.                 painter.drawPixmap(0,0,m_arrPixmap[0]);  
  56.         }  
  57.         break;  
  58.     case 3:  
  59.         {  
  60.             if(m_iType <=3 )  
  61.             painter.drawPixmap(0,0,m_arrPixmap[m_iType-1]);  
  62.             else
  63.                 painter.drawPixmap(0,0,m_arrPixmap[0]);  
  64.         }  
  65.         break;  
  66.     case 4:  
  67.         {  
  68.             painter.drawPixmap(0,0,m_arrPixmap[m_iType-1]);  
  69.         }  
  70.         break;  
  71.     }  
  72. }  
  73. void MyButton::enterEvent(QEvent * e)  
  74. {  
  75.     m_iType = 2;  
  76.     update();  
  77. }  
  78. void MyButton::leaveEvent(QEvent * e)  
  79. {  
  80.     m_iType = 1;  
  81.     update();  
  82. }  

mainwindow.h

  1. /*/////////////////////////////////////////////////////////////// 
  2. 2011年。寫於日本地震前幾天。。。。 
  3. 作者CK。。。 
  4. QQ:78961410.. 
  5. 老婆淘寶:yoyock.taobao.com - -.... 
  6. ///////////////////////////////////////////////////////////////*/
  7. #ifndef MAINWINDOW_H
  8. #define MAINWINDOW_H
  9. #include <QMainWindow>
  10. class MainWindow : public QMainWindow  
  11. {  
  12.     Q_OBJECT  
  13. public:  
  14.     explicit MainWindow(QWidget *parent = 0);  
  15.     ~MainWindow();  
  16. };  
  17. #endif // MAINWINDOW_H

mainwindow.cpp

  1. /*/////////////////////////////////////////////////////////////// 
  2. 2011年。寫於日本地震前幾天。。。。 
  3. 作者CK。。。 
  4. QQ:78961410.. 
  5. 老婆淘寶:yoyock.taobao.com - -.... 
  6. ///////////////////////////////////////////////////////////////*/
  7. #include "mainwindow.h"
  8. #include "mybutton.h"
  9. MainWindow::MainWindow(QWidget *parent) :  
  10.     QMainWindow(parent)  
  11. {  
  12.     MyButton *b = new MyButton(this,QString("./syg_1.png"),  
  13.                       QString("./syg_2.png"),QString("./syg_3.png"),  
  14.                       QString("./syg_4.png"));  
  15.     b->move(50,50);  
  16.     b->setEnabled(false);  
  17.     b->show();  
  18.     MyButton *b1 = new MyButton(this,QString("./syg_1.png"),  
  19.                       QString("./syg_2.png"),QString("./syg_3.png"),  
  20.                       QString("./syg_4.png"));  
  21.     b1->move(50,100);  
  22.     b1->show();  
  23. }  
  24. MainWindow::~MainWindow()  
  25. {  
  26. }  

main.cpp

  1. /*/////////////////////////////////////////////////////////////// 
  2. 2011年。寫於日本地震前幾天。。。。 
  3. 作者CK。。。 
  4. QQ:78961410.. 
  5. 老婆淘寶:yoyock.taobao.com - -.... 
  6. ///////////////////////////////////////////////////////////////*/
  7. #include <QtGui/QApplication>
  8. #include "mainwindow.h"
  9. int main(int argc, char *argv[])  
  10. {  
  11.     QApplication a(argc, argv);  
  12.     MainWindow w;  
  13.     w.showMaximized();  
  14.     return a.exec();  
  15. }  
<