Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. mousePressEvent in top-level QRubberBand
Forum Updated to NodeBB v4.3 + New Features

mousePressEvent in top-level QRubberBand

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 4 Posters 4.6k Views 3 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.
  • pearseP Offline
    pearseP Offline
    pearse
    wrote on last edited by
    #1

    I seem to be unable to override the QWidget::mousePressEvent with my top-level QRubberBand. I can effectively do this with a top-level QWidget.

    My rubber band must be top-level (parent=0) because I am trying to draw it over a QGLWidget, which paints over any rubberband that is not top-level.

    The following code will print "widget pressed" when the top-level QWidget is clicked, but will not print "rubber band pressed" when the QRubberBand is. Any help here is very much appreciated. Thank you.

    Header Code:

    #ifndef RBTEST_H
    #define RBTEST_H
    
    #include <QWidget>
    #include <QRubberBand>
    
    class WidgetTest : public QWidget {
        Q_OBJECT
        public:
            WidgetTest(QWidget* parent=0);
            void mousePressEvent(QMouseEvent* e); 
    };
    
    class RBTest : public QRubberBand {
        Q_OBJECT
        public:
            RBTest(Shape s, QWidget* parent=0);
            void mousePressEvent(QMouseEvent* e); 
    };
    
    #endif
    

    cpp code:

    #include <iostream>
    #include <QtGui>
    #include "rbTest.h"
    
    using namespace std;
    
    WidgetTest::WidgetTest(QWidget* parent) : QWidget(parent) {}
    
    void WidgetTest::mousePressEvent(QMouseEvent* e) {
        cout << "widget pressed" << endl;
    }
    
    RBTest::RBTest(Shape s, QWidget* parent) : QRubberBand(s, parent) {}
    
    void RBTest::mousePressEvent(QMouseEvent* e) {
        cout << "rubber band pressed" << endl;
    }
    

    Main file code:

    #include <QApplication>
    #include <iostream>
    #include "rbTest.h"
    
    using namespace std;
    
    int main(int argc, char* argv[]) {
        QApplication app(argc, argv);
        RBTest* rbt = new RBTest(QRubberBand::Rectangle, 0); 
        WidgetTest* wt = new WidgetTest(0);
            
        rbt->show();
        wt->show();
            
        return app.exec();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Can you provide a minimal sample application that shows your issue with an OpenGL widget and QRubberBand ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      pearseP 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Can you provide a minimal sample application that shows your issue with an OpenGL widget and QRubberBand ?

        pearseP Offline
        pearseP Offline
        pearse
        wrote on last edited by pearse
        #3

        @SGaist Yes, the sample code is in the original post. Can you see it? There are three code tags, one for rbTest.h, the next for rbTest.cpp, and the final one is for main.cpp.

        Alternatively, you can find the files in the zip file linked below:
        https://drive.google.com/a/ucar.edu/file/d/0B44_2BA1czYlS1Y4WERpS3M0djQ/view?usp=sharing

        Thanks!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I don't have permission to access that file.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • pearseP Offline
            pearseP Offline
            pearse
            wrote on last edited by
            #5

            Sorry about that. Permissions are now set to public.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I've moved the rubber band a bit to separate it clearly from the widget and when I click on it I get the message from the mouse press event.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • pearseP Offline
                pearseP Offline
                pearse
                wrote on last edited by
                #7

                What OS are you on? And could you please elaborate one what you did to separate the two widgets? I am unable to reproduce your method.

                Is it not possible to have a top level rubber band over another widget and still receive click events?

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @pearse said:

                  QRubberBand

                  Hi
                  Im not sure if helpful but it does set

                  QRubberBand::QRubberBand(Shape s, QWidget *p)
                  : QWidget(*new QRubberBandPrivate, p, (p && p->windowType() != Qt::Desktop) ? Qt::Widget : RUBBERBAND_WINDOW_TYPE)
                  {
                  ...
                  setAttribute(Qt::WA_TransparentForMouseEvents); <<< is that ok ?

                  Im not sure if poster want mousedown on the Band OR the widget under?

                  1 Reply Last reply
                  0
                  • pearseP Offline
                    pearseP Offline
                    pearse
                    wrote on last edited by
                    #9

                    Hi mrjj. Thank you for your time and help, and thank you too, to SGaist. I'm trying to overlay a persistent rubberband over the entire application, and capture any mouse event upon it so it can be moved.

                    I've tried applying your constructor but I cannot seem to compile it. Does your constructor variable "QRubberBandPrivate" specify my overridden rubber-band (RBTest)?

                    I also don't see documentation on the QWidget constructor that we're inheriting from here, the only one I do see only takes two arguments.

                    I receive the following errors regarding QRubberBandPrivate:

                    rbTest.cpp:28:16: error: allocation of incomplete type 'QRubberBandPrivate'
                    : QWidget(*new QRubberBandPrivate(s,p), p, (p && p->windowType() != Qt::Desktop) ? Qt::Widget : RUBBERBAND_WINDOW_TYPE) {

                    mrjjM 1 Reply Last reply
                    0
                    • pearseP pearse

                      Hi mrjj. Thank you for your time and help, and thank you too, to SGaist. I'm trying to overlay a persistent rubberband over the entire application, and capture any mouse event upon it so it can be moved.

                      I've tried applying your constructor but I cannot seem to compile it. Does your constructor variable "QRubberBandPrivate" specify my overridden rubber-band (RBTest)?

                      I also don't see documentation on the QWidget constructor that we're inheriting from here, the only one I do see only takes two arguments.

                      I receive the following errors regarding QRubberBandPrivate:

                      rbTest.cpp:28:16: error: allocation of incomplete type 'QRubberBandPrivate'
                      : QWidget(*new QRubberBandPrivate(s,p), p, (p && p->windowType() != Qt::Desktop) ? Qt::Widget : RUBBERBAND_WINDOW_TYPE) {

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @mrjj said:

                      setAttribute(Qt::WA_TransparentForMouseEvents);

                      Hi
                      i was showing the call setAttribute and wonder if you want to click inside the band ?
                      This flag disable mouse events for the widget as far as i know.
                      I didnt mean you to use the constructor. the QRubberBandPrivate is not to be used outside so to speak.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        I tested on OS X.

                        Nothing really special, I've set a geometry on the rubber band that was different that the one of the widget.

                        What do you want to do with that rubber band ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • pearseP Offline
                          pearseP Offline
                          pearse
                          wrote on last edited by
                          #12

                          Hey guys, maybe I haven't been clear in my responses. I've made a youtube video that demonstrates the incorrect behavior I'm seeing. I hope this clears up what I'm trying to do:

                          https://youtu.be/_Xo8xksg1oY

                          mrjjM 1 Reply Last reply
                          1
                          • pearseP pearse

                            Hey guys, maybe I haven't been clear in my responses. I've made a youtube video that demonstrates the incorrect behavior I'm seeing. I hope this clears up what I'm trying to do:

                            https://youtu.be/_Xo8xksg1oY

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @pearse
                            Hi
                            Win 10, mingw, Qt 5.6
                            It does work

                            1 Reply Last reply
                            0
                            • pearseP Offline
                              pearseP Offline
                              pearse
                              wrote on last edited by
                              #14

                              I should have specified that I'm on QT 4.8. Still, I don't think the behavior would change between versions.

                              Is this a bug?

                              mrjjM 1 Reply Last reply
                              0
                              • pearseP pearse

                                I should have specified that I'm on QT 4.8. Still, I don't think the behavior would change between versions.

                                Is this a bug?

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @pearse
                                well could be bug in Qt 4.8 on OSX.
                                But maybe you could test with a newer Qt and see if its fixed in later releases ?

                                1 Reply Last reply
                                0
                                • pearseP Offline
                                  pearseP Offline
                                  pearse
                                  wrote on last edited by
                                  #16

                                  Unfortunately I'm developing on a 12 year old code base built on Qt 4.8. We've looked into upgrading to 5.X but have found incompatibility issues. This is very unfortunate :(

                                  Is there a bug reporting mechanism that I can use to file this problem? Thank you again for your help.

                                  mrjjM 1 Reply Last reply
                                  0
                                  • pearseP pearse

                                    Unfortunately I'm developing on a 12 year old code base built on Qt 4.8. We've looked into upgrading to 5.X but have found incompatibility issues. This is very unfortunate :(

                                    Is there a bug reporting mechanism that I can use to file this problem? Thank you again for your help.

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #17

                                    @pearse

                                    • Unfortunately I'm developing on a 12 year old code base built on Qt 4.8
                                      I didnt mean whole project. just the test project. To validate if 4.8 issue.
                                      You can also look for similar sounding bugs
                                      https://bugreports.qt.io/secure/Dashboard.jspa
                                      and when/if u think its a bug, create report.
                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      One thing to take into account: Qt 4.8.7 was the last release and there will be no more Qt 4.8 releases in the future except if there's a security issues.

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • pirannaP Offline
                                        pirannaP Offline
                                        piranna
                                        wrote on last edited by
                                        #19

                                        I've got the same problem on Ubuntu 16.04 and 16.10 on a new project, where the QrubberBand not get the mouse events and they go through to the underlying widget. Is there any workaround for that problem?

                                        I wanted to use Qt 5.7, but Ubuntu in its latest version only have up to Qt 5.5 and I've not be able to find documentation for that, so I've needed to stick with 4.8 :-(

                                        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