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

如何在 QGraphicsScene 上繪制一個(gè)點(diǎn)(鼠標(biāo)單擊)?

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

問題描述

我有以下代碼來設(shè)置 QGraphicsScene.我希望單擊場景并在我單擊的位置繪制一個(gè)點(diǎn).我怎么能這樣做?這是我當(dāng)前的代碼:

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

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() 所以無需手動映射坐標(biāo).

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 通過鼠標(biāo)按下或釋放鼠標(biāo)來執(zhí)行某些操作.

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 本身沒有無量綱點(diǎn).您可能希望使用 QGraphicsEllipse 項(xiàng)目或簡單地使用半徑非常小的 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() 正確映射到鼠標(biāo)在場景上點(diǎn)擊的位置.

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

如果要使用表單,則需要將子類 QGraphicsView 的實(shí)例添加到 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 已經(jīng)有布局,它將如下所示:

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

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

如果你不使用 QMainWindow 和一個(gè)表單,你可以將它添加到一個(gè) QWidget 如果你為它設(shè)置一個(gè)布局然后添加你的 QGraphicsView 以類似的方式到該布局.如果您不想在 QGraphicsView 周圍留出邊距,只需調(diào)用 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();
    }

就是這樣.現(xiàn)在您對 QGraphicsView 及其與鼠標(biāo)的交互很危險(xiǎn).

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

請務(wù)必閱讀和研究 Qt 的圖形視圖框架和相關(guān)examples在使用QGraphicsView 和 QGraphicsScene.它們是非常強(qiáng)大的 2D 圖形工具,可能需要一些學(xué)習(xí)曲線,但它們值得.

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.

這篇關(guān)于如何在 QGraphicsScene 上繪制一個(gè)點(diǎn)(鼠標(biāo)單擊)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 欧洲精品久久久久毛片完整版 | 免费的av网站 | 天天色综网 | 最新免费av网站 | 天天爽天天操 | 欧美成人精品激情在线观看 | 91精品国产综合久久久久 | 高清视频一区二区三区 | 黄色综合 | 中文字幕一级 | 亚洲成人精品影院 | 午夜成人在线视频 | 99九九久久| 一区二区三区中文字幕 | 精品在线观看一区 | 久久久久国产一级毛片高清网站 | 精品视频一区二区三区四区 | 久久久精品一区二区三区 | www.亚洲视频 | 精品久久网| 成人片免费看 | 日本精品久久 | 亚洲欧洲中文日韩 | 日韩在线观看视频一区 | 中文字幕视频在线观看 | 国产精品国产三级国产aⅴ无密码 | 97久久精品午夜一区二区 | 国产成人精品一区二区三 | 成人在线观看亚洲 | 欧美一级二级在线观看 | 亚洲精品久久久一区二区三区 | 国产高清一区二区三区 | 欧美一区二区三区在线播放 | 中文字幕在线一 | 日本午夜一区二区三区 | 久久久久久av | 久久国内精品 | 丁香婷婷综合激情五月色 | 天天干狠狠 | 国产亚洲黄色片 | 亚洲精品一区二区在线观看 |