Qt-interest Archive, January 2007
How to position a QMenu relative to the cursor
Message 1 in thread
How do I specify where a popup menu shall appear? E.g. over or under the
mouse cursor. I found out that I can use sizeHint() to get the menu's
geometry and adjust its position accordingly. Is this the correct way, or
is there a better way to do it?
Arne
--
[ signature omitted ]
Message 2 in thread
Arne Schmitz wrote:
> How do I specify where a popup menu shall appear? E.g. over or under the
> mouse cursor. I found out that I can use sizeHint() to get the menu's
> geometry and adjust its position accordingly. Is this the correct way, or
> is there a better way to do it?
Call the QPopupMenu/QMenu::popup() function?
--
[ signature omitted ]
Message 3 in thread
Andreas Aardal Hanssen wrote:
> Arne Schmitz wrote:
>> How do I specify where a popup menu shall appear? E.g. over or under the
>> mouse cursor. I found out that I can use sizeHint() to get the menu's
>> geometry and adjust its position accordingly. Is this the correct way, or
>> is there a better way to do it?
>
> Call the QPopupMenu/QMenu::popup() function?
Yes, yes, of course. But I meant the relative position to the mouse cursor.
Is there a way to specify that the menu shall popup NE, SE, NW or SW of the
cursor? I would call sizeHint() to do this myself, but I am not sure if
this is correct, or what happens if the menu does not fit on the screen
anymore.
Arne
--
[ signature omitted ]
Message 4 in thread
Arne Schmitz wrote:
>> Arne Schmitz wrote:
>>> How do I specify where a popup menu shall appear?
>> Call the QPopupMenu/QMenu::popup() function?
> Yes, yes, of course. But I meant the relative position to the mouse
> cursor. Is there a way to specify that the menu shall popup NE, SE, NW or
> SW of the cursor? I would call sizeHint() to do this myself, but I am not
> sure if this is correct, or what happens if the menu does not fit on the
> screen anymore.
If you want to specify what direction the popup pops up, there is no
explicit API for that; context menus tend to pop up down and to the right
in left-to-right locales, and right-to-left otherwise, with exceptions for
when the menu doesn't on the desktop.
You can call move() or setGeometry() to get full control over where it is
shown.
Andreas
--
[ signature omitted ]
Message 5 in thread
You can subclass and reimplement like the snippet below -- should be
similar for QPopupMenu.
(Please note this is dodgy old code that I don't use anymore -- it may
be buggy and there may be better ways to do what you want!)
Sam Dutton
...........................
void MyCombo::mousePressEvent(QMouseEvent *mouseEvent)
{
_mousePressGlobalY = mouseEvent->globalY();
QComboBox::mousePressEvent(mouseEvent);
}
void MyCombo::popup()
{
const QString current = currentText();
refresh();
setCurrentItem(current);
int mapComboGlobalX = mapToGlobal(QPoint(0, 0)).x();
// NB: popup() must become before resizing and moving (I think!)
QComboBox::popup();
if (_mousePressGlobalY + listBox()->height() >
qApp->desktop()->height())
{
int maximumHeight = QMIN((int)(qApp->desktop()->height()
- _mousePressGlobalY),
(int)(listBox()->itemHeight() *
listBox()->count() + 5));
listBox()->setFixedHeight(maximumHeight);
}
listBox()->move(QMAX(0, mapComboGlobalX), _mousePressGlobalY);
listBox()->move(mapComboGlobalX, QMIN(_mousePressGlobalY,
qApp->desktop()->height() - listBox()->height()));
}
SAM DUTTON
SENIOR SITE DEVELOPER
200 GRAY'S INN ROAD
LONDON
WC1X 8XZ
UNITED KINGDOM
T +44 (0)20 7430 4496
F
E SAM.DUTTON@xxxxxxxxx
WWW.ITN.CO.UK
-----Original Message-----
From: Andreas Aardal Hanssen [mailto:ahanssen@xxxxxxxxxxxxx]
Sent: Wednesday 03 January 2007 14:23
To: qt-interest@xxxxxxxxxxxxx
Subject: Re: How to position a QMenu relative to the cursor
Arne Schmitz wrote:
>> Arne Schmitz wrote:
>>> How do I specify where a popup menu shall appear?
>> Call the QPopupMenu/QMenu::popup() function?
> Yes, yes, of course. But I meant the relative position to the mouse
> cursor. Is there a way to specify that the menu shall popup NE, SE, NW
> or SW of the cursor? I would call sizeHint() to do this myself, but I
> am not sure if this is correct, or what happens if the menu does not
> fit on the screen anymore.
If you want to specify what direction the popup pops up, there is no
explicit API for that; context menus tend to pop up down and to the
right in left-to-right locales, and right-to-left otherwise, with
exceptions for when the menu doesn't on the desktop.
You can call move() or setGeometry() to get full control over where it
is shown.
Andreas
--
[ signature omitted ]