简体   繁体   English

qt 4.7-在父窗口中特定位置的位置对话框

[英]qt 4.7 - position dialog at specific position in parent window

Environment: 环境:

  • XP and later, OS X 10.6.8 and later XP及更高版本,OS X 10.6.8及更高版本
  • Qt 4.7.1 64 bit, building under OS X 10.6.8 Qt 4.7.1 64位,在OS X 10.6.8下构建
  • Qt Creator 2.1.0 Qt Creator 2.1.0

Issue: 问题:

I have a mainwindow. 我有一个主窗口。 In it, some container levels down, is a gui element cb_B_8 , a button. 在其中,一些容器级别降低,是一个gui元素cb_B_8 ,一个按钮。 I can get that element's position and size this way, and it comes to me precisely relative to the mainwindow area under its titlebar ( theParent is the mainwindow instance): 我可以通过这种方式获得该元素的位置和大小,并且它相对于其标题栏下的mainwindow区域( theParent是mainwindow实例)精确地指向我:

QPoint buttpos = theParent->ui->cb_B_8->pos(); // = 473,576
QRect  butrect = theParent->ui->cb_B_8->rect(); // = 0,0,32,26

That makes perfect sense to me. 这对我来说很有意义。

So now I open a dialog this way: 现在,我以这种方式打开一个对话框:

void MainWindow::mybandersnatch(int i)
{
setband *tt;
    tt = new setband(this,i);
    tt->show();
    tt->raise();
}

} }

The dialog starts like this: 对话框开始如下:

setband::setband(QWidget *parent, int bindex) :
QDialog(parent),
ui(new Ui::setband)
{
    theParent = parent; // dialog local copy of parent
    ...

It opens in the center of my mainwindow ( theParent ) 它在我的主窗口( theParent )的中心打开

I want to re-position this dialog so that the top of the dialog window is directly under the lower edge of the button. 我想重新定位此对话框,以便对话框窗口的顶部直接位于按钮的下边缘下方。

I have been unable to retrieve the dialog's position relative to the window that is its parent; 我一直无法获取对话框相对于其父窗口的位置。 and I have also been unable to position the dialog relative to that window. 而且我也无法将对话框相对于该窗口定位。 I have tried a few things that seemed likely but have come up empty. 我尝试了一些似乎可能但无法解决的事情。

Conceptually, considering I have the button's position relative to the window, if I had the dialog's position relative to the window, and could likewise set it relative to the window, I want to set it exactly this way: 从概念上讲,考虑到我具有按钮相对于窗口的位置, 如果我具有对话框相对于窗口的位置,并且同样可以相对于窗口进行设置,则我想这样设置:

newDialogYpos = button.yPos + button.yHeight; // below button

buttonXcenter = button.xPos + (button.width / 2);
dialogHalfWidth = dialog.width / 2;
newDialogXpos = buttonXcenter - dialogHalfWidth;

One slight twist to this is it has to work on multi-display computers. 与此稍有不同的是,它必须在多显示器计算机上工作。 Seems like some of the positioning is relative to the main display, which is pathological for my needs - the dialog has to end up relative to the app, not the main display. 似乎某些位置是相对于主显示屏的,这对于我的需求而言是病理性的-对话框必须相对于应用程序而不是主显示屏结束。 I've tried these things to get positions, and come up dry: 我已经尝试过这些方法来获得位置,然后变得干燥:

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

Appreciate any insight. 赞赏任何见识。

The pos of a widget inside another is in the coordinate system of its parent. 另一个内部的小部件的pos位于其父级的坐标系中。 You need to use the parent's mapToGlobal() to get screen coordinates. 您需要使用父级的mapToGlobal()来获取屏幕坐标。

So, I got everything I was asking figured out, although apparently the Qt get titlebar height method is broken. 因此,我已经弄清了我要问的所有内容,尽管显然Qt获取标题栏高度方法已损坏。 The following works on all of the eight displays in my system: 以下内容适用于系统中的所有八个显示器:

void setband::positionDialog()
{
QPoint mw; // mainwindow position
QPoint bp; // button position
QRect r;   // button rect
QRect d;   // dialog rect
QPoint sp; // target position of all this
QPoint addend; // the delta required to move from dialog position
int tbh; // titlebar height (qt returns 0 for this, sigh)

    bp = theParent->ui->cb_B_11->pos();                 // = 473,576
    mw = theParent->pos();                              // = 1600,0
    d = this->rect();                                   // = 0,0 410x320 (dialog)
    r = theParent->ui->cb_B_11->rect();                 // = 0,0 32,26
    addend.setX(-((d.width() / 2) - (r.width() / 2)));  // position dialog centered on X re button
//  tbh = QApplication::style()->pixelMetric(QStyle::PM_TitleBarHeight); // get title bar height returns 0?!?
    tbh = 25;                                           // use bloody guesswork, then
    addend.setY(tbh + r.height());                      // need to move past button pos+height+titlebarHeight
    sp = mw + bp + addend;                              // combine all this
    this->move(sp);                                     // and move
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM