博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt组态图片区域事件响应
阅读量:4060 次
发布时间:2019-05-25

本文共 2743 字,大约阅读时间需要 9 分钟。

相当于N 张图片叠加起来使用,先绘制其他图片,因为只有有效区域的Alpha值为0xff,其他为0x00,这样就不会显示出来,同时不会响应鼠标事件。

必须要先绘制子items,再绘制背景,防止背景将items盖住。

如果items和background items的尺寸一致的话,可以适应窗体的缩放。

#ifndef ZGRAPHICSITEM_H

#define ZGRAPHICSITEM_H
#include <QObject>
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneMouseEvent>
class ZGraphicsItem : public QObject,public QGraphicsPixmapItem
{
    Q_OBJECT
public:
    explicit ZGraphicsItem(QGraphicsItem *parent = 0);
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
signals:
    void ZSignalClicked();
public slots:
};
#endif // ZGRAPHICSITEM_H
#include "zgraphicsitem.h"
#include <QCursor>
ZGraphicsItem::ZGraphicsItem(QGraphicsItem *parent) :
    QGraphicsPixmapItem(parent)
{
}
void ZGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        emit this->ZSignalClicked();
    }
    QGraphicsPixmapItem::mousePressEvent(event);
}

#ifndef ZWIDGET_H

#define ZWIDGET_H
#include <QWidget>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QHBoxLayout>
#include <zgraphicsitem.h>
class ZWidget : public QWidget
{
    Q_OBJECT
public:
    ZWidget(QWidget *parent = 0);
    ~ZWidget();
private slots:
    void ZSlotLeftTopClicked();
private:
    QGraphicsScene *m_Scene;
    QGraphicsView *m_view;
    QVBoxLayout *m_vlayoutMain;
    //background item.
    ZGraphicsItem *m_BackItem;
    ZGraphicsItem *m_LeftTopItem;
    ZGraphicsItem *m_ZiMuCeShiItem;
};
#endif // ZWIDGET_H
#include "zwidget.h"
#include <QDebug>
ZWidget::ZWidget(QWidget *parent)
    : QWidget(parent)
{
    this->m_Scene=new QGraphicsScene;
    this->m_view=new QGraphicsView(this->m_Scene);
    //here we must create action-sensitive item first,
    //at the last draw the background to avoid hide others items.
    this->m_LeftTopItem=new ZGraphicsItem;
    this->m_LeftTopItem->setPixmap(QPixmap(":/images/zuoshangjiao.png"));
    connect(this->m_LeftTopItem,SIGNAL(ZSignalClicked()),this,SLOT(ZSlotLeftTopClicked()));
    this->m_Scene->addItem(this->m_LeftTopItem);
    this->m_ZiMuCeShiItem=new ZGraphicsItem;
    this->m_ZiMuCeShiItem->setPixmap(QPixmap(":/images/zimuceshi.png"));
    connect(this->m_ZiMuCeShiItem,SIGNAL(ZSignalClicked()),this,SLOT(ZSlotLeftTopClicked()));
    this->m_Scene->addItem(this->m_ZiMuCeShiItem);
    //create items.
    this->m_BackItem=new ZGraphicsItem;
    this->m_BackItem->setPixmap(QPixmap(":/images/background.png"));
    this->m_Scene->addItem(this->m_BackItem);
    //layout main.
    this->m_vlayoutMain=new QVBoxLayout;
    this->m_vlayoutMain->addWidget(this->m_view);
    this->setLayout(this->m_vlayoutMain);
}
ZWidget::~ZWidget()
{
    delete this->m_view;
    delete this->m_BackItem;
    delete this->m_LeftTopItem;
    delete this->m_ZiMuCeShiItem;
    delete this->m_Scene;
    delete this->m_vlayoutMain;
}
void ZWidget::ZSlotLeftTopClicked()
{
    static int i=0;
    qDebug()<<"clicked:"<<i++;
}
你可能感兴趣的文章
Vue组件
查看>>
python_time模块
查看>>
python_configparser(解析ini)
查看>>
selenium学习资料
查看>>
<转>文档视图指针互获
查看>>
从mysql中 导出/导入表及数据
查看>>
HQL语句大全(转)
查看>>
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>
javascript传参字符串 与引号的嵌套调用
查看>>
swiper插件的的使用
查看>>
layui插件的使用
查看>>
JS牛客网编译环境的使用
查看>>
9、VUE面经
查看>>
关于进制转换的具体实现代码
查看>>
Golang 数据可视化利器 go-echarts ,实际使用
查看>>
mysql 跨机器查询,使用dblink
查看>>
mysql5.6.34 升级到mysql5.7.32
查看>>
dba 常用查询
查看>>
Oracle 异机恢复
查看>>
Oracle 12C DG 搭建(RAC-RAC/RAC-单机)
查看>>