Results 1 to 10 of 10

Thread: moving QWidget and Mainform

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default 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
    Qt Code:
    1. myMainForm w;
    2. a.setMainWidget(&w);
    3. w.show();
    4. QWidget mw2;
    5. mw2.setGeometry(0,0,200,400);
    6. mw2.move(w.mapToGlobal(QPoint(w.width(),50)));
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: moving QWidget and Mainform

    Install event filter for main widget and then when catch mouseMove event move your second widget
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default 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
    Attached Images Attached Images
    • File Type: jpg v.jpg (7.4 KB, 33 views)
    Last edited by mickey; 5th April 2006 at 16:36.
    Regards

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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:
    Qt Code:
    1. class MyWidget : public QWidget {
    2. public:
    3. ...
    4. bool eventFilter(QObject* o, QEvent* e);
    5. ....
    6. }
    To copy to clipboard, switch view to plain text mode 

    mywidget.cpp:
    Qt Code:
    1. bool MyWidget::eventFilter(QObject* o, QEvent* e) {
    2. if (e->type() == QEvent::Move) {
    3. QMoveEvent* moveEvent = static_cast<QMoveEvent*>(e);
    4. // move this widget here
    5. // (moveEvent->pos() contains the position of main window)
    6. }
    7. return FALSE;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. myMainForm w;
    2. ...
    3. MyWidget mw2;
    4. ...
    5. w.installEventFilter(&mw2);
    To copy to clipboard, switch view to plain text mode 

    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..
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: moving QWidget and Mainform

    I changed the line below
    Qt Code:
    1. //main.cpp
    2. exw.move(w.mapToGlobal(QPoint(w.width(),50)));
    To copy to clipboard, switch view to plain text mode 

    to:
    Qt Code:
    1. //externWidget::installFilter
    2. this->move(w.mapToGlobal(QPoint(w.width(),50)));
    To copy to clipboard, switch view to plain text mode 

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

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: moving QWidget and Mainform

    Window Geometry

    Qt Code:
    1. exw.move(w.frameGeometry().right(), w.frameGeometry().center().y() - (exw.frameGeometry().height() / 2));
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: moving QWidget and Mainform

    thanks, but its behavior is the same of mapTo.....I'll try again....
    Regards

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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:
    Qt Code:
    1. // get frame geometries of both widgets
    2. QRect lefttRect = leftWidget->frameGeometry();
    3. QRect rightRect = rightWidget->frameGeometry();
    4.  
    5. // move the widget on the left to the left side of the widget on the right
    6. leftRect.moveCenter(rightRect.center());
    7. leftRect.moveRight(rightRect.left());
    8. leftWidget->move(leftRect.topLeft());
    9.  
    10. // OR
    11.  
    12. // move the widget on the right to the right side of the widget on the left
    13. rightRect.moveCenter(leftRect.center());
    14. rightRect.moveLeft(leftRect.right());
    15. rightWidget->move(rightRect.topLeft());
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default 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??
    Regards

  10. #10
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default 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?
    Qt Code:
    1. ExternWidget exw (0,""); // this don't get parent w to ExternWidget
    2. ExternWidget exw (&w,"");
    To copy to clipboard, switch view to plain text mode 
    Regards

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.