Qt geometry:why position always 0 ?

问题点:要获取 x/y坐标,必须满足前置条件:即必须首先设法满足 触发可以调用geometry的方法,才会产生出(或者说才会:生成?才会调用可被获取的pos位置坐标的方法函数)geometry坐标信息。

而窗户的宽度和高度参数,则可直接获取。

比如:

 在主窗口中 new一个子窗口,在主窗口的 cpp中无法获得子窗口的 x/y坐标。<< 错:可以

必须在所要获取坐标的当前(子)窗口中,才可获得当前(子)窗口的 x/y坐标 ??<< 错:可以

// 在子窗口的构造函数中,运行下面2句。

this->show();  // 先调用

this->hide();  // 紧接着就关闭它

刚过河,马上就拆桥。就可获得当前窗口的 x/y坐标了。相当于强制触发 运行可获得 geometry坐标信息的函数。

注1:此时,在子窗口的构造函数中是无法获取当前子窗口的 x/y坐标的。因为这个构造函数只运行一次。?

已经在主窗口中 new运行了,而在主窗口中又无法获得子窗口的 x/y坐标。

注2:似乎,只有在本窗口的构造函数中运行 show(),才能获得本窗口的 x/y坐标??

注3:此时,在子窗口的slot函数中(主窗口发出信号调用),可以获得 本子窗口的 x/y坐标。不再需要进行 show()。

qDebug() <<" 子窗口_slot() "<< this->frameGeometry();

输出: QRect(639,371 358x65)

上面是简单的实现方法,复杂的实现方法,参考:

qt子窗口跟随主窗口进行移动(放大和缩小也实现)

https://blog.csdn.net/weixin_43676892/article/details/112346843

Qt Forum

why position always 0 ?

https://forum.qt.io/topic/59010/why-position-always-0/9

There's no need for assumptions ;) It's all in the code and pretty simple.

When you place a widget without a layout in the designer the uic generates a code that places it where you designed it by calling setGeometry. You can confirm that by inspecting the generated ui_yourclass.h file. This code is then (usually) called at the top of the constructor with ui->setupUi. Since it calls setGeometry you can then read it back later in the constructor and it's what you set in the designer (obviously).

If you place a widget in a layout no such code is generated. The ui->setupUi call simply puts the widget in a layout and that's it.

It doesn't touch its geometry.

So in the constructor after the call to ui->setupUi the layout didn't trigger yet, as the widget is not yet shown.

So since nothing touched its geometry yet it will have the default (0,0) position and size.

When the widget is shown or resized the layout triggers the calculation of its geometry and sets it via setGeometry.

Really straightforward and no trickery whatsoever.

官方文档:

geometry : QRect

This property holds the geometry of the widget relative to its parent and excluding the window frame

When changing the geometry, the widget, if visible, receives a move event (moveEvent()) and/or a resize event (resizeEvent()) immediately. If the widget is not currently visible, it is guaranteed to receive appropriate events before it is shown.

执行窗口重绘函数,也是可以获得 x/y坐标的。

void paintEvent(QPaintEvent *event)

实际测试:

void MainWindow::on_pushButton_clicked()
{
    //fw->show(); // show()在前 第 1次打开,则 qDebug 第 1次运行就可以获得 x/y坐标。
    //fw->hide();
    qDebug() << "Form IN on_pushButton_clicked AAAAAA :: geometry()";
    qDebug() << fw ->geometry().x();
    qDebug() << fw ->geometry().y();
    qDebug() << fw ->geometry().width();
    qDebug() << fw ->geometry().height();
    fw->show(); // show()在后 第 1次打开,则 qDebug 第 2次运行就可以获得 x/y坐标。
}

void MainWindow::on_pushButton_2_clicked()
{
    //show()在 on_pushButton_clicked()中打开,则本槽函数第 1次运行,就可以获得 x/y坐标。
    // 直接运行本槽函数,show()总是没有运行,于是,得到的 x/y坐标总是 0,0
    // 当可以正常获取 x/y坐标之后,移动窗口,则可以获得窗口的 新坐标。
    // 结论:geometry()应该有一个总开关,一旦开启,就可以正常获取窗口移动中的 不同的 x/y坐标了。
    qDebug() << "Form IN on_pushButton_clicked BBBBBB :: geometry()";
    qDebug() << fw ->geometry().x();
    qDebug() << fw ->geometry().y();
    qDebug() << fw ->geometry().width();
    qDebug() << fw ->geometry().height();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值