PDA

View Full Version : Which event when raising a window



Caius Aérobus
26th March 2012, 10:53
Hello,
I have 2 main windows and I would like that each time one of them is raised by the user - by clicking on it - the other one be raised so as to always have all windows either visible or not, ie not hidden by another application (otherwise I have to raise each window individually).
I was about to solve it by overloading the changeEvent() method but this method is not called when a window is raised by clicking on it. Any idea?



void
myClass::changeEvent(QEvent *event)
{
if (event->type() == QEvent::WindowStateChange) {
if (this->windowState() & Qt::WindowActive) {
printf("raised\n");
}
QWidget::changeEvent(event);
}
}

wysota
26th March 2012, 11:14
Are you aware that clicking on a window doesn't have to bring it to front? This depends on the settings of the window manager. I don't think there is an event that handles what you want. You might try your luck with monitoring QApplication::focusChanged() signal however it is related to keyboard focus and not window visibility. You can also try with your current approach but so far you have been looking for a wrong event -- try checking for QEvent::ActivationChange instead.

Caius Aérobus
26th March 2012, 11:31
Tried it but did not change anything, ie slot is not called.

wysota
26th March 2012, 12:44
What slot do you mean?

Caius Aérobus
26th March 2012, 13:05
According to your suggestion, I tried this:



void
myClass::changeEvent(QEvent *event)
{
if (event->type() == QEvent::ActivationChange) {
if (this->windowState() & Qt::WindowActive) {
printf("raised\n");
}
QWidget::changeEvent(event);
}
}


and I expected that clicking on the upper band of the window, making it the active window, would have made this slot executed.

wysota
26th March 2012, 14:19
It's not a slot.

And the docs say:


QEvent::ActivationChange 99 A widget's top-level window activation state has changed.

Again, it has nothing to do with the window being raised or not. Activation is related to the keyboard focus.

Caius Aérobus
26th March 2012, 15:37
You wrote:


try checking for QEvent::ActivationChange instead

so that is the reason I tried it!

wysota
26th March 2012, 15:48
I have also written other things. Have you checked them?

Caius Aérobus
26th March 2012, 16:16
Yes I understood that it is not possible to have all windows of an application raised together when one of them has been raised.
I just investigated this route since you mentionned it.

wysota
26th March 2012, 17:03
I meant the focusChanged() signal.

Spitfire
28th March 2012, 14:09
I don't know about chaning focus wysota mentioned, but changeEvent works perfectly fine on w7 an ubuntu for me:



// main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

MainWindow w2;
w2.show();

QObject::connect( &w, SIGNAL( activated( QWidget* ) ), &w2, SLOT( activate( QWidget* ) ) );
QObject::connect( &w2, SIGNAL( activated( QWidget* ) ), &w, SLOT( activate( QWidget* ) ) );

return a.exec();
}




// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow( QWidget* parent = 0 );

protected:
void changeEvent( QEvent* e );

public slots:
void activate( QWidget* w );

signals:
void activated( QWidget* w );
};

#endif // MAINWINDOW_H




// mainwindow.cpp
#include "mainwindow.h"

#include <QEvent>

MainWindow::MainWindow( QWidget* p )
:
QMainWindow( p )
{
}

void MainWindow::changeEvent( QEvent* e )
{
if( e->type() == QEvent::ActivationChange
&& this->isActiveWindow() )
{
emit this->activated( this );
}
QMainWindow::changeEvent( e );
}

void MainWindow::activate( QWidget* w )
{
this->raise();
this->stackUnder( w );
}

JFPERREARD
7th January 2016, 16:32
Very good, thank you for this help,
Ok with QT5 on Windows 8