如何在QGraphicsScene上绘制点(在mouseclick上)? [英] How to draw a point (on mouseclick) on a QGraphicsScene?

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

问题描述

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

  MainWindow :: MainWindow(QWidget * parent):
QMainWindow b $ b 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);
}


解决方案

更新:类 QGraphicsSceneMouseEvent ,使这一点更容易。
我刚刚完成了一个使用它的例子:



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



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






你是在正确的轨道,但你还有一点点。 / p>

您需要子类 QGraphicsView 才能使用鼠标按钮或鼠标释放使用 QMouseEvent

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

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

信号:

公共位置:
void mousePressEvent(QMouseEvent * e);
// void mouseReleaseEvent(QMouseEvent * e);
// void mouseDoubleClickEvent(QMouseEvent * e);
// void mouseMoveEvent(QMouseEvent * e);
private:
QGraphicsScene * scene;
};

QGraphicsView 少点。你可能想使用非常小的半径使用 QGraphicsEllipse 项或简单地, scene-> addEllipseItem()

  #includemyqgraphicsview.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 c $ c>,使事件映射的 pos()正确到鼠标点击场景的位置。



如果要使用窗体,您需要向centralWidget的ui布局中添加子类化的 QGraphicsView 实例。

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

或者如果您的ui已经有一个布局,它将看起来像这样:

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

如果不使用 QMainWindow 和一个表单,如果您为它设置了布局,然后将 QGraphicsView 添加到 QWidget 该布局以类似的方式。如果你不想在 QGraphicsView 周围留一个边距,只需调用show就可以了,不要把它放在不同的布局中。

  #include< QtGui / QApplication> 
#includemyqgraphicsview.h

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

MyQGraphicsView视图;
view.show();

return a.exec();
}

就是这样。现在你是危险的 QGraphicsView 的和他们与鼠标的交互。



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


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

解决方案

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

http://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.

这篇关于如何在QGraphicsScene上绘制点(在mouseclick上)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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