Qt sends a signal to the main application window

I need a QDialog to send a signal to redraw the main window.
But to connect, you need an object to connect.
Therefore, I have to create each dialog with a new one and explicitly put connect () every time.

I really need to just send MainWindow :: Redraw () from any function and have one connection () inside Mainwindow to receive them.

But you cannot make the signal static, and dialogs are clearly not inherited from MainWindow.

Editing:
Thank you - I do not want to bypass the signal / slots. I want to get around using the singleton main application pointer, for example afxGetApp (). But I don’t understand how easy it is to send a signal and start it (or down?) In mainwindow, where I will catch it. I portrayed signals / slots as exceptions

+4
source share
3 answers

Let clients send CustomRedrawEvents to QCoreApplication.

class CustomRedrawEvent : public QEvent { public: static Type registeredEventType() { static Type myType = static_cast<QEvent::Type>(QEvent::registerEventType()); return myType; } CustomRedrawEvent() : QEvent(registeredEventType()) { } }; void redrawEvent() { QCoreApplication::postEvent( QCoreApplication::instance(), new CustomRedrawEvent()); } 

Install the event in an instance of CoreApplication and connect to the rewrite signal:

 class CustomRedrawEventFilter : public QObject { Q_OBJECT public: CustomRedrawEventFilter(QObject *const parent) : QObject(parent) { } signals: void redraw(); protected: bool eventFilter(QObject *obj, QEvent *event) { if( event && (event->type()==CustomRedrawEvent::registeredEventType())) { emit redraw(); return true; } return QObject::eventFilter(obj, event); } }; //main() QMainWindow mainWindow; QCoreApplication *const coreApp = QCoreApplication::instance(); CustomRedrawEventFilter *const eventFilter(new CustomRedrawEventFilter(coreApp)); coreApp->installEventFilter(eventFilter); mainWindow.connect(eventFilter, SIGNAL(redraw()), SLOT(update())); 
+3
source

An easy way to do this would be to simply call repaint () for all widgets returned by the static QApplication :: topLevelWidgets () method. This avoids the need for signals and slots.

+1
source

If you are looking for a parallel normal Qt idiom, you can point the global pointer to mainwindow. This should give you the necessary functionality if I understand you correctly.

0
source

Source: https://habr.com/ru/post/1304803/


All Articles