Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] mousePressEvent in QtQuickView
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] mousePressEvent in QtQuickView

Scheduled Pinned Locked Moved Solved QML and Qt Quick
mousepresseventeventqwindowqquickwindowqquickview
13 Posts 2 Posters 2.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #2

    Show us a minimal class and qml file the view is used in. Examples don't help. It could be a lot of simple things. Create a small side project that has the problem and post that.

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      madoodia
      wrote on last edited by madoodia
      #3

      I got you, but it is not a minimal, because I don't know problem is because of opengl or not
      btw the underlay sample in kdab repo is really minimal

      underlay is using QQuickView as base class and use lib for Rendering, however we don't need to check the lib/meshrenderer
      if we override any event in the QQuickView class it can not detect it and no action on it

      I just added this to the header file

      protected:
        void mousePressEvent(QMouseEvent *e) override;
      

      and this to cpp file to check the event

      void MyQuickView::mousePressEvent(QMouseEvent *e)
      {
        //   qApp->sendEvent(mWindow, e);
        if (!e->isAccepted())
        {
          qDebug("Hello");
          QQuickView::mousePressEvent(e);
        }
      }
      

      and didn't touch the main.qml file

      technically it should work because QQuickView class has virtual functions as events that is inherited from QWindow
      https://doc.qt.io/qt-5/qquickview.html#reimplemented-protected-functions

      make some sense?
      thanks

      1 Reply Last reply
      0
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #4

        main.qml:

        import QtQuick 2.12
        import QtQuick.Window 2.12
        
        import QuickTypes 1.0
        
        MouseItem {
            visible: true
            width: 640
            height: 480
            title: qsTr("Mouse Item test")
        }
        

        main.cpp:

        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        
        #include <mouseitem.h>
        
        int main(int argc, char *argv[])
        {
            QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        
            QGuiApplication app(argc, argv);
        
            qmlRegisterType<MouseItem>("QuickTypes",1,0,"MouseItem");
        
            QQmlApplicationEngine engine;
            const QUrl url(QStringLiteral("qrc:/main.qml"));
            QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                             &app, [url](QObject *obj, const QUrl &objUrl) {
                if (!obj && url == objUrl)
                    QCoreApplication::exit(-1);
            }, Qt::QueuedConnection);
            engine.load(url);
        
            return app.exec();
        }
        

        mouseitem.h:

        #ifndef MOUSEITEM_H
        #define MOUSEITEM_H
        
        #include <QQuickView>
        
        class MouseItem : public QQuickView
        {
            Q_OBJECT
        public:
            MouseItem();
        
        protected:
            void mousePressEvent(QMouseEvent *e) override;
        
        };
        
        #endif // MOUSEITEM_H
        

        mouseitem.cpp:

        #include "mouseitem.h"
        
        #include <QDebug>
        
        MouseItem::MouseItem()
            : QQuickView()
        {
        
        }
        
        void MouseItem::mousePressEvent(QMouseEvent *e)
        {
          //   qApp->sendEvent(mWindow, e);
          qDebug() << "mousePressEvent";
          if (!e->isAccepted())
          {
            qDebug("Hello");
            QQuickView::mousePressEvent(e);
          }
        }
        

        I see the text "mousePressEvent", but I don't see the hello. So the events are happening.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #5

          Is says here accepted is set true by default. I think if you dont want to accept the event you set it to false (setAccepted). If not you set to true? Not entirely sure.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            madoodia
            wrote on last edited by
            #6

            TBH, I did your method without !e->isAccepted() too, it didn't work
            now it works now, even

            void MouseItem::mousePressEvent(QMouseEvent *e)
            {
              qDebug() << "mousePressEvent";
              qDebug("mousePressEvent");
            }
            

            don't understand
            but there is another problem that it works, and print the message when click on the window, but it freezes the qml controllers
            this is weird.
            will test other events and will leave comment if there is any problem

            1 Reply Last reply
            0
            • M Offline
              M Offline
              madoodia
              wrote on last edited by
              #7

              hey @fcarney
              what if your MouseItem inherites from QQuickItem
              and contain another QQuickWindow or QQuickView class in it, then register the Item?

              you know what I mean?
              A window inside an Item in qml

              1 Reply Last reply
              0
              • fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on last edited by
                #8

                freezes the qml controllers

                It might be eating all the events. setAccepted to false will be like you didnt handle the event, I think.
                Also, I don't know if you are supposed to embed an Windows in an Item.

                C++ is a perfectly valid school of magic.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  madoodia
                  wrote on last edited by
                  #9

                  it does not reach to that point to check the setAccepted, it does not call mousePressEvent (or any other events) at all
                  when you use a Q*Window (object) inside a QQuickItem

                  How can we check this? that item will disable window events or not?
                  any idea?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    madoodia
                    wrote on last edited by
                    #10

                    This is the more specific question about the mousePressEvent problem
                    https://forum.qt.io/topic/113640/why-q-window-events-are-eaten-by-qml-item

                    1 Reply Last reply
                    0
                    • fcarneyF Offline
                      fcarneyF Offline
                      fcarney
                      wrote on last edited by
                      #11

                      Have you tried doing your opengl in a qtquickitem? I wonder if the problem is trying to embed a top level window inside a a qml item.

                      C++ is a perfectly valid school of magic.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        madoodia
                        wrote on last edited by
                        #12

                        I did, it works fine and show window contents (3D OpenGL) inside the Item, just I found we should enable mouse accepting, I'm still testing.
                        will update the post

                        thanks

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          madoodia
                          wrote on last edited by
                          #13

                          The problem is solved
                          please check this
                          https://forum.qt.io/topic/113640/solved-why-q-window-events-are-eaten-by-qml-item

                          thanks

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved