久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 QGraphicsScene 上繪制一個點(鼠標單擊)?

How to draw a point (on mouseclick) on a QGraphicsScene?(如何在 QGraphicsScene 上繪制一個點(鼠標單擊)?)
本文介紹了如何在 QGraphicsScene 上繪制一個點(鼠標單擊)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有以下代碼來設置 QGraphicsScene.我希望單擊場景并在我單擊的位置繪制一個點.我怎么能這樣做?這是我當前的代碼:

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:有一個名為 QGraphicsSceneMouseEvent 的新類,它使這更容易一些.我剛剛在這里完成了一個使用它的例子:

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

它與下面的答案的不同之處在于它子類化 QGraphicsScene,而不是 QGraphicsView,并且它使用 mouseEvent->scenePos() 所以無需手動映射坐標.

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.

您需要子類化 QGraphicsView 才能使用 QMouseEvent 通過鼠標按下或釋放鼠標來執行某些操作.

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 本身沒有無量綱點.您可能希望使用 QGraphicsEllipse 項目或簡單地使用半徑非常小的 scene->addEllipseItem().

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

注意 mapToScene() 的用法,使事件的 pos() 正確映射到鼠標在場景上點擊的位置.

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

如果要使用表單,則需要將子類 QGraphicsView 的實例添加到 ui 的 centralWidget 布局中.

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

或者如果您的 ui 已經有布局,它將如下所示:

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

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

如果你不使用 QMainWindow 和一個表單,你可以將它添加到一個 QWidget 如果你為它設置一個布局然后添加你的 QGraphicsView 以類似的方式到該布局.如果您不想在 QGraphicsView 周圍留出邊距,只需調用 show 即可,不要將其放在不同的布局中.

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

就是這樣.現在您對 QGraphicsView 及其與鼠標的交互很危險.

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

請務必閱讀和研究 Qt 的圖形視圖框架和相關examples在使用QGraphicsView 和 QGraphicsScene.它們是非常強大的 2D 圖形工具,可能需要一些學習曲線,但它們值得.

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 上繪制一個點(鼠標單擊)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 韩日一级片 | 久久99热这里只频精品6学生 | 精品亚洲一区二区三区四区五区 | 特级毛片爽www免费版 | 久久久夜 | 日韩精品综合 | 欧美激情国产精品 | 人人草在线视频 | 免费精品视频 | 午夜大片 | 成年在线观看 | 亚洲视频一区在线观看 | 亚洲一区二区三区视频 | 三级中文字幕 | 9l视频自拍九色9l视频成人 | 欧美黄视频 | 成人免费黄色大片 | 性巴克成人免费网站 | 中文亚洲字幕 | 欧美一区二区三区的 | 亚洲视频在线观看一区 | 亚洲高清中文字幕 | 一本到av | 五月婷婷在线观看 | 狠狠草视频 | 亚洲欧美日韩一区二区三区四区 | 99视频网站 | 国产精品久久视频 | 天天射天天射 | 长河落日连续剧48集免费观看 | 伊人91| 97色在线 | 成人黄性视频 | 天天射av| 日本三极片 | 久久都是精品 | 亚色视频| 久久青青 | 青青草国产成人av片免费 | 特黄一级片 | 三级久久久|