Important: Please read the Qt Code of Conduct -

mousePressEvent in top-level QRubberBand

  • 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 {
            WidgetTest(QWidget* parent=0);
            void mousePressEvent(QMouseEvent* e); 
    class RBTest : public QRubberBand {
            RBTest(Shape s, QWidget* parent=0);
            void mousePressEvent(QMouseEvent* e); 

    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);
        return app.exec();

  • Lifetime Qt Champion

    Hi and welcome to devnet,

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

  • @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:


  • Lifetime Qt Champion

    I don't have permission to access that file.

  • Sorry about that. Permissions are now set to public.

  • Lifetime Qt Champion

    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.

  • 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?

  • Lifetime Qt Champion

    @pearse said:


    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?

  • 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) {

  • Lifetime Qt Champion

    @mrjj said:


    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.

  • Lifetime Qt Champion

    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 ?

  • 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:

  • Lifetime Qt Champion

    Win 10, mingw, Qt 5.6
    It does work

  • 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?

  • Lifetime Qt Champion

    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 ?

  • 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.

  • Lifetime Qt Champion


    • 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
      and when/if u think its a bug, create report.

  • Lifetime Qt Champion

    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.

  • 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 :-(

Log in to reply