窗口相关位置函数
知识点
geometry().x() 多屏幕实现方法
#include <QWidget>
#include <QPoint>
#include <QScreen>
// 假设这个函数是某个QWidget的子类中的成员函数
void MyWidget::showGlobalXPosition() {
// 获取窗口部件的左上角位置(相对于其父窗口)
QPoint localPos = this->pos();
// 将局部位置转换为全局屏幕位置
QPoint globalPos = this->mapToGlobal(localPos);
// 现在globalPos.x()就是窗口部件的全局x坐标
int globalX = globalPos.x();
// 输出或使用globalX
qDebug() << "Global X position:" << globalX;
// 如果你还想知道这个坐标属于哪个屏幕,可以这样做:
QScreen *screen = QGuiApplication::screens().at(0); // 默认取第一个屏幕,但通常你会想找到实际的屏幕
for (QScreen *scr : QGuiApplication::screens()) {
if (scr->geometry().contains(globalPos)) {
screen = scr;
break;
}
}
// 现在screen变量指向了包含窗口部件的屏幕
qDebug() << "The widget is on screen:" << screen->name(); // 假设屏幕有名字
}
当类名和geometry函数重复时,调用会导致进入geometry()构造函数
geometry.h
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include <QWidget>
#include <QDialog>
#include <QLabel>
#include <QGridLayout>
#include <QMoveEvent>
#include <QResizeEvent>
class geometry : public QDialog
{
Q_OBJECT
public:
geometry(QWidget *parent = nullptr);
~geometry();
void UpdateLabel();

最低0.47元/天 解锁文章
1067

被折叠的 条评论
为什么被折叠?



