Qt修炼手册7_图形:用户自定义QGraphicsItem
生活随笔
收集整理的這篇文章主要介紹了
Qt修炼手册7_图形:用户自定义QGraphicsItem
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.前言
Qt中提供的Item未必能夠滿足需要,因此有必要實現自定義的QGraphicsItem對象。與QPushButton一樣,如果發生鼠標事件,那么為了更換被點擊按鈕的圖像,可以使用paint實現用戶自定義QGraphicsItem。
2.一個簡單的實驗
#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) //通知要實現的類構成何種界面的宏 public:MyItem(QGraphicsItem *parent = 0 );MyItem(const QString normal,const QString pressed = "",QGraphicsItem *parent = 0);//繪圖區域QRectF boundingRect() const;//繪制按鈕圖像的函數void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget = 0);//加載圖像void setPixmap(const QString normal,const QString pressed = "");~MyItem();signals:void clicked(); //信號 protected: //事件void mousePressEvent(QGraphicsSceneMouseEvent* event);void mouseMoveEvent(QGraphicsSceneMouseEvent* event);void mouseReleaseEvent(QGraphicsSceneMouseEvent* event); private:QPixmap m_normal; //按鈕圖像QPixmap m_pressed; //按鈕按下后的狀態bool m_isPressed; //查看按鈕是否處于點擊狀態的變量。 }; #include "MyItem.h" MyItem::MyItem(const QString normal,const QString pressed ,QGraphicsItem *parent):QGraphicsItem(parent),m_isPressed(false) {//構造函數調用加載圖像的類的成員函數setPixmapsetPixmap(normal,pressed); } void MyItem::setPixmap(QString normal, QString pressed) {if( !m_normal.isNull() ){m_normal.detach(); //QPixmap的成員函數detach()用于控制QPixmap實例共享的Pixmap數據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(); //調用update()函數,則必須調用要實現的paint()函數。} } void MyItem::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget) {//paint函數將QPixmap存儲的圖像繪制到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(); //發送頭文件聲明的clicked()信號以觸發事件} }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) { }
總結
以上是生活随笔為你收集整理的Qt修炼手册7_图形:用户自定义QGraphicsItem的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深圳南山区法院受理11人集体诉腾讯案
- 下一篇: 主成分分析的数学原理