日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt修炼手册7_图形:用户自定义QGraphicsItem

發布時間:2025/3/15 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。