Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Limit Window Movement

  1. #1
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Limit Window Movement

    I am trying to limit the area that a dialog box can be moved. I am developing a program with many areas to it, one area being a tab widget. I would like to keep all the popups within the tab that called them. Does anyone have any suggestions? I still want to make them mobile, so I don't really want the parent child relationship.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limit Window Movement

    Override moveEvent and ignore it if the dialog coordinates are outside a predefined area.
    Be careful that in the QMoveEvent you get the new position.

    Regards

  3. #3
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Re: Limit Window Movement

    Thank you I will try that

  4. #4
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Re: Limit Window Movement

    void tmp::moveEvent(QMoveEvent * pEvent)
    {
    pEvent->ignore();
    }

    am I looking at this wrong wouldn't the above code stop me from being able to move the dialog? If it did I could just wrap it in an if statement, but it wont stop me from moving my dialog box. Could you please tell me what I am doing wrong?

  5. #5
    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: Limit Window Movement

    From QWidget::moveEvent() docs:
    When the widget receives this event, it is already at the new position.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    abbapatris (17th July 2007)

  7. #6
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Re: Limit Window Movement

    I guess I am a little confused how over writing the moveEvent would help then if it takes effect after the move. I am sorry I am really new to Qt.

  8. #7
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Limit Window Movement

    Quote Originally Posted by abbapatris View Post
    I guess I am a little confused how over writing the moveEvent would help then if it takes effect after the move. I am sorry I am really new to Qt.

    Well, moveEvent() is meant to tell you : "Hey, the widget has moved ! Here's its new position"

    Of course, it won't solve your problem here, as jpn showed you.

    Maybe you could override mouseMoveEvent(), and in this method, determine if a button is pressed on the window titlebar, for example (you have button() / buttons() to obtain the buttons states)

    If the window has reached the boundaries you set, you reject the event.
    I haven't tested so...

  9. The following user says thank you to guilugi for this useful post:

    abbapatris (17th July 2007)

  10. #8
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Re: Limit Window Movement

    Thanks, I am tryign to figure that out. I didn't think it would be this difficult to implement. I figured there would be a way to put its parameters cause there are already parameters implemented with the monitor.

  11. #9
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Re: Limit Window Movement

    I have found a way to make it work using QMoveEvent. Its not pretty but it works.

    Qt Code:
    1. void LimitMovement::moveEvent(QMoveEvent * pEvent)
    2. {
    3. bool outOfRange = false;
    4. QPoint newPosition = pEvent->pos();
    5. if((pEvent->pos().x() + this->width()) >= (parentClass->pos().x() + parentClass->width()))
    6. {
    7. newPosition.setX(pEvent->pos().x()-10);
    8. outOfRange = true;
    9. }
    10. if((pEvent->pos().y() + this->height()) >= (parentClass->pos().y() + parentClass->height()+30))
    11. {
    12. newPosition.setY(parentClass->pos().y() + parentClass->height() - (this->height()+10));
    13. outOfRange = true;
    14. }
    15. if(pEvent->pos().x() <= parentClass->pos().x())
    16. {
    17. newPosition.setX(pEvent->pos().x()+10);
    18. outOfRange = true;
    19. }
    20. if(pEvent->pos().y() <= (parentClass->pos().y()))
    21. {
    22. newPosition.setY(parentClass->pos().y());
    23. outOfRange = true;
    24. }
    25. if(outOfRange)
    26. {
    27. this->hide();
    28. this->move(newPosition);
    29. this->show();
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    This works, I am only having small problem with the bottom of the dialog. It doesn't seem to act like the rest of the sides for some reason.
    Last edited by jacek; 17th July 2007 at 20:52. Reason: missing [code] tags

  12. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limit Window Movement

    Well, who said it is going to be easy?
    As for the bottom part of the dialog, use QWidget::frameGeometry().width()&height().
    The ones you use do not include any frame and the title bar height.

    Regards

  13. The following user says thank you to marcel for this useful post:

    abbapatris (17th July 2007)

  14. #11
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Re: Limit Window Movement

    That explains it...thanks so much. That will fix a few of my issues

  15. #12
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Re: Limit Window Movement

    well forget what I said...what I did doesn't seem to work with my larger program...it only seems to work with my small program and I am not sure why.

  16. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limit Window Movement

    What do you mean by larger program?
    A dialog is just a dialog and a move event is just what it is.

    Make sure you integrated the code correctly in the other application.

    Regards

  17. #14
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Re: Limit Window Movement

    I find that if I use it in just a mainwindow it will work fine. But as soon as that main window gets a frame in it the dialog will fail to stop at the border. I have been trying to figure it out and I just am confused right now.

  18. #15
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limit Window Movement

    Well, could you post the code in question?

  19. #16
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Re: Limit Window Movement

    Before I try to post all the code, (cause I am not sure what code is causing the issue) is there a way just to stop the mouse from having focus on the dialog box?

  20. #17
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limit Window Movement

    You could try disabling it by calling setFocusPolicy(Qt::NoFocus) in the dialog constructor, but I don't guarantee it will do too much.

  21. #18
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Re: Limit Window Movement

    alright...I am running out of ideas. I didn't think this would be this difficult. Other then the code I already posted here is the other code.

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    lockedarea w;
    w.show();
    a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    return a.exec();
    }
    lockedarea::lockedarea(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    ui.setupUi(this);
    tmp *Tmp = new tmp(this);
    Tmp->show();
    }

    lockedarea::~lockedarea()
    {

    tmp::tmp(QWidget *parent)
    : LimitMovement(parent)
    {
    ui.setupUi(this);
    //this->move(parent->pos());
    mmm *ccc = new mmm(parent);
    ccc->show();

    }

    tmp::~tmp()
    {

    }
    mmm::mmm(QWidget *parent)
    : LimitMovement(parent)
    {
    ui.setupUi(this);
    }

    mmm::~mmm()
    {

    }

    LimitMovement::LimitMovement(QWidget *parent)
    : QWidget()//parent)
    {
    parentSize = parent->frameGeometry().size();
    parentPosition = parent->pos();
    this->move(parentPosition.x()+(parentSize.width()/2),
    parentPosition.y()+(parentSize.height()/2));
    }





    ---That's all my code...there isn't much to the dialogs

  22. #19
    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: Limit Window Movement

    How about a completely different approach? Because window frames including titlebar are not handled by Qt but the underlying system, how about implementing a custom titlebar to get full control over window movement?
    J-P Nurmi

  23. The following user says thank you to jpn for this useful post:

    abbapatris (19th July 2007)

  24. #20
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Re: Limit Window Movement

    Thats a very good idea. Is there a tutorial or some documentation that could help with this that you know of?

Similar Threads

  1. Replies: 4
    Last Post: 4th June 2007, 12:07
  2. Regarding drawing on Transparent Window
    By Shalabh in forum Qt Programming
    Replies: 3
    Last Post: 31st May 2007, 10:32
  3. Independant window in an application
    By titoo in forum Qt Programming
    Replies: 4
    Last Post: 28th February 2007, 11:07
  4. move parent window to the front.
    By hvengel in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2007, 08:41
  5. cannot make a main window modal
    By Dark_Tower in forum Qt Programming
    Replies: 12
    Last Post: 23rd March 2006, 10:21

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
  •  
Qt is a trademark of The Qt Company.