I want to change the size of a window when press the minimize button instead of minimizing it .
-
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::changeEvent ( QEvent * event)
{
if(windowState() & Qt::WindowMinimized)
{
setWindowState(windowState() & ~Qt::WindowMinimized);
resize(size()*0.5);
}
}
@
But this code doesn't run as I supposed .
After I press the minimize button ,
the window minimizes before its size has been changed ,
and then appears with a shortened size .What should I do if I want to change the size of a window when press the minimize button instead of minimizing it .
enviroment :
Qt 4.7.2
ubuntu 11.04 -
I think you should accept the event in your code, after it has done your stuff. If you don't do that, the event is passed further down the event loop, and then the default handler minimizes the window. But if you accept, processing of this particular event stops:
@
event->accept();
@ -
No,
the event is send, when the window is minimized, not to minimize it.bq. The window's state (minimized, maximized or full-screen) has changed (QWindowStateChangeEvent).
So IMHO using changeEvent is too late.
On X11, internally for minimizing a window, the following event is send by QWidget:
@
XEvent e;
e.xclient.type = ClientMessage;
e.xclient.message_type = ATOM(WM_CHANGE_STATE);
e.xclient.display = X11->display;
e.xclient.window = data->winid;
e.xclient.format = 32;
e.xclient.data.l[0] = IconicState;
e.xclient.data.l[1] = 0;
e.xclient.data.l[2] = 0;
e.xclient.data.l[3] = 0;
e.xclient.data.l[4] = 0;
XSendEvent(X11->display,
RootWindow(X11->display,d->xinfo.screen()),
False, (SubstructureNotifyMask|SubstructureRedirectMask), &e);...
data->window_state = newstate;if (needShow) show(); if (newstate & Qt::WindowActive) activateWindow(); QWindowStateChangeEvent e(oldstate); QApplication::sendEvent(this, &e);
@
as you can see, the stateChange is the last thing.
You could overwrite "QWidget::x11Event":http://doc.qt.nokia.com/4.7/qwidget.html#x11Event to catch the minimize event and then you should be able to stop from propagating it.
-
@#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QDebug>
#include <qwindowdefs.h>
#include <Xlib.h>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}bool MainWindow::x11Event ( XEvent * e )
{
if(e->xclient.type == ClientMessage )
{
qDebug()<<e->xclient.type
<<e->xclient.message_type
<<e->xclient.display
<<e->xclient.window
<<e->xclient.format
<<e->xclient.data.l[0]
<<e->xclient.data.l[1]
<<e->xclient.data.l[2]
<<e->xclient.data.l[3]
<<e->xclient.data.l[4];
}
return false;
}@code like this .
Add these in .pro file:
@INCLUDEPATH += /usr/include/X11
LIBS += -L/usr/X11/lib -lX11@But I see nothing output before it minimized .
Is my code right ?
Or should I catch the minimize event somewhere else ?thank you .