PDA

View Full Version : moving QWidget and Mainform



mickey
5th April 2006, 11:40
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


myMainForm w;
a.setMainWidget(&w);
w.show();
QWidget mw2;
mw2.setGeometry(0,0,200,400);
mw2.move(w.mapToGlobal(QPoint(w.width(),50)));

zlatko
5th April 2006, 11:50
Install event filter for main widget and then when catch mouseMove event move your second widget

mickey
5th April 2006, 15:30
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

jpn
5th April 2006, 16:23
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:


class MyWidget : public QWidget {
public:
...
bool eventFilter(QObject* o, QEvent* e);
....
}


mywidget.cpp:


bool MyWidget::eventFilter(QObject* o, QEvent* e) {
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;
}




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..

mickey
5th April 2006, 21:32
I changed the line below


//main.cpp
exw.move(w.mapToGlobal(QPoint(w.width(),50)));


to:


//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?

jpn
5th April 2006, 21:57
Window Geometry (http://doc.trolltech.com/3.3/geometry.html)


exw.move(w.frameGeometry().right(), w.frameGeometry().center().y() - (exw.frameGeometry().height() / 2));

mickey
5th April 2006, 23:24
thanks, but its behavior is the same of mapTo.....I'll try again....

jpn
6th April 2006, 09:37
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:


// 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());

mickey
6th April 2006, 16:57
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??

mickey
6th April 2006, 17:54
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?


ExternWidget exw (0,""); // this don't get parent w to ExternWidget
ExternWidget exw (&w,"");