PDA

View Full Version : PopUp Widget coordinates are not set in respect of MainWindow



Mitya_
6th December 2018, 16:03
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.


#ifndef POPUPMENU_H
#define POPUPMENU_H

#include <QWidget>
#include <QPalette>


namespace Ui {
class PopUpMenu;
}

class PopUpMenu : public QWidget
{
Q_OBJECT

public:
explicit PopUpMenu(QWidget *parent);
~PopUpMenu();
private:
Ui::PopUpMenu *ui;
std::unique_ptr<QPalette> pal;
};

#endif // POPUPMENU_H

PopUpMenu::PopUpMenu(QWidget *parent) :
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>();
pal->setColor(QPalette::Window, QColor((Qt::black)));//testing
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

d_stranz
6th December 2018, 16:18
You need to use QWidget::mapToGlobal() to translate your widget's pos() into screen coordinates, and use those coordinates in the setGeometry() call.

Mitya_
6th December 2018, 19:13
d_stranz
Thank you VERY MUCH.