QT教程 休闲棋牌游戏开发(1)
2011-03-17 11:22
260 查看
这是在一个网友的不段要求下要写的一个小教程。如果你是Qt大鸟请绕道,如果你是Qt新手甚至都不会用QT来写程序也请先百度一下,先入个门再回来。如果。。你连C++基础都没搞定,那请该干嘛就干嘛去!!
开发工具Qt Creator + qt 4.7 可到http://qt.nokia.com/downloads 下载LGPL和对应你所用的系统的那个版本。。
OK,费话完。那下面开始进入正题。先来两张图表达一下这教程的意图,一图胜于千言万语。
这是小弟在某个项目的界面。本教程就教您怎么实现这些界面效果。
开始了----------
先来实现一个好看的按钮类。小二开始上菜了。
mybutton.h
mybutton.cpp
mainwindow.h
mainwindow.cpp
main.cpp
要用到的图片
syg_1.png
syg_2.png
syg_3.png
syg_4.png
四个状态。四个图片。。
程序编译完运行后如下图:
OK。一个好看的按钮就好了。。。。
开发工具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="",QString str3="",QString str4=""); virtual void paintEvent(QPaintEvent * e); virtual void enterEvent(QEvent * e); virtual void 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(); }
要用到的图片
syg_1.png
syg_2.png
syg_3.png
syg_4.png
四个状态。四个图片。。
程序编译完运行后如下图:
OK。一个好看的按钮就好了。。。。
相关文章推荐
- QT教程 休闲棋牌游戏开发(2)
- QT教程 休闲棋牌游戏开发(1)
- QT教程 休闲棋牌游戏开发(2)
- 浪人棋牌游戏开发教程
- QT开发环境安装配置教程
- 【Visual C++】游戏开发笔记四十七 浅墨DirectX教程十五 翱翔于三维世界:摄像机的实现
- WPF/Silverlight下动画/游戏开发教程(Game Course)专题
- 游戏控制杆OUYA游戏开发快速入门教程
- Unity3D系列教程--使用免费工具在Unity3D中开发2D游戏 第一节
- Windows8 游戏开发教程-一、Metro UI 和绘图(转)
- Android 3D游戏开发教程
- 正规的棋牌游戏开发公司需要具备哪些条件
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(四十五)制作精美的可任意拖放对象的物品栏及装备栏
- OUYA游戏开发快速入门教程第1章了解OUYA及其设备
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(二十) 第一部分拓展小结篇
- Lua游戏开发教程之时区问题详解
- cocos2d-x游戏开发系列教程-坦克大战游戏关卡选择场景的编写上
- C++ DirectX 游戏开发视频教程 08 资源下载链接
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(二十) 第一部分拓展小结篇
- 【Visual C++】游戏开发五十七 浅墨DirectX教程二十四 打造游戏GUI界面(二)