简体   繁体   中英

Moving different QGraphicsitems independent Qt GraphicsScene

So I'm trying to make an UML diagrams generator from c++ code, in Qt. The problem I have: when I draw a rectangle(diagram) I can move it all over the place, but when I generate several, I can only move the 1st one and the rest moving at the same time like they would be connected to the first rectangle. I must mention that I'm new to Qt. I want to know how can I make to move every single item independent in order to figure a friendly class diagram. One more thing to mention: the diagram shape is made from 3 combined rectangles(rec+rec1+rec2). I'll show you the code:

#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: it's my 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);
}

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

And in the dialog.cpp I have:

#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;
}

Each independently movable item on the scene has to be a separate QGraphicsItem with the ItemIsMovable flag set.

You can also define dependencies between items so that a parent item drags its children items along, even if children can still move independently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM