0
votes

I have a QMainWindow-derived Widget that contains a QGLWidget-derived Widget. I have a custom Event that will get fired from within the GLWidget's paintGL() method that way (the notification shall be asynchonous, that's why I'd like to post an event):

QCoreApplication::postEvent(parent(),new ColpickEvent(rgb, pickPoint));

The event looks like this (very simple, no capsuling of data):

#define COLORPICK_EVENT QEvent::Type(31337)
class ColpickEvent : public QEvent
{
public:
    ColpickEvent(QRgb col, const QPoint& pos)
        : QEvent(COLORPICK_EVENT), Color(col), PixelPos(pos) {};
    QColor  Color;
    QPoint  PixelPos;
};

The MainWindow overrides QObject::event(QEvent*) like this:

#define EVENT_TYPE(ClassName) { ClassName *e = static_cast<ClassName*>(event)
#define END_EVENT } break
#define ACCEPT_EVENT e->accept(); return true
bool MainWindow::event(QEvent* event)
{
    switch(event->type())
    {
    /*... handle other events ...*/
    case COLORPICK_EVENT:
        EVENT_TYPE(ColpickEvent);
        // do something with the data provided by the event ...
        ACCEPT_EVENT;
        END_EVENT;
    /*... handle other events ...*/
    }
    return QMainWindow::event(event);
}

What happens is, that my ColpickEvent never seems to get dispatched to MainWindow. Any ideas why this happens (or actually doesn't happen)?

1
you should post an answer with your solution and accept it so it's clearer for future visitorsratchet freak
You're right, but I found my solution too quickly - I wasn't allowed to add an answer by myself for 8 hours.St0fF

1 Answers

0
votes

OOPS.

As the top level widget was derived from QMainWindow, it needed to have a centralWidget. Somehow it wasn't possible to set the GLWidget as centralWidget, as I needed a Layout to overlay other widgets on top of it. So the centralWidget of MainWindow is a simple QWidget "w" with a GridLayout. The GLWidget is added to the Layout, that way its parent was changed to "w".

Because of this, the solution is simply enough:

QCoreApplication::postEvent(parent()->parent(),new ColpickEvent(rgb, pickPoint));

(see the double-parent-call).