I'm currently using QT 5.14.2 and QT Creator 4.11.1.
In my current project I placed a QPushButton in the ui, then in the cpp file I set the window size to maxsize so it would use the whole screen with the close/rezise/minimize buttons available. In the program I simply created a function that would create a popup menu when i would click on the button and place it just right under the button. Although it's fine when you don't resize window, but after rezising it, the position's attributes doesn't change for the button and after clicking on it will create the popup menu as if the window is still maximized (so outside of the actual program's window). Is this supposed to be a bug under a certain QT version ? (I also tried to check out the whole function list that a pushbutton offers to me but to no avail, none of them do (my goal here is to do ->) an update on the button's coords based on the window's size and location on the screen so the popup menu would appear right beneath the button's y pos)
Also here is my snippet :
void DropMenu::on_dropMenu_clicked()
{
QMenu * menu = new QMenu(this);
menu->addAction(new QAction("Help"));
int ypos = (ui->dropMenu->pos().y() ui->dropMenu->height() * 2); //using * 2 because the program window's y and x pos is 0 which is the window's border height (where app icon and app name is)
qDebug() << ui->dropMenu->pos().x();
qDebug() << ui->dropMenu->pos().y() << " " << (ui->dropMenu->pos().y() ui->dropMenu->height()) << " " << ypos;
QPoint point;
point.setX(ui->dropMenu->pos().x());
point.setY(ypos); //to render it just under the button's y pos
menu->popup(point);
connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(DisplayHelp(QAction*)));
}
CodePudding user response:
So to get your QMainWindow positon the following code should be used :
QPoint point(ui->centralwidget->mapToGlobal(ui->centralwidget->pos())); //Initialising QPoint x and y with mapToGlobal which display's for us the x y based on where our window is on the screen.
point.setY(point.y() ui->dropButton->height()); //Adding the button's y coord in order to make the popup window created just below the button each time.
//Note that my button is in the top-left corner that's why I don't add the x coord of my button based on where it is in the window
menu->popup(point); //Displaying the popup menu.
connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(DisplayHelp(QAction*))); //Hooking up to a signal to do things for us.