如何在 QGraphicsScene 上绘制一个点(通过鼠标单击)?

发布于 2024-12-11 07:38:37 字数 444 浏览 0 评论 0原文

我有以下代码来设置QGraphicsScene。我希望单击场景并在我单击的位置绘制一个点。我怎么能这样做呢?这是我当前的代码:

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

    QGraphicsScene *scene;
    QGraphicsView *view = new QGraphicsView(this);

    view->setGeometry(QRect(20, 50, 400, 400));
    scene = new QGraphicsScene(50, 50, 350, 350);
    view->setScene(scene);
}

I have the following code to set up a QGraphicsScene. I wish to click on the scene and draw a point at the location I've clicked. How could I do this? This is my current code:

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

    QGraphicsScene *scene;
    QGraphicsView *view = new QGraphicsView(this);

    view->setGeometry(QRect(20, 50, 400, 400));
    scene = new QGraphicsScene(50, 50, 350, 350);
    view->setScene(scene);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南笙 2024-12-18 07:38:37

更新:有一个名为 QGraphicsSceneMouseEvent 的新类,它使这变得更容易一些。
我刚刚在这里完成了一个使用它的示例:

https://stackoverflow.com/a/26903599/999943

它与下面的答案是它是 QGraphicsScene 的子类,而不是 QGraphicsView 的子类,并且它使用mouseEvent->scenePos() 因此不需要手动映射坐标。


您走在正确的道路上,但还有一些路要走。

您需要子类化QGraphicsView,以便能够使用QMouseEvent通过按下鼠标或释放鼠标来执行某些操作。

    #include <QGraphicsView>
    #include <QGraphicsScene>
    #include <QGraphicsEllipseItem>
    #include <QMouseEvent>

    class MyQGraphicsView : public QGraphicsView
    {
        Q_OBJECT
    public:
        explicit MyQGraphicsView(QWidget *parent = 0);

    signals:

    public slots:
        void mousePressEvent(QMouseEvent * e);
        // void mouseReleaseEvent(QMouseEvent * e);
        // void mouseDoubleClickEvent(QMouseEvent * e);
        // void mouseMoveEvent(QMouseEvent * e);
    private:
        QGraphicsScene * scene;
    };

QGraphicsView 本身没有无维度点。您可能想要使用 QGraphicsEllipse 项,或者简单地使用半径非常小的 scene->addEllipseItem()

    #include "myqgraphicsview.h"
    #include <QPointF>

    MyQGraphicsView::MyQGraphicsView(QWidget *parent) :
        QGraphicsView(parent)
    {
        scene = new QGraphicsScene();
        this->setSceneRect(50, 50, 350, 350);
        this->setScene(scene);
    }

    void MyQGraphicsView::mousePressEvent(QMouseEvent * e)
    {
        double rad = 1;
        QPointF pt = mapToScene(e->pos());
        scene->addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0, 
            QPen(), QBrush(Qt::SolidPattern));
    }

请注意使用 mapToScene() 来使事件的 pos() 正确映射到场景中鼠标单击的位置。

如果您要使用表单,则需要将子类 QGraphicsView 的实例添加到 ui 的centralWidget 布局中。

    QGridLayout * gridLayout = new QGridLayout(ui->centralWidget);
    gridLayout->addWidget( new MyQGraphicsView() );

或者如果你的用户界面已经有一个布局,它看起来像这样:

    ui->centralWidget->layout()->addWidget( new MyGraphicsView() );

如果你不使用QMainWindow和表单,你可以将它添加到QWidget如果你设置为其创建一个布局,然后以类似的方式将您的 QGraphicsView 添加到该布局中。如果您不希望 QGraphicsView 周围有边距,只需在其上调用 show 并且不要将其放在不同的布局中。

    #include <QtGui/QApplication>
    #include "myqgraphicsview.h"

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);

        MyQGraphicsView view;
        view.show();

        return a.exec();
    }

就是这样。现在,QGraphicsView 及其与鼠标的交互很危险。

请务必阅读和研究 Qt 的 图形视图框架 以及相关的示例在使用QGraphicsView时有效并且QGraphicsScene。它们是非常强大的 2D 图形工具,可能有一些学习曲线,但它们是值得的。

UPDATE: There is a new class called QGraphicsSceneMouseEvent that makes this a little easier.
I just finished an example using it here:

https://stackoverflow.com/a/26903599/999943

It differs with the answer below in that it subclasses QGraphicsScene, not QGraphicsView, and it uses mouseEvent->scenePos() so there isn't a need to manually map coordinates.


You are on the right track, but you still have a little more to go.

You need to subclass QGraphicsView to be able to do something with mouse presses or with mouse releases using QMouseEvent.

    #include <QGraphicsView>
    #include <QGraphicsScene>
    #include <QGraphicsEllipseItem>
    #include <QMouseEvent>

    class MyQGraphicsView : public QGraphicsView
    {
        Q_OBJECT
    public:
        explicit MyQGraphicsView(QWidget *parent = 0);

    signals:

    public slots:
        void mousePressEvent(QMouseEvent * e);
        // void mouseReleaseEvent(QMouseEvent * e);
        // void mouseDoubleClickEvent(QMouseEvent * e);
        // void mouseMoveEvent(QMouseEvent * e);
    private:
        QGraphicsScene * scene;
    };

QGraphicsView doesn't natively have dimension-less points. You will probably want to use QGraphicsEllipse item or simply, scene->addEllipseItem() with a very small radius.

    #include "myqgraphicsview.h"
    #include <QPointF>

    MyQGraphicsView::MyQGraphicsView(QWidget *parent) :
        QGraphicsView(parent)
    {
        scene = new QGraphicsScene();
        this->setSceneRect(50, 50, 350, 350);
        this->setScene(scene);
    }

    void MyQGraphicsView::mousePressEvent(QMouseEvent * e)
    {
        double rad = 1;
        QPointF pt = mapToScene(e->pos());
        scene->addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0, 
            QPen(), QBrush(Qt::SolidPattern));
    }

Note the usage of mapToScene() to make the pos() of the event map correctly to where the mouse is clicked on the scene.

You need to add an instance of your subclassed QGraphicsView to the centralWidget's layout of your ui if you are going to use a form.

    QGridLayout * gridLayout = new QGridLayout(ui->centralWidget);
    gridLayout->addWidget( new MyQGraphicsView() );

or if your ui has a layout already it will look like this:

    ui->centralWidget->layout()->addWidget( new MyGraphicsView() );

If you don't use a QMainWindow and a form, you can add it to a QWidget if you set a layout for it and then add your QGraphicsView to that layout in a similar manner. If you don't want a margin around your QGraphicsView, just call show on it and don't put it inside a different layout.

    #include <QtGui/QApplication>
    #include "myqgraphicsview.h"

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);

        MyQGraphicsView view;
        view.show();

        return a.exec();
    }

And that's it. Now you are dangerous with QGraphicsView's and their interaction with the mouse.

Be sure to read and study about Qt's Graphics View Framework and the related examples to be effective when using QGraphicsView and QGraphicsScene. They are very powerful tools for 2D graphics and can have a bit of a learning curve but they are worth it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文