这是在一个网友的不段要求下要写的一个小教程。如果你是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="",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。一个好看的按钮就好了。。。。