QPushButton Position 在 window 调整大小后不更新其位置
QPushButton Postion does not update its positon after window resize
我目前使用的是 QT 5.14.2 和 QT Creator 4.11.1。
在我当前的项目中,我在 ui 中放置了一个 QPushButton,然后在 cpp 文件中,我将 window 大小设置为最大大小,这样它将使用整个屏幕 close/rezise/minimize 按钮可用。在程序中,我只是创建了一个函数,当我单击按钮并将其放在按钮正下方时,该函数将创建一个弹出菜单。虽然当你不调整 window 时很好,但在调整它之后,按钮的位置属性不会改变,点击它后将创建弹出菜单,就好像 window 仍然一样最大化(所以在实际程序的 window 之外)。这应该是某个 QT 版本下的错误吗? (我还尝试查看按钮提供给我的整个功能列表,但无济于事,其中 none 执行(我的目标是执行 ->)基于 window 在屏幕上的大小和位置,因此弹出菜单会出现在按钮的 y 位置的正下方)
这也是我的片段:
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*)));
}
因此,要获得您的 QMainWindow 位置,应使用以下代码:
QPoint point(ui->centralwidget->mapToGlobal(ui->centralwidget->pos())); //Initialising QPoint x and y with mapToGlobal which will display 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.
我目前使用的是 QT 5.14.2 和 QT Creator 4.11.1。
在我当前的项目中,我在 ui 中放置了一个 QPushButton,然后在 cpp 文件中,我将 window 大小设置为最大大小,这样它将使用整个屏幕 close/rezise/minimize 按钮可用。在程序中,我只是创建了一个函数,当我单击按钮并将其放在按钮正下方时,该函数将创建一个弹出菜单。虽然当你不调整 window 时很好,但在调整它之后,按钮的位置属性不会改变,点击它后将创建弹出菜单,就好像 window 仍然一样最大化(所以在实际程序的 window 之外)。这应该是某个 QT 版本下的错误吗? (我还尝试查看按钮提供给我的整个功能列表,但无济于事,其中 none 执行(我的目标是执行 ->)基于 window 在屏幕上的大小和位置,因此弹出菜单会出现在按钮的 y 位置的正下方)
这也是我的片段:
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*)));
}
因此,要获得您的 QMainWindow 位置,应使用以下代码:
QPoint point(ui->centralwidget->mapToGlobal(ui->centralwidget->pos())); //Initialising QPoint x and y with mapToGlobal which will display 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.