PopUp Widget coordinates are not set in respect of MainWindow
Hi Guys,
What I'm trying to implement is custom PopUp Menu for custom PushButton placed in TollBar.
PopUp menu is implemented via QWidget. For now it is simple black widget, in the end it should contain a number of PushButtons.
Code:
#ifndef POPUPMENU_H
#define POPUPMENU_H
#include <QWidget>
#include <QPalette>
namespace Ui {
class PopUpMenu;
}
{
Q_OBJECT
public:
explicit PopUpMenu
(QWidget *parent
);
~PopUpMenu();
private:
Ui::PopUpMenu *ui;
std::unique_ptr<QPalette> pal;
};
#endif // POPUPMENU_H
PopUpMenu
::PopUpMenu(QWidget *parent
) : ui(new Ui::PopUpMenu)
{
ui->setupUi(this);
this->setGeometry(parent->x(), parent->y() + parent.height(), parent->width() * 3, parent->height());
//this->setGeometry(parent->rect().bottomLeft().x(), parent->rect().bottomLeft().y(), parent->width() * 3, parent->height());
pal = std::make_unique<QPalette>();
this->setPalette(*pal);
this->setWindowFlags(Qt::Popup);
}
PopUpMenu::~PopUpMenu()
{
delete ui;
}
In my button I simply create an instance of PopUpMenu and via reimplemented QWidget::enterEvent() show it buy invoking menu.show().
the line this->setGeometry(parent->x(), parent->y() + parent.height(), parent->width() * 3, parent->height());
works fine for size, but places the PopUp widget in respect of global display coordinates, that is, in top left corner of display, but not an application window.
Tried mapFrom(To) functions - no luck.
Need advise how to set the coordinates related to QWidget not to display.
Thank you
Re: PopUp Widget coordinates are not set in respect of MainWindow
You need to use QWidget::mapToGlobal() to translate your widget's pos() into screen coordinates, and use those coordinates in the setGeometry() call.
Re: PopUp Widget coordinates are not set in respect of MainWindow
d_stranz
Thank you VERY MUCH.