修复 QGraphicsPixmapItem与QGraphicsScene的编程实例 图标拖动。

展示效果。

我自已不太会说话,也不懂。上源码。notice:图片自已添加。无用部分自已删掉。我不整理了。

#ifndef SWITCHWINDOW_H
#define SWITCHWINDOW_H

#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QPointF>
#include "process/nodeview.h"
#include "process/mysence.h"
#include <QMap>



class SwitchWindow : public QMainWindow
{
    Q_OBJECT

public:
    SwitchWindow(QWidget *parent = 0);
    ~SwitchWindow();
    //NodeUI *selectedNodeUI();
    bool isNodeUiClicked();
    void nodeUiSizeAdjust();

private:
    void initUI();
    void GetScreenInfo();
    QGraphicsView *view;
    MySence *scene;
    NodeUI* node1;
    NodeUI* node2;
    //instead of (NodeUI *nodeui;)&(QPointF nodeUiPos;)
    volatile bool mPressed;//节点是否按压
    QList<NodeUI*> nodeUiLists; //节点item
    QList<QPointF> nodeUiPosLists;//节点坐标
    QList<QPixmap> nodeUiPixmapLists;//节点图片
NodeUI* Currentnode ;

    NodeUI *nodeui;      //当前节点
    QPointF nodeUiPos;  //当前节点坐标
    qint16 sceenSizeX; //屏幕x尺寸
    qint16 sceenSizeY;

public slots:
    void isMoving(QPointF &pos);
    void isReleased();
    void selectionChanged();

signals:
    void nodeUiClicked(NodeUI* node);

};


#endif // SWITCHWINDOW_H

#include "process/switchwindow.h"
#include <QDesktopWidget>
#include <QApplication>
#include <QPixmap>
#include <QGraphicsItem>
#include <QMouseEvent>
#include <QWidget>
#include <QGraphicsPixmapItem>
#include <QMessageBox>
#include <QDebug>
#include<QMetaType>

const qreal MY_NODEUI_POS_Y = 200;
const qreal MY_NODEUI_DIS = 110;
const qreal MY_NODEUI_STA = 90;
const int   MYNODEUI_SIZE = 100;
const int   MYNODEUI_SIZE_M = 20;
const int   SCREEN_SIZE = 800;

SwitchWindow::SwitchWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // qRegisterMetaType<TEST_TYPE>("TEST_TYPE");

    mPressed = false;//初始化变量
    GetScreenInfo(); //获取屏幕的大小

    //setMouseTracking(true);
    view = new QGraphicsView;
    scene = new MySence;
    // node1 = new NodeUI;
    Currentnode = new NodeUI;
    initUI();

    connect(scene, static_cast<void(MySence::*)(QPointF&)>(&MySence::isMoving), [&](QPointF pos) {
        isMoving(pos);
    });

   connect(scene, SIGNAL(selectionChanged()), this, SLOT( selectionChanged()));

   connect(node1, static_cast<void(NodeUI::*)(bool&)>(&NodeUI::nodeIsPressed), [&](bool NodePressed){
        mPressed = NodePressed;
        qDebug()<<u8"m1pressed的值"<<mPressed;
    });//为什么不响应
   connect(node2, static_cast<void(NodeUI::*)(bool&)>(&NodeUI::nodeIsPressed), [&](bool NodePressed){
        mPressed = NodePressed;
        qDebug()<<u8"m1pressed的值"<<mPressed;
    });//为什么不响应
   connect(node1, static_cast<void(NodeUI::*)(bool&)>(&NodeUI::nodeIsRelease), [&](bool NodePressed){
       mPressed = NodePressed;
//        if(isNodeUiClicked())
//            qDebug()<<"clicked";
       qDebug()<<u8"m1prease的值"<<NodePressed;
   });//为什么不响应
    connect(node2, static_cast<void(NodeUI::*)(bool&)>(&NodeUI::nodeIsRelease), [&](bool NodePressed){
        mPressed = NodePressed;
//        if(isNodeUiClicked())
//            qDebug()<<"clicked";
        qDebug()<<u8"m1prease的值"<<NodePressed;
    });//为什么不响应





}


void SwitchWindow::initUI()
{

    scene->setSceneRect(0,0,800,400);//显示kuang框的大小
    QString file;
    QString text;
    QPointF pos;

  //  HOME:1
    file = QString("C:/Users/dell/Pictures/Camera Roll/1.jpg");
    text = QString("Home");
    pos = QPointF(MY_NODEUI_STA,MY_NODEUI_POS_Y); //构建一个点
    node1 = new NodeUI(file,text,MYNODEUI_SIZE);//设置节点ui的文本和图片大小
    node1->setPos(pos); //设置节点 在场景坐标系中的位置
    nodeUiLists.append(node1);
    nodeUiPosLists.append(pos);
    nodeUiPixmapLists.append(node1->pixmap());

    //VIDIO:2
    file = QString("C:/Users/dell/Pictures/Camera Roll/2.jpg");
    text = QString("Vidio");
    pos = QPointF(MY_NODEUI_STA+MY_NODEUI_DIS*1,MY_NODEUI_POS_Y);
    node2= new NodeUI(file,text,MYNODEUI_SIZE);
    node2->setPos(pos);
    nodeUiLists.append(node2);
    nodeUiPosLists.append(pos);
    nodeUiPixmapLists.append(node2->pixmap());

    //APPLICATION:3
    file = QString("C:/Users/dell/Pictures/Camera Roll/th.jpg");
    text = QString("Application");
    pos = QPointF(MY_NODEUI_STA+MY_NODEUI_DIS*2,MY_NODEUI_POS_Y);
    node1 = new NodeUI(file,text,MYNODEUI_SIZE);
    node1->setPos(pos);
    nodeUiLists.append(node1);
    nodeUiPosLists.append(pos);
    nodeUiPixmapLists.append(node1->pixmap());

//        //NETWORK:4
//        file = QString("C:/Users/dell/Pictures/Camera Roll/Pstart.jpg");
//        text = QString("Network");
//        pos = QPointF(MY_NODEUI_STA+MY_NODEUI_DIS*3,MY_NODEUI_POS_Y);
//        node1 = new NodeUI(file,text,MYNODEUI_SIZE);
//        node1->setPos(pos);
//        nodeUiLists.append(node1);
//        nodeUiPosLists.append(pos);
//        nodeUiPixmapLists.append(node1->pixmap());

    //    //COMPUTER:5
    //    file = QString("C:/Users/dell/Pictures/Camera Roll/1.jpg");
    //    text = QString("Phone");
    //    pos = QPointF(MY_NODEUI_STA+MY_NODEUI_DIS*4,MY_NODEUI_POS_Y);
    //    node1 = new NodeUI(file,text,MYNODEUI_SIZE);
    //    node1->setPos(pos);
    //    nodeUiLists.append(node1);
    //    nodeUiPosLists.append(pos);
    //    nodeUiPixmapLists.append(node1->pixmap());

    //    //CUSTOMIZE:5
    //    file = QString("C:/Users/dell/Pictures/Camera Roll/2.jpg");
    //    text = QString("Setting");
    //    pos = QPointF(MY_NODEUI_STA+MY_NODEUI_DIS*5,MY_NODEUI_POS_Y);
    //    node1 = new NodeUI(file,text,MYNODEUI_SIZE);
    //    node1->setPos(pos);
    //    nodeUiLists.append(node1);
    //    nodeUiPosLists.append(pos);
    //    nodeUiPixmapLists.append(node1->pixmap());

    //重新计算UiSize
    nodeUiSizeAdjust();

    int i = 0;
    foreach(NodeUI* node_temp,nodeUiLists)
    {
        node_temp->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);

        qDebug()<<"name:"<<node_temp->getMyText()<<nodeUiPosLists.at(i);
        scene->addItem(node_temp);
        i++;
    }

    view->setScene(scene);
    //set drag mode
    view->setDragMode(QGraphicsView::RubberBandDrag);
    view->setBackgroundBrush(QImage("C:/Users/dell/Pictures/Camera Roll/2.jpg"));

    view->setRenderHints(QPainter::Antialiasing);
    //no menu
    view->setContextMenuPolicy(Qt::NoContextMenu);

    view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    view->setCacheMode(QGraphicsView::CacheBackground);
    setCentralWidget(view);
    setWindowTitle(tr("qw Window"));
}



//槽,当scene鼠标拖拽是执行
//控制UI图标的水平
void SwitchWindow::isMoving(QPointF &pos)
{
    qDebug()<<u8" 移动ok"<<u8"mpressed="<<mPressed;
    int i=0;
    if(mPressed){
        foreach(NodeUI* node,nodeUiLists)
        {
            node->setPos(nodeUiPosLists.at(i).x()+pos.x(),MY_NODEUI_POS_Y);
            i++;

        }
        nodeUiSizeAdjust();
    }
}



//槽,当nodeui鼠标释放时执行
//应当设置标志位,让UI图片停止对鼠标拖动事件的响应
void SwitchWindow::isReleased()
{

}

//槽,当scene的selectedItem变化时,发送同名信号到此槽
void SwitchWindow::selectionChanged()
{

    int i=0,j=0;
    QList<QGraphicsItem *> items = scene->selectedItems();

    if (items.count() == 1) {
        QPointF pos = items.first()->pos();     //当前所选择的UI图标的坐标

        Currentnode = dynamic_cast<NodeUI *>(items.first());
        qDebug()<<"items.x:"<<pos.x()<<"items.y:"<<pos.y();

        foreach(NodeUI* node,nodeUiLists)
        {
            if(node == Currentnode)
                break;
            i++;
        }
        j=i;
        i=0;
        foreach(QPointF ppos,nodeUiPosLists)
        {
            nodeUiPosLists[i].setX((i-j)*MY_NODEUI_DIS+pos.x());
            nodeUiPosLists[i].setY(MY_NODEUI_POS_Y);
            i++;
//              qDebug()< nodeUiPosLists[i].x()<<
//              nodeUiPosLists[i].y();
        }

    }
    else
    {
        return;
    }
}

//判断是否Nodeui接收的是否是单击信号。
//判断依据是当前单击的nodeui对象的pos与存储在nodeUiPosListsd的位置比较,相等则为单击
bool SwitchWindow::isNodeUiClicked()
{
    int i=-1;
    QList<QGraphicsItem *> items = scene->selectedItems();
    if (items.count() == 1) {
        QPointF pos = items.first()->pos();
        NodeUI* node_temp = dynamic_cast<NodeUI *>(items.first());
        if(pos ==nodeUiPosLists.at(i)){
            //emit nodeUiClicked(node_temp);
            QMessageBox::information(this,"New Window","will open : "+node_temp->getMyText());
            return true;
        }
    }
    return false;
}
void SwitchWindow::nodeUiSizeAdjust()
{
    quint16 i=0;
    foreach(NodeUI* node,nodeUiLists)
    {
        //qDebug()<<"i= "<<i;
        QPointF pos=node->pos();

        pos.setX(node->pos().x()+MYNODEUI_SIZE/2);
        //pos.setX(node->pos().x()+node->pixmap().width());
        if(pos.x()>=0 && pos.x()<=SCREEN_SIZE/2)
        {
            //(MYNODEUI_SIZE-MYNODEUI_SIZE_M)/(SCREEN_SIZE/2)==(size-20)/pos.x()
            quint16 size=pos.x()/5+20;
            QPixmap pixmap = nodeUiPixmapLists.at(i);
            //QPixmap pixmap = nodeUiLists.at(i)->pixmap();
            pixmap = pixmap.scaled(size,size,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
            nodeUiLists[i]->setPixmap(pixmap);
        }

        //if(pos.x()>SCREEN_SIZE/2 && pos.x()<=SCREEN_SIZE)
        if(pos.x()>SCREEN_SIZE/2 && pos.x()<=SCREEN_SIZE+10)
        {
            //(MYNODEUI_SIZE-MYNODEUI_SIZE_M)/(SCREEN_SIZE/2)==(size-20)/pos.x()
            quint16 size=(SCREEN_SIZE-pos.x())/5+20;
            QPixmap pixmap = nodeUiPixmapLists.at(i);
            //QPixmap pixmap = nodeUiLists.at(i)->pixmap();
            pixmap = pixmap.scaled(size,size,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
            nodeUiLists[i]->setPixmap(pixmap);
        }
        i++;
    }
}


SwitchWindow::~SwitchWindow()
{
    deleteLater();
}
//获取设备分辨率的呢个信息
void SwitchWindow::GetScreenInfo()
{
    QDesktopWidget* desktopWidget = QApplication::desktop();
    //获取可用桌面大小
    //QRect deskRect = desktopWidget->availableGeometry();
    //获取设备屏幕大小
    QRect screenRect = desktopWidget->screenGeometry();

    sceenSizeX = screenRect.width();
    sceenSizeY = screenRect.height();

    //获取系统设置的屏幕个数(屏幕拷贝方式该值为1)
    //g_nScreenCount = desktopWidget->screenCount();
}
#ifndef MYSENCE_H
#define MYSENCE_H

#include"QGraphicsScene"
#include <QGraphicsSceneEvent>
#include <QGraphicsRectItem>
#include <QObject>
#include <QDrag>
#include <QGraphicsSceneDragDropEvent>
#include <QMimeData>
#include <QListWidget>
#include <QGraphicsPixmapItem>
#include <QDebug>
#include "imageitem.h"
#include <QLabel>
#include <QWidget>
#include <QDateTime>
#include <QMimeData>
#include <QPointF>
#include <QGraphicsProxyWidget>
#include <QGraphicsDropShadowEffect>


class MySence:public QGraphicsScene
{
    Q_OBJECT
public:
    MySence(QObject* parent=0);
protected:
    //drag操作 移动事件
    void dragMoveEvent(QGraphicsSceneDragDropEvent *event) override;
    //drag操作 进入事件
    void dragEnterEvent(QGraphicsSceneDragDropEvent* event)override ;
    //drag操作 离开事件
    void dragLeaveEvent(QGraphicsSceneDragDropEvent* event)override;
    //drop 拖放事件
    void dropEvent(QGraphicsSceneDragDropEvent* event) override;

//    //鼠标按压事件
  void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
//    //鼠标释放事件
//   void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
    //鼠标移动事件
  void mouseMoveEvent(QGraphicsSceneMouseEvent  *event) override;
signals:
    void yousignal();
    void nodeIsPressed(bool &NodePressed);
signals:
    void isMoving(QPointF &pos);

public slots:

private:
    QPointF  beforePos;
    QPointF  releasePos;

public:
    enum treeItemType{itTopItem=1001,itGroupItem,itImageItem};//
    QGraphicsPixmapItem *pPixmapItem ;
    QGraphicsScene scene;
    QPixmap pixmap;
};

#endif // MYSENCE_H
#include "process/mysence.h"
#include"qdebug.h"

#include <QGraphicsSceneDragDropEvent>
#include <QMimeData>
#include <QListWidget>
#include <QGraphicsPixmapItem>
#include <QDebug>
#include "imageitem.h"
#include <QLabel>
#include <QTreeWidgetItem>
#include <QWidget>
#include <QDateTime>
#include <QMimeData>
#include <QPointF>
#include <QGraphicsProxyWidget>
#include <QGraphicsDropShadowEffect>

#include <QFileInfo>
#include <QMessageBox>
#include "frmshowpictures.h"

#define DRAG_DATA QStringLiteral("application/x-qabstractitemmodeldatalist")

MySence::MySence(QObject *parent):QGraphicsScene(parent)
{
    qDebug() << "dragMoveEvent";
};

void MySence::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
    //坑1 并不是在DragEnterEvent 开启接受权限 而是在这
    event->accept();
}

void MySence::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
    if(event->mimeData()->hasFormat(DRAG_DATA))
    {
        event->accept();
    }

}

void MySence::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
    //qDebug() << "dragLeaveEvent";
}

void MySence::dropEvent(QGraphicsSceneDragDropEvent *event)
{

    QString fileName;
    qDebug()<<"tree"<<"ji"<<event->mimeData()->formats();
    if(event->mimeData()->hasUrls())
    {
        qDebug()<<"tree";
        QList<QUrl> urls = event->mimeData()->urls();
        fileName = urls[0].toLocalFile();
        qDebug()<<u8"图片文件"<<fileName;
        pPixmapItem = new ImageItem(QPixmap(fileName));
        pPixmapItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
        pPixmapItem->setPos(event->scenePos());
        addItem(pPixmapItem);
    }
    else if(event->mimeData()->hasFormat(DRAG_DATA))
    {
        QTreeWidget *pTreeWidget = qobject_cast<QTreeWidget *>(event->source());
        //得到图片名称 text(0)中的0 表示 columns 第一栏

        fileName=  pTreeWidget->currentItem()->data(0,Qt::UserRole).toString();
        qDebug() << "love"<< pTreeWidget->currentItem()->data(0,Qt::UserRole);
        qDebug() << "Wlove"<<pTreeWidget->currentItem()->data(1,0);

        if(pTreeWidget->currentItem()->data(1,0).toString()=="type=itImageItem")
        {
            pPixmapItem = new ImageItem(QPixmap(fileName));
            pPixmapItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
            pPixmapItem->setPos(event->scenePos());
            addItem(pPixmapItem);
        }
        //        else if ( (pListwidget->currentItem()->text()) ==  u8"时间")
        //        {
        //            pPixmapItem = new ImageItem(QPixmap(fileName));
        //            // 创建 QDateTime 对象,并根据当前时间设置日期时间值
        //            QDateTime dateTime = QDateTime::currentDateTime();

        //            // 创建 QLabel 对象以显示时间戳
        //            QLabel *pLabel = new QLabel(dateTime.toString(), 0);
        //            pLabel->setStyleSheet("background-color: white;");

        //            // 创建 QGraphicsProxyWidget 对象,并将 QLabel 添加到其中
        //            QGraphicsProxyWidget *pProxyWidget = new QGraphicsProxyWidget(pPixmapItem);
        //            pProxyWidget->setWidget(pLabel);
        //            pProxyWidget->setPos(event->scenePos());       //落在鼠标最后松开的地方  //设置坐标

        //            // 将 QLabel 与 QPixmapItem 相关联以确保它始终处于正确的位置
        //            pPixmapItem->setGraphicsEffect(new QGraphicsDropShadowEffect());
        //            pPixmapItem->setData(Qt::UserRole, QVariant::fromValue(pProxyWidget));
        //            addItem(pPixmapItem);
        //        }
        //        else
        //        {
        //            return;
        //        }

    }
    else
    {
        qDebug()<<u8"重新选择";
        return;

    }

    //    if (event->mimeData()->hasFormat("Data/name"))
    //    {
    //        //获取拖拽的数据
    //        QByteArray itemData = event->mimeData()->data("Data/name");
    //        QDataStream dataStream(&itemData, QIODevice::ReadOnly);
    //        QString text;
    //        dataStream >>text;
    //        qDebug()<<text;

    //        //创建一个item
    //        QGraphicsRectItem  * item = new QGraphicsRectItem();
    //        //坑2 event->pos() 失效了 坐标一直是(0,0),直接用场景坐标
    //        item->setRect(event->scenePos().x(),event->scenePos().y(),100,50);
    //        this->addItem(item);


    //    }
    //    QGraphicsScene::dropEvent(event);
}

//鼠标按压
void MySence::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug()<<"mousePressEvent Button" << event->scenePos().x() << event->scenePos().y();//鼠标的坐标点。注意用scenePos()
    beforePos = event->scenePos();
    QGraphicsScene::mousePressEvent(event);
}

鼠标释放
//void MySence::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
//{

//    qDebug()<<"mouseReleaseEvent Button" << event->scenePos().x() << event->scenePos().y();//鼠标的坐标点。注意用scenePos()
//    releasePos = event->scenePos();
//    QGraphicsScene::mouseReleaseEvent(event);

//}

//鼠标移动
void MySence::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug()<<u8"移动量";
    //QPointF pos = event->scenePos();
    QPointF pos(event->scenePos().x()-beforePos.x(),event->scenePos().y()-beforePos.y());
    emit isMoving(pos);
    //qDebug()<<""<<"x:"<<pos.x()<<"y:"<<pos.y();
    QGraphicsScene::mouseMoveEvent(event);
}
#ifndef NODEVIEW_H
#define NODEVIEW_H

#include <QGraphicsPixmapItem>
#include <QStyleOptionGraphicsItem>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QPointF>
//#include <QGraphicsObject>
#include <QtWidgets>

class NodeUI : public QObject,public QGraphicsPixmapItem
{
    Q_OBJECT
public:
    NodeUI();

    NodeUI(QString &file,QString &text,int imagesize=80);
    void setMyPixmap(QString &file,int size);
    void setMyText(QString &text);
    QString getMyText();


signals:
    void yousignal();
    void nodeIsPressed(bool &NodePressed);
    void nodeIsRelease(bool &NodePRelease);
   void isMoving(QPointF &pos);
private slots:
    void youslot();


protected:
     //QPainterPath shape() const override;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)  override;
       QRectF boundingRect() const override ;



    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
     void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;

private:
    QString myText;
        int i=1;
private:
    QPointF  beforePos;
    QPointF  releasePos;

};

#endif // NODEVIEW_H
#include "nodeview.h"

#include <QPixmap>
#include <iostream>
#include <QDebug>

/*note: imagesize = 80 is in the nodeui.h*/
NodeUI::NodeUI()
{
    this->setFlag(QGraphicsItem::ItemIsSelectable);
    this->setAcceptedMouseButtons(Qt::LeftButton);
    this->setAcceptHoverEvents(true);//重写hover事件需添加
}

NodeUI::NodeUI(QString &file,QString &text,int imagesize)
{

    //this->setAcceptHoverEvents(true);//打开鼠标悬停。不可删
    qDebug()<<"1231";
    setMyText(text);
    setMyPixmap(file,imagesize);
}
//设置私有成员 节点ui 的文本
void NodeUI::setMyText(QString &text)
{
    myText = text;
}

//设置 节点ui的 图标
void NodeUI::setMyPixmap(QString &file,int size)
{
    //myImage = file;
    QPixmap pixmap;
    pixmap.load(file);
    pixmap= pixmap.scaled(size,size,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    setPixmap(pixmap);
}

//oundRect{return QRectF() };决定鼠标点击起作用的位置.
//触发条件,鼠标放上去。点击释放。返回矩形
//这个widget在这个Rect内发生了update或变化 会触发Qt对这部分进行绘制
QRectF NodeUI::boundingRect() const
{

    QRect rect = this->pixmap().rect();
    //qDebug()<<"hjjhj"<<rect;
    //return QRectF(rect);
    return QRectF(0,0,rect.width(),rect.width());
}
//QPainterPath NodeUI::shape() const
//{

//    QRectF rect = boundingRect();

//    QPainterPath path;
//    qDebug()<<"path"<<path;
//    path.setFillRule( Qt::WindingFill );   //设置填充方式
//    path.addEllipse(rect);

//    return path;
//}

//boundingRect到paint 再到shape的重写顺序
void NodeUI::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
    qDebug()<<"pain";
    QPixmap pixmap = this->pixmap();
    QRect rect = pixmap.rect();
       painter->drawPixmap(rect,pixmap);
    //grad 渐变画刷 ;Qt::darkGray颜色的画
    bool down = option->state & QStyle::State_Sunken;
    QRectF r = boundingRect();

//    QLinearGradient grad(r.topLeft(), r.bottomRight());
//    //从按钮最小外包围矩形左上角到右下角进行渐变。
//    //如果鼠标将按钮按下 了,则设置渐变的起点颜色为Qt::darkGray、终点颜色为Qt::white。
//    grad.setColorAt(down ? 1 : 0, option->state & QStyle::State_MouseOver ? Qt::white : Qt::lightGray);
//    grad.setColorAt(down ? 0 : 1, Qt::darkGray);
//    painter->setPen(Qt::darkGray);
//    painter->setBrush(grad);
//    painter->drawEllipse(r);

//    //鼠标既没按下按钮也没悬浮在按钮上面
//    QLinearGradient grad2(r.topLeft(), r.bottomRight());
//    grad.setColorAt(down ? 1 : 0, Qt::darkGray);
//    grad.setColorAt(down ? 0 : 1, Qt::lightGray);
//    painter->setPen(Qt::NoPen);
//    painter->setBrush(grad);
//    if (down)
//        painter->translate(2, 2);
//    painter->drawEllipse(r.adjusted(5, 5, -5, -5));
//    painter->drawPixmap(pixmap.width()/2, pixmap.height()/2, pixmap);


    //    painter->setRenderHint(QPainter::Antialiasing);
    QFont font("Verdana",8, QFont::Normal);
    painter->setFont(font);
    painter->drawText(QRectF(0,rect.height(),rect.width(),15),Qt::AlignCenter,myText);

    //    if (option->state & QStyle::State_Sunken)//判断鼠标是否按下
    //    {
    //
    //        QRectF rect1 = boundingRect();
    //        //QPen pen(Qt::darkGreen);
    //        painter->setPen(QPen(Qt::darkGreen));
    //    }else
    //    {
    //        qDebug()<<"painpainsdfs";
    //    }
}


void NodeUI::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (i == 1) {
        qDebug()<<u8" m1pressed";
        i = 0;
        event->accept();
        //qDebug()<<u8" 000";
    }
    else {
       // qDebug()<<u8" 111";
        event->ignore();
    }

    bool NodePressed =true;
    emit nodeIsPressed(NodePressed) ;


    //connect(this, &NodeUI::yousignal, this,&NodeUI::youslot,Qt::UniqueConnection);
    QGraphicsItem::mousePressEvent(event);// 将点击事件向下传递到item中
}



void NodeUI::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    // releaseMouse();
    i=1;
    bool NodePressed =false;
    qDebug()<<u8" m1prease";
    emit nodeIsRelease(NodePressed);
   QGraphicsItem::mouseReleaseEvent(event);
}

void NodeUI::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
   // qDebug()<<u8" 移动ok";
    //QGraphicsItem::mouseMoveEvent(event);
}




QString NodeUI::getMyText()
{
    return myText;

}

void NodeUI::youslot()
{
    qDebug()<<u8" movemove";


}

//void NodeUI::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
//{
//    qDebug()<<u8" 信号";
//    //开启信号权限
//   // event->accept();
//}


特别注意。

  QGraphicsItem::mousePressEvent(event);// 将点击事件向下传递到item中
QGraphicsScene::mousePressEvent(event);必须要有。原因是会发生信号过滤。

list中储存的nodeui*必须申请内存。然后每个node1,node2都要用connect连接。鼠标按压和释放,和移动事件。

拖放事件的重写,我自已用,可删掉。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值