C++ (Qt)#ifdef Q_WS_X11#include <QX11Info>#include <X11/Xlib.h>#include <X11/Xatom.h>#endif static void activateWindow( QWidget* w ){ if( !w->isShown() ) w->show(); w->raise(); #ifdef Q_WS_X11 Display* dpy = QX11Info::display(); XClientMessageEvent ev = { 0 }; ev.type = ClientMessage; ev.window = w->winId(); ev.message_type = XInternAtom( dpy, "_NET_ACTIVE_WINDOW", False ); ev.format = 32; ev.send_event = True; ev.data.l[1] = QX11Info::appUserTime(); XSendEvent( dpy, RootWindowOfScreen( DefaultScreenOfDisplay( dpy) ), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent*) &ev ); XSync( dpy, False ); #elif defined(Q_OS_WIN) SetWindowPos( w->winId(), HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE ); #endif w->activateWindow();} class YourMessageBox : public QMessageBox{ ... ... virtual void setVisible ( bool visible ) { QMessageBox::setVisible( visible ); if( visible ) activateWindow( this ); }};
C++ (Qt)class YourMainWindow : public QMainWindow{public: YourMainWindow( QWidget* parent = 0 ) : QMainWindow( parent ) { qApp->installEventFilter( this ); } bool eventFilter( QObject* o, QEvent* e ) { QWidget* w = o->isWidgetType() ? qobject_cast< QWidget* >( o ) : 0; if( e->type() == QEvent::ShowToParent && w && w->window() == w && ( w->windowType() == Qt::Window || w->windowType() == Qt::Dialog ) ) { activateWindow( w ); } return false; }};
C++ (Qt)class YourMainWindow : public QMainWindow{ //error: expected class-name before '{' tokenpublic: YourMainWindow( QWidget* parent = 0 ) : QMainWindow( parent ) //error: class 'YourMainWindow' does not have any field named 'QMainWindow' { qApp->installEventFilter( this ); //error: no matching function for call to 'QApplication::installEventFilter(YourMainWindow* const)' } bool eventFilter( QObject* o, QEvent* e ) { QWidget* w = o->isWidgetType() ? qobject_cast< QWidget* >( o ) : 0; if( e->type() == QEvent::ShowToParent && w && w->window() == w && ( w->windowType() == Qt::Window || w->windowType() == Qt::Dialog ) ) { activateWindow( w ); //error: 'activateWindow' was not declared in this scope } return false; }};