QT开发中文网

Qt在布局后,怎么获取某个控件在整个ui中的坐标?c++

在C++中,可以通过QWidget的mapToGlobal方法将控件相对于父级窗口或屏幕的坐标转换为全局坐标。示例代码如下:

#include <QApplication>
#include <QMainWindow>
#include <QLabel>
#include <QVBoxLayout>
#include <QWidget>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QMainWindow window;
    QWidget *central_widget = new QWidget();
    window.setCentralWidget(central_widget);

    QLabel *label = new QLabel("Hello, world!");
    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(label);
    central_widget->setLayout(layout);

    // 获取label在整个ui中的坐标
    QPoint global_pos = label->mapToGlobal(label->pos());
    qDebug() << global_pos;

    window.show();
    return a.exec();
}

上述代码中,我们通过QWidget的pos方法获取了label在其父级窗口中的坐标,并使用mapToGlobal方法将其转换为全局坐标。最后打印出的global_pos即为label在整个ui中的坐标。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=495

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?