PDA

View Full Version : Limit Window Movement



abbapatris
17th July 2007, 11:43
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.

marcel
17th July 2007, 11:54
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

abbapatris
17th July 2007, 12:11
Thank you I will try that

abbapatris
17th July 2007, 12:22
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?

jpn
17th July 2007, 12:29
From QWidget::moveEvent() docs:

When the widget receives this event, it is already at the new position.

abbapatris
17th July 2007, 12:38
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.

guilugi
17th July 2007, 14:02
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...:)

abbapatris
17th July 2007, 14:19
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.

abbapatris
17th July 2007, 19:27
I have found a way to make it work using QMoveEvent. Its not pretty but it works.



void LimitMovement::moveEvent(QMoveEvent * pEvent)
{
bool outOfRange = false;
QPoint newPosition = pEvent->pos();
if((pEvent->pos().x() + this->width()) >= (parentClass->pos().x() + parentClass->width()))
{
newPosition.setX(pEvent->pos().x()-10);
outOfRange = true;
}
if((pEvent->pos().y() + this->height()) >= (parentClass->pos().y() + parentClass->height()+30))
{
newPosition.setY(parentClass->pos().y() + parentClass->height() - (this->height()+10));
outOfRange = true;
}
if(pEvent->pos().x() <= parentClass->pos().x())
{
newPosition.setX(pEvent->pos().x()+10);
outOfRange = true;
}
if(pEvent->pos().y() <= (parentClass->pos().y()))
{
newPosition.setY(parentClass->pos().y());
outOfRange = true;
}
if(outOfRange)
{
this->hide();
this->move(newPosition);
this->show();
}
}

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.

marcel
17th July 2007, 19:33
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

abbapatris
17th July 2007, 19:59
That explains it...thanks so much. That will fix a few of my issues

abbapatris
18th July 2007, 18:18
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.

marcel
18th July 2007, 18:45
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

abbapatris
18th July 2007, 19:01
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.

marcel
18th July 2007, 19:04
Well, could you post the code in question?

abbapatris
18th July 2007, 19:33
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?

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

abbapatris
19th July 2007, 13:36
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

jpn
19th July 2007, 13:48
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?

abbapatris
19th July 2007, 13:50
Thats a very good idea. Is there a tutorial or some documentation that could help with this that you know of?

jpn
19th July 2007, 13:54
Is there a tutorial or some documentation that could help with this that you know of?
Try following Brandybuck's suggestion:
http://www.qtcentre.org/forum/f-qt-programming-2/t-skinned-dialogs-3975.html#10