在 QGraphicsScene 上绘制 QGraphicsItem 的正确方法 [英] Correct way to draw a QGraphicsItem on QGraphicsScene

查看:69
本文介绍了在 QGraphicsScene 上绘制 QGraphicsItem 的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 qt 的网站和此处查看了一些资源,但无法解决我的问题.

I checked a couple of resources on qt's website and here but I could not solve my problem.

我正在尝试在鼠标单击时在 QGraphicsScene 上绘制一个矩形,我希望新矩形正好在用户单击的位置居中,但这在场景足够大之前不起作用.

I'm trying to draw a rectangle on QGraphicsScene on mouse click and I want the new rectangle to be centered exactly where the user clicked but this does not work until the scene is big enough.

这是我尝试过的

MainWindow.cpp

MainWindow::MainWindow(QWidget *parent) :
   QMainWindow(parent),
   ui(new Ui::MainWindow)
{
   ui->setupUi(this);
=
   ui->graphView->setContextMenuPolicy(Qt::CustomContextMenu);
   scene = new QGraphicsScene();
   ui->graphView->setScene(scene);
...
}


    void MainWindow::on_graphView_customContextMenuRequested(const QPoint &pos)
    {
     auto pp= ui->graphView->mapToScene(pos);
    tableOfRectangles.push_back( new component(pp,s,n,t)); //component is my class that inherits from qgraphicsitem
     scene->addItem(tableOfRectangles[tableOfRectangles.size()-1]);
    }

并在 compenent.cpp

component::component(QPointF pos,unsigned int id, QString cname, QString ctype  )
{
    this->center = pos;
    this->id = id;
    this->name = cname;
    this->type = ctype;
    setFlag(ItemIsMovable);


}

QRectF component::boundingRect() const
{

    return QRectF(center.x(),center.y(),80,80);
}


我的问题是:

第一个矩形绘制在中间,随着我向正确位置添加的越多,继续稍微移动.通过添加更多矩形(或拖动现有矩形)使场景足够大(当滚动条开始出现时),新矩形会在鼠标位置正确添加.但是我如何强制它们从一开始就插入到正确的位置?

The very first rectangles are drawn in the middle, and keep shifting slightly the more I add toward the correct position. Byadding more rectangles (or dragging the existing ones) making the scene big enough (when scroll bars start to appear), the new rectangles are added correctly at mouse position. but how do I do force them to be inserted at the correct position since the beginning?

推荐答案

您正在将相对于项目的 boundingRect 坐标与相对于场景的坐标结合起来.另一方面,不要使创建自定义项复杂化,而是使用自定义 QGraphicsRectItem.最后建议你建立一个sceneRect.

You are combining the boundingRect coordinates that are relative to the item with the coordinates relative to the scene. On the other hand, don't complicate creating a custom item, instead use a custom QGraphicsRectItem. Finally it is recommended that you establish a sceneRect.

component.h

#ifndef COMPONENT_H
#define COMPONENT_H

#include <QGraphicsRectItem>

class Component : public QGraphicsRectItem
{
public:
    Component(unsigned int id, QString cname, QString ctype, QGraphicsItem *parent=nullptr);
private:
    unsigned int m_id;
    QString m_cname;
    QString m_ctype;
};

#endif // COMPONENT_H

component.cpp

#include "component.h"

Component::Component(unsigned int id, QString cname, QString ctype, QGraphicsItem*parent):
    QGraphicsRectItem(parent), m_id(id), m_cname(cname), m_ctype(ctype)
{
    setRect(-40, -40, 80, 80);
    setFlag(ItemIsMovable);
}

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

    ui->graphView->setContextMenuPolicy(Qt::CustomContextMenu);
    scene = new QGraphicsScene();
    ui->graphView->setScene(scene);
    ui->graphView->setSceneRect(QRect(0, 0, 400, 400));
}

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

void MainWindow::on_graphView_customContextMenuRequested(const QPoint &pos)
{

    QPointF pp = ui->graphView->mapToScene(pos);
    Component* component = new Component(s, n, t);
    scene->addItem(component);
    component->setPos(pp);
}

这篇关于在 QGraphicsScene 上绘制 QGraphicsItem 的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆