移动不同的QGraphicsitems独立Qt GraphicsScene

时间:2016-01-15 14:32:05

标签: c++ qt

所以我试图在Qt中用c ++代码创建一个UML图表生成器。我遇到的问题是:当我绘制一个矩形(图表)时,我可以将它移动到整个地方,但是当我生成几个时,我只能移动第一个,其余的同时移动,就像它们将连接到第一个矩形。 我必须提到我是Qt的新手。我想知道如何独立移动每个项目以形成友好的类图。 还有一点需要提及:图表形状由3个组合矩形组成(rec + rec1 + rec2)。 我将向您展示代码:

#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QtCore>
#include <QtGui>
#include<QPainter>
#include<QGraphicsItem>
#include <QGraphicsScene>
#include "mysquare.h"

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();


private:
    Ui::Dialog *ui;
    //scene
    QGraphicsScene *scene;
    MySquare *square;



};

#endif // DIALOG_H
##################################

mysquare.h:它是我的GraphicsItem

#ifndef MYSQUARE_H
#define MYSQUARE_H
#include <QPainter>
#include <QGraphicsItem>
#include <QDebug>


class MySquare : public QGraphicsItem
{
public:
    MySquare();

    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    bool Pressed;

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
};

#endif // MYSQUARE_H

MySquare.cpp

#include "mysquare.h"
#include <fstream>
#include <iostream>
using namespace std;

MySquare::MySquare()
{
    Pressed = false;
    setFlag(ItemIsMovable);
}

QRectF MySquare::boundingRect() const
{
    return QRectF(50,90,130,220);
}

void MySquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    int a = 50, b = 90, c=130, d=220;
    QBrush brush(Qt::gray);
    QPen outlinePen(Qt::black);
    outlinePen.setWidth(2);

    //Let's say I want to draw 3 diagrams
    int k = 3;

    for(int i=1; i <=  k ; i++)     //k is the number of classes (diagrams)
    {
        QRectF rec(a,b,c,d);
        painter->fillRect(rec,brush);
        painter->drawRect(rec);
        painter->drawText(rec,Qt::AlignCenter,"Class name");

        QRectF rec1(a,b+20,c,100);
        painter->drawRect(rec1);
        painter->drawText(rec1,Qt::AlignVCenter,"Attributes");

        QRectF rec2(a,b+100,c,100);
        painter->drawRect(rec2);
        painter->drawText(rec2,Qt::AlignVCenter,"Methods");

        a = a + c + 25;
        b = b+50;
        if(i > 3)
        {
            if(i%4 == 0)    // Im doing this just because I cant move every diagram independently so I draw them at some distance
            {
                b = b+200;
                a = 50;
                c = 130;
            }
        }
    }


}

}

void MySquare::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Pressed = true;
    update();
    QGraphicsItem::mousePressEvent(event);
}

void MySquare::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Pressed = false;
    update();
    QGraphicsItem::mouseReleaseEvent(event);
}

////////////////////////////////////////////

在dialog.cpp中我有:

#include "dialog.h"
#include "ui_dialog.h"
#include<iostream>
#include<fstream>
#include<string>
#include<QtGui>
#include<QPaintEvent>
#include<QGraphicsItem>

using namespace std;

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);



    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);

    square = new MySquare();
    scene ->addItem(square);
}

Dialog::~Dialog()
{
    delete ui;
}

1 个答案:

答案 0 :(得分:0)

场景中每个可独立移动的项目必须是单独的QGraphicsItem并设置ItemIsMovable标记。

您还可以定义项之间的依赖关系,以便父项拖动其子项,即使子项仍可以独立移动。