QT教程 休闲棋牌游戏开发(1)

      这是在一个网友的不段要求下要写的一个小教程。如果你是Qt大鸟请绕道,如果你是Qt新手甚至都不会用QT来写程序也请先百度一下,先入个门再回来。如果。。你连C++基础都没搞定,那请该干嘛就干嘛去!!

 

 开发工具Qt Creator + qt 4.7 可到http://qt.nokia.com/downloads 下载LGPL和对应你所用的系统的那个版本。。

 

     OK,费话完。那下面开始进入正题。先来两张图表达一下这教程的意图,一图胜于千言万语。

 

这是小弟在某个项目的界面。本教程就教您怎么实现这些界面效果。

       开始了----------

 

先来实现一个好看的按钮类。小二开始上菜了。

mybutton.h

[cpp]  view plain copy
  1. /*/// 
  2. 2011年。写于日本地震前几天。。。。 
  3. 作者CK。。。 
  4. QQ:78961410.. 
  5. 老婆淘宝:yoyock.taobao.com - -.... 
  6. ///*/  
  7. #ifndef MYBUTTON_H  
  8. #define MYBUTTON_H  
  9.   
  10. #include <QAbstractButton>  
  11. #include <qpixmap>  
  12. class MyButton : public QAbstractButton  
  13. {  
  14.     Q_OBJECT  
  15. public:  
  16.     explicit MyButton(QWidget* parent,QString str1,QString str2="",QString str3="",QString str4="");  
  17.     virtual void paintEvent(QPaintEvent * e);  
  18.     virtual void enterEvent(QEvent * e);  
  19.     virtual void leaveEvent(QEvent * e);  
  20. private:  
  21.     //四张图片代表按钮的四个状态,1,默认状态。2,鼠标移动状态。3,鼠标按下状态,4,按钮不可用状态。  
  22.     QPixmap m_arrPixmap[4];  
  23.     int m_iTypeTotal;  
  24.     int m_iType;  
  25.   
  26. };  
  27.   
  28. #endif // MYBUTTON_H  

mybutton.cpp

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

mainwindow.h

[cpp]  view plain copy
  1. /*/// 
  2. 2011年。写于日本地震前几天。。。。 
  3. 作者CK。。。 
  4. QQ:78961410.. 
  5. 老婆淘宝:yoyock.taobao.com - -.... 
  6. ///*/  
  7.   
  8. #ifndef MAINWINDOW_H  
  9. #define MAINWINDOW_H  
  10.   
  11. #include <QMainWindow>  
  12.   
  13. class MainWindow : public QMainWindow  
  14. {  
  15.     Q_OBJECT  
  16.   
  17. public:  
  18.     explicit MainWindow(QWidget *parent = 0);  
  19.     ~MainWindow();  
  20.   
  21. };  
  22.   
  23. #endif // MAINWINDOW_H  

mainwindow.cpp

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

main.cpp

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

 

要用到的图片

syg_1.png

syg_2.png

syg_3.png

syg_4.png

四个状态。四个图片。。

 

程序编译完运行后如下图:

OK。一个好看的按钮就好了。。。。

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值