【问题标题】:QGraphicsScene item is drawn at twice(x2) positionQGraphicsScene 项目在两次(x2)位置绘制
【发布时间】:2019-07-19 19:08:56
【问题描述】:

(警告交叉发布于:https://forum.qt.io/topic/105158/qgraphicsscene-item-is-drawn-at-twice-x2-position

在下面的代码中,我正在创建一个自定义小部件,其中嵌入了 QGraphicsScene 的子类。我的目标是单击并向场景添加一个点。一个点是我的 QGraphicsItem (GIPoint) 的子类。我想移动该点,然后将其连接到其他点并制作样条路径。

我面临的问题是,该点不是在我单击的位置绘制的,而是在通过将鼠标事件的 scenePos() 坐标加倍形成的位置上。因此,如果我单击 (100,100),则该点将绘制在 (200,200) 处。尽管阅读了文档,但我怀疑我误解了坐标系。

How to add item in a QGraphicsScene? 的问题似乎是相关的,但是通过mapToScene(event->pos()); 转换鼠标事件坐标的建议解决方案实际上使位置加倍(在打印之前它会在相同的位置上绘制,但它会是 x2。现在它也将其打印为 x2)。

因此,我还要求向我指出一些关于小部件放置如何工作的简单易懂的建议。顺便提一句。是QRectF GIPoint::boundingRect() const { return QRectF(pos().x(), pos().y(), 5, 5); 关于矩形的(x,y)坐标是否正确?

我的示例代码如下:

/* use the following pro:
QT += widgets core gui

CONFIG += debug console
SOURCES = example.cpp
TARGET = example
*/
#include <QGraphicsItem>
#include <QPainter>
#include <QWidget>
#include <QRectF>
#include <QPointF>
#include <QGraphicsScene>
#include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QGraphicsView>
#include <QApplication>
#include <QOpenGLWidget>
#include <QMainWindow>
#include <QGridLayout>
#include <QDebug>

class GIPoint : public QGraphicsItem{
public:
    GIPoint(QGraphicsItem * parent, const QPointF &position);
protected:
    QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) override;
    QRectF boundingRect() const override;
    void paint(
        QPainter *painter,
        const QStyleOptionGraphicsItem *option,
        QWidget *widget
    );
};
class GraphicsSceneWidget : public QGraphicsScene {

public:
    explicit GraphicsSceneWidget(QObject *parent);
    ~GraphicsSceneWidget();
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
};

class VectorGraphicsWidget : public QWidget {
public:
    VectorGraphicsWidget(QWidget *parent);
    ~VectorGraphicsWidget();
private:
    GraphicsSceneWidget *myGraphicsSceneWidget;
};

// implementation
GIPoint::GIPoint(
    QGraphicsItem *parent,
    const QPointF &position
) : QGraphicsItem(parent) {
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setPos(position);
    qWarning() << "GIPoint::GIPoint() : init at " << position;
}
QVariant GIPoint::itemChange(
    GraphicsItemChange change,
    const QVariant &value
){
    if (change == QGraphicsItem::ItemPositionChange) {
        qWarning("position changed");
    }
    return value;
}
QRectF GIPoint::boundingRect() const {
    return QRectF(pos().x(), pos().y(), 5, 5);
//  return QRectF(0,0, 5, 5);
}
void GIPoint::paint(
    QPainter *painter,
    const QStyleOptionGraphicsItem *option,
    QWidget *widget
){
    (void )option;
    (void )widget;
    QPointF xx = scenePos();
    QRectF rect = QRectF(xx.x(), xx.y(), 10, 10);
    qWarning() << "painting: scenePos " << scenePos() << ", rect " << rect;
    QBrush brush = QBrush(Qt::black, Qt::SolidPattern);
    //painter->fillRect(rect, brush);
    painter->drawRect(rect);
}

GraphicsSceneWidget::GraphicsSceneWidget(QObject *parent)
    : QGraphicsScene(parent)
{}
GraphicsSceneWidget::~GraphicsSceneWidget(){}
void GraphicsSceneWidget::mousePressEvent(
    QGraphicsSceneMouseEvent *event
){
    GIPoint *gip = new GIPoint(Q_NULLPTR, event->scenePos());
    addItem(gip);
    QGraphicsScene::mousePressEvent(event);
}

VectorGraphicsWidget::VectorGraphicsWidget(QWidget *parent) :
    QWidget(parent)
{
    myGraphicsSceneWidget = new GraphicsSceneWidget(this);
    QGraphicsView *view = new QGraphicsView(myGraphicsSceneWidget);
    myGraphicsSceneWidget->setSceneRect(QRectF(0, 0, 500, 500));
    QGridLayout *centralLayout = new QGridLayout;
    centralLayout->addWidget(view);
    setLayout(centralLayout);
    myGraphicsSceneWidget->addRect(
        QRectF(0, 0, 100, 100),
        QPen(Qt::black),
        QBrush(Qt::green)
    );
    view->show();
}
VectorGraphicsWidget::~VectorGraphicsWidget() {
    delete myGraphicsSceneWidget;
}
int main(int argc, char **argv){
    QApplication app(argc, argv);
    app.setApplicationName("test");
    app.setOrganizationName("myorg");
    app.setOrganizationDomain("myorg.com");

    QMainWindow *w = new QMainWindow();
    w->resize(500, 500);
    w->setCentralWidget(new VectorGraphicsWidget(Q_NULLPTR));
    w->show();

    return app.exec();
}

【问题讨论】:

    标签: c++ qt qt5 qgraphicsscene


    【解决方案1】:

    问题在于boundingRect()paint() 方法是尊重项目的坐标系,而不是场景。所以解决方法不是在这两种方法中都使用scenePos(),而是0, 0

    QRectF GIPoint::boundingRect() const {
        return QRectF(0, 0, 10, 10);
    }
    void GIPoint::paint(
        QPainter *painter,
        const QStyleOptionGraphicsItem *option,
        QWidget *widget
    ){
        (void )option;
        (void )widget;
        QRectF rect = QRectF(0, 0, 10, 10);
        qWarning() << "painting: scenePos " << scenePos() << ", rect " << rect;
        QBrush brush = QBrush(Qt::black, Qt::SolidPattern);
        painter->fillRect(rect, brush);
        painter->drawRect(rect);
    }
    

    虽然我建议使用 QGraphicsRectItem,因为它实现了你所做的。

    【讨论】:

    • 好的,谢谢!我自己才弄清楚,但首先编辑了我的交叉帖子!事实上,我让 boundingRect 返回 (-0.5, -0.5, 10, 10)。感谢继承 QGraphicsRectItem 的建议。这是有道理的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多