【发布时间】:2016-11-03 16:44:11
【问题描述】:
环境:
- XP 及更高版本,OS X 10.6.8 及更高版本
- Qt 4.7.1 64 位,在 OS X 10.6.8 下构建
- Qt Creator 2.1.0
问题:
我有一个主窗口。在其中,一些容器水平下降,是一个gui元素cb_B_8,一个按钮。我可以通过这种方式获得该元素的位置和大小,并且它恰好相对于其标题栏下的主窗口区域(theParent 是主窗口实例):
QPoint buttpos = theParent->ui->cb_B_8->pos(); // = 473,576
QRect butrect = theParent->ui->cb_B_8->rect(); // = 0,0,32,26
这对我来说很有意义。
所以现在我这样打开一个对话框:
void MainWindow::mybandersnatch(int i)
{
setband *tt;
tt = new setband(this,i);
tt->show();
tt->raise();
}
}
对话框开始如下:
setband::setband(QWidget *parent, int bindex) :
QDialog(parent),
ui(new Ui::setband)
{
theParent = parent; // dialog local copy of parent
...
它在我的主窗口中心打开 (theParent)
我想重新定位此对话框,使对话框窗口的顶部直接位于按钮的下边缘下方。
我无法检索对话框相对于其父窗口的位置;而且我也无法相对于该窗口定位对话框。我尝试了一些看似可能但结果空洞的事情。
从概念上讲,考虑到我有按钮相对于窗口的位置,如果我有对话框相对于窗口的位置,并且同样可以相对于窗口进行设置,我想准确地设置它这样:
newDialogYpos = button.yPos + button.yHeight; // below button
buttonXcenter = button.xPos + (button.width / 2);
dialogHalfWidth = dialog.width / 2;
newDialogXpos = buttonXcenter - dialogHalfWidth;
对此的一个小改动是它必须在多显示器计算机上工作。似乎某些定位是相对于主显示的,这对我的需求来说是病态的——对话框最终必须相对于应用程序,而不是主显示。我已经尝试过这些事情来获得职位,并且干了起来:
t = this->rect(); // = 0,0 410x320 (dialog)
dp = this->pos(); // = 0,0 (dialog)
ap = QApplication::desktop()->screen()->pos(); // = 0,0 ?
mtp = this->window()->mapFromParent(tmw->pos()); // = 0,0 ?
// main display is 1680x1050, app is not on this display
// this display is 1280x1024 (I have 8 displays on this machine)
// this display is immediately to the right of the main display
// app window is fullscreen, so at top left of 1280x1024 display
// this result seems to incorporate other display positions + widths:
// ------------------------------------------------------------------
w = QApplication::desktop()->screen()->rect(); // = 0,0 3080x1050
欣赏任何见解。
【问题讨论】: