moving QWidget and Mainform
Hi, I insert this QWidget in main.cpp and I move it beside myMainForm (). I'd like, when I move with mouse the windows application, that mw2 moving automatically (and take it beside mainForm); now when I move windows app, the position of mw2 don't change (it is fixed). Is it possible? Are you understand? thanks
Code:
myMainForm w;
a.setMainWidget(&w);
w.show();
mw2.setGeometry(0,0,200,400);
mw2.
move(w.
mapToGlobal(QPoint(w.
width(),
50)));
Re: moving QWidget and Mainform
Install event filter for main widget and then when catch mouseMove event move your second widget
1 Attachment(s)
Re: moving QWidget and Mainform
mmm When I said move mouse I refer click on the bar of application (with mouse down) and move mouse; this changing the position on my application on the screen...
In pics: when I move window on the left, I'd like that the window of right follow the first. thanks
Re: moving QWidget and Mainform
Yes, that can be done by an event filter, as zlatko already said.
You should catch move events though, not mouse move events..
mywidget.h:
mywidget.cpp:
Code:
if (e
->type
() == QEvent::Move) { QMoveEvent* moveEvent
= static_cast<QMoveEvent
*>
(e
);
// move this widget here
// (moveEvent->pos() contains the position of main window)
}
return FALSE;
}
Code:
myMainForm w;
...
MyWidget mw2;
...
w.installEventFilter(&mw2);
So the widget filters all events going to the main form.
Whenever the main form is moved, the widget notices the move event and follows..
Re: moving QWidget and Mainform
I changed the line below
Code:
//main.cpp
exw.
move(w.
mapToGlobal(QPoint(w.
width(),
50)));
to:
Code:
//externWidget::installFilter
this
->move
(w.
mapToGlobal(QPoint(w.
width(),
50)));
but while the first set the posiion of extern widget beside window App (see pictures), if I put it within installFilter the position it's not the same (and if I move main window, externWidget move out screen very fast!) Why this?
Re: moving QWidget and Mainform
Window Geometry
Code:
exw.move(w.frameGeometry().right(), w.frameGeometry().center().y() - (exw.frameGeometry().height() / 2));
Re: moving QWidget and Mainform
thanks, but its behavior is the same of mapTo.....I'll try again....
Re: moving QWidget and Mainform
You don't have to translate (map) the widget coordinates at all, because both of your widgets seem to be top level widgets.
Maybe this is more readable and understandable format:
Code:
// get frame geometries of both widgets
QRect lefttRect
= leftWidget
->frameGeometry
();
QRect rightRect
= rightWidget
->frameGeometry
();
// move the widget on the left to the left side of the widget on the right
leftRect.moveCenter(rightRect.center());
leftRect.moveRight(rightRect.left());
leftWidget->move(leftRect.topLeft());
// OR
// move the widget on the right to the right side of the widget on the left
rightRect.moveCenter(leftRect.center());
rightRect.moveLeft(leftRect.right());
rightWidget->move(rightRect.topLeft());
Re: moving QWidget and Mainform
ok. the problem is: if I put exw.move (...) within lEventFilter don't works bacause the width and height of MainForm aren't true; if I put move inside main.cpp, it works because width and hieght are true! But why this??
Re: moving QWidget and Mainform
The problem was this: if I need to declare in the second way but doing so, don't appear the window bar (window title). How can do?
Code:
ExternWidget exw (0,""); // this don't get parent w to ExternWidget
ExternWidget exw (&w,"");