移动不同的QGraphicsitems独立的Qt Graphicscene 我试图在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 ################################## \ifndef对话框 #定义对话框 #包括 #包括 #包括 #包括 #包括 #包括 #包括“mysquare.h” 名称空间用户界面{ 类对话框; } 类对话框:公共QDialog { Q_对象 公众: 显式对话框(QWidget*parent=0); ~Dialog(); 私人: Ui::Dialog*Ui; //场面 qgraphicscene*场景; MySquare*square; }; #endif//对话框 ##################################

移动不同的QGraphicsitems独立的Qt Graphicscene 我试图在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 ################################## \ifndef对话框 #定义对话框 #包括 #包括 #包括 #包括 #包括 #包括 #包括“mysquare.h” 名称空间用户界面{ 类对话框; } 类对话框:公共QDialog { Q_对象 公众: 显式对话框(QWidget*parent=0); ~Dialog(); 私人: Ui::Dialog*Ui; //场面 qgraphicscene*场景; MySquare*square; }; #endif//对话框 ##################################,c++,qt,C++,Qt,h:这是我的网站 #ifndef MYSQUARE_H #define MYSQUARE_H #include <QPainter> #include <QGraphicsItem> #include <QDebug> class MySquare : public QGraphicsItem { public: MySquare(); QRectF boundingRect() const; void paint(QPainte

h:这是我的网站

#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
\ifndef MYSQUARE\H
#定义MYSQUARE_H
#包括
#包括
#包括
类MySquare:publicqgraphicsitem
{
公众:
MySquare();
QRectF boundingRect()常量;
无效绘制(QPainter*painter、const QStyleOptionGraphicsItem*选项、QWidget*小部件);
布尔压榨;
受保护的:
无效鼠标压力事件(QGraphicsSceneMouseEvent*事件);
无效mouseReleaseEvent(QGraphicsSceneMouseEvent*事件);
};
#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);
}

////////////////////////////////////////////
#包括“mysquare.h”
#包括
#包括
使用名称空间std;
MySquare::MySquare()
{
按下=假;
setFlag(ItemIsMovable);
}
QRectF MySquare::boundingRect()常量
{
返回QRectF(50,90130220);
}
void MySquare::paint(QPainter*painter,const QStyleOptionGraphicsItem*选项,QWidget*小部件)
{
INTA=50,b=90,c=130,d=220;
QBrush刷(Qt::灰色);
QPen大纲图(Qt::黑色);
轮廓设置宽度(2);
//假设我想画3张图
int k=3;
for(int i=1;i fillRect(rec,brush);
油漆工->绘图员(rec);
画师->绘图文本(rec,Qt::AlignCenter,“类名”);
qrectfrec1(a,b+20,c,100);
油漆工->绘图员(rec1);
画师->绘图文本(rec1,Qt::AlignVCenter,“属性”);
qrectfrec2(a,b+100,c,100);
油漆工->绘图员(rec2);
画师->绘图文本(rec2,Qt::AlignVCenter,“方法”);
a=a+c+25;
b=b+50;
如果(i>3)
{
if(i%4==0)//我之所以这样做,是因为我不能独立地移动每个图表,所以我在一定距离处绘制它们
{
b=b+200;
a=50;
c=130;
}
}
}
}
}
void MySquare::mousePressEvent(qgraphicscscenemouseevent*事件)
{
按下=真;
更新();
QGraphicsItem::mousePressEvent(事件);
}
void MySquare::mouseReleaseEvent(QGraphicsSceneMouseEvent*事件)
{
按下=假;
更新();
QGraphicsItem::mouseReleaseEvent(事件);
}
////////////////////////////////////////////
在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;
}
#包括“dialog.h”
#包括“ui_dialog.h”
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
Dialog::Dialog(QWidget*父项):
QDialog(父级),
ui(新建ui::对话框)
{
用户界面->设置用户界面(此);
场景=新的Qgraphicscene(此);
ui->graphicsView->setScene(场景);
square=新的MySquare();
场景->附加项(方形);
}
对话::~Dialog()
{
删除用户界面;
}

场景中每个独立可移动的项目必须是一个单独的
QGraphicsItem
,设置了
ItemIsMovable
标志


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

是否希望
MySquare
中的每个矩形独立移动?是的,这正是我想要的。我不确定是否正确创建了这些对象,因为还有另一个问题。您可以吗给我一个基于我的代码的例子?我对Qt是新手,很难跟上。我的意思是,如果我想独立移动由“for”(int I=1;我使用paint函数仅用于在项目内部自定义绘制。创建新项目时,应完成创建矩形(项目)的循环(在您的代码中,这是对话框构造函数)。我还需要MySquare类吗?可能,但可能需要与您的UML表示匹配的另一个名称,或者完全通用的类似“diagItem”的名称。如果我尝试使用“QGraphicsRectItem”而不是“QRectF”,则可以像在“QRectF”中那样插入文本?