我目前正在使用 QT 5.14.2 和 QT Creator 4.11.1。
在我當前的專案中,我在 ui 中放置了一個 QPushButton,然后在 cpp 檔案中我將視窗大小設定為 maxsize,以便它將使用整個螢屏并提供可用的關閉/調整/最小化按鈕。在程式中,我只是創建了一個函式,當我單擊按鈕并將其放在按鈕正下方時,該函式將創建一個彈出選單。雖然不調整視窗大小時很好,但是在調整視窗大小后,按鈕的位置屬性不會改變,單擊它后會創建彈出選單,就好像視窗仍然是最大化的一樣(所以在實際程式之外窗戶)。這應該是某個QT版本下的錯誤嗎?(我還嘗試查看按鈕提供給我的整個功能串列,但無濟于事,他們都沒有這樣做(我的目標是做->)按鈕的更新'
這也是我的片段:
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*)));
}
uj5u.com熱心網友回復:
因此,要獲得您的 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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/482831.html
