Qt修炼手册7_图形:用户自定义QGraphicsItem
生活随笔
收集整理的這篇文章主要介紹了
Qt修炼手册7_图形:用户自定义QGraphicsItem
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.前言
Qt中提供的Item未必能夠滿足需要,因此有必要實(shí)現(xiàn)自定義的QGraphicsItem對(duì)象。與QPushButton一樣,如果發(fā)生鼠標(biāo)事件,那么為了更換被點(diǎn)擊按鈕的圖像,可以使用paint實(shí)現(xiàn)用戶自定義QGraphicsItem。
2.一個(gè)簡單的實(shí)驗(yàn)
#include <QtWidgets/QApplication> #include "MyItem.h" #include <qgraphicsscene.h> #include <qgraphicsview.h>#define WIDTH 300 #define HEIGHT 200int main(int argc, char *argv[]) {QApplication a(argc, argv);QGraphicsScene scene;MyItem* item;item = new MyItem("button.png","button-active");item->setPos(100,100);scene.addItem(item);QGraphicsView view;view.setScene(&scene);view.setFrameShape(QFrame::NoFrame);view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);view.setBackgroundBrush(Qt::black);view.show();return a.exec(); }
#pragma once #include <QObject> #include <qgraphicsitem.h> #include <qpainter.h> #include <qgraphicssceneevent.h>class MyItem :public QObject, public QGraphicsItem {Q_OBJECTQ_INTERFACES(QGraphicsItem) //通知要實(shí)現(xiàn)的類構(gòu)成何種界面的宏 public:MyItem(QGraphicsItem *parent = 0 );MyItem(const QString normal,const QString pressed = "",QGraphicsItem *parent = 0);//繪圖區(qū)域QRectF boundingRect() const;//繪制按鈕圖像的函數(shù)void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget = 0);//加載圖像void setPixmap(const QString normal,const QString pressed = "");~MyItem();signals:void clicked(); //信號(hào) protected: //事件void mousePressEvent(QGraphicsSceneMouseEvent* event);void mouseMoveEvent(QGraphicsSceneMouseEvent* event);void mouseReleaseEvent(QGraphicsSceneMouseEvent* event); private:QPixmap m_normal; //按鈕圖像QPixmap m_pressed; //按鈕按下后的狀態(tài)bool m_isPressed; //查看按鈕是否處于點(diǎn)擊狀態(tài)的變量。 }; #include "MyItem.h" MyItem::MyItem(const QString normal,const QString pressed ,QGraphicsItem *parent):QGraphicsItem(parent),m_isPressed(false) {//構(gòu)造函數(shù)調(diào)用加載圖像的類的成員函數(shù)setPixmapsetPixmap(normal,pressed); } void MyItem::setPixmap(QString normal, QString pressed) {if( !m_normal.isNull() ){m_normal.detach(); //QPixmap的成員函數(shù)detach()用于控制QPixmap實(shí)例共享的Pixmap數(shù)據(jù)m_pressed.detach();}m_normal.load(normal);m_pressed.load(pressed); } void MyItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {if( event->button() == Qt::LeftButton ){if( contains( event->pos()) )m_isPressed = true;update(); //調(diào)用update()函數(shù),則必須調(diào)用要實(shí)現(xiàn)的paint()函數(shù)。} } void MyItem::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget) {//paint函數(shù)將QPixmap存儲(chǔ)的圖像繪制到QGraphicsScene。Q_UNUSED(option);Q_UNUSED(widget);if (m_isPressed)painter->drawPixmap(0,0,m_pressed);elsepainter->drawPixmap(0,0,m_normal); } void MyItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {if ( event->button() == Qt::LeftButton ){m_isPressed = false;update();if(contains(event->pos()))emit clicked(); //發(fā)送頭文件聲明的clicked()信號(hào)以觸發(fā)事件} }void MyItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {if (event->buttons() & Qt::LeftButton){if (contains(event->pos())){m_isPressed = true;}else{m_isPressed = false;}update();} }QRectF MyItem::boundingRect() const {return m_normal.rect(); }MyItem::~MyItem(void) { }
總結(jié)
以上是生活随笔為你收集整理的Qt修炼手册7_图形:用户自定义QGraphicsItem的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深圳南山区法院受理11人集体诉腾讯案
- 下一篇: 主成分分析的数学原理