pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何在 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 ()?環形?)
主站蜘蛛池模板: 臭氧灭菌箱-油桶加热箱-原料桶加热融化烘箱-南京腾阳干燥设备厂 臭氧发生器_臭氧消毒机 - 【同林品牌 实力厂家】 | LED投光灯-工矿灯-led路灯头-工业灯具 - 山东普瑞斯照明科技有限公司 | 德国BOSCH电磁阀-德国HERION电磁阀-JOUCOMATIC电磁阀|乾拓百科 | 济南律师,济南法律咨询,山东法律顾问-山东沃德律师事务所 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | AGV无人叉车_激光叉车AGV_仓储AGV小车_AGV无人搬运车-南昌IKV机器人有限公司[官网] | 多功能三相相位伏安表-变压器短路阻抗测试仪-上海妙定电气 | 合金耐磨锤头_破碎机锤头_郑州市德勤建材有限公司 | 电动葫芦-河北悍象起重机械有限公司 | 深圳品牌设计公司-LOGO设计公司-VI设计公司-未壳创意 | 色谱柱-淋洗液罐-巴罗克试剂槽-巴氏吸管-5ml样品瓶-SBS液氮冻存管-上海希言科学仪器有限公司 | 影合社-影视人的内容合作平台| 网络推广公司_网络营销方案策划_企业网络推广外包平台-上海澜推网络 | 首页-瓜尔胶系列-化工单体系列-油田压裂助剂-瓜尔胶厂家-山东广浦生物科技有限公司 | 雾度仪_雾度计_透光率雾度仪价格-三恩时(3nh)光电雾度仪厂家 | 广西教师资格网-广西教师资格证考试网| 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 招商帮-一站式网络营销服务|搜索营销推广|信息流推广|短视视频营销推广|互联网整合营销|网络推广代运营|招商帮企业招商好帮手 | 亚克力制品定制,上海嘉定有机玻璃加工制作生产厂家—官网 | 滑石粉,滑石粉厂家,超细滑石粉-莱州圣凯滑石有限公司 | 合肥钣金加工-安徽激光切割加工-机箱机柜加工厂家-合肥通快 | 橡胶接头_橡胶软接头_套管伸缩器_管道伸缩器厂家-巩义市远大供水材料有限公司 | 合肥展厅设计-安徽展台设计-合肥展览公司-安徽奥美展览工程有限公司 | 硬齿面减速机[型号全],ZQ减速机-淄博久增机械 | 罗茨真空机组,立式无油往复真空泵,2BV水环真空泵-力侨真空科技 | 广州展览制作|展台制作工厂|展览设计制作|展览展示制作|搭建制作公司 | 船用泵,船用离心泵,船用喷射泵,泰州隆华船舶设备有限公司 | 伺服电机_直流伺服_交流伺服_DD马达_拓达官方网站 | 袋式过滤器,自清洗过滤器,保安过滤器,篮式过滤器,气体过滤器,全自动过滤器,反冲洗过滤器,管道过滤器,无锡驰业环保科技有限公司 | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 上海小程序开发-上海小程序制作公司-上海网站建设-公众号开发运营-软件外包公司-咏熠科技 | 微型驱动系统解决方案-深圳市兆威机电股份有限公司 | 电主轴,车床电磨头,变频制动电机-博山鸿达特种电机 | 佛山市德信昌电子有限公司| 深圳办公室装修,办公楼/写字楼装修设计,一级资质 - ADD写艺 | 微水泥_硅藻泥_艺术涂料_艺术漆_艺术漆加盟-青岛泥之韵环保壁材 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | 实木家具_实木家具定制_全屋定制_美式家具_圣蒂斯堡官网 | 冷凝水循环试验箱-冷凝水试验箱-可编程高低温试验箱厂家-上海巨为(www.juweigroup.com) | 硬度计,金相磨抛机_厂家-莱州华煜众信试验仪器有限公司 | 匀胶机旋涂仪-声扫显微镜-工业水浸超声-安赛斯(北京)科技有限公司 |