窗口相关位置函数
- 知识点
- geometry.h
- geometry.cpp
- main.cpp
- 运行图
知识点
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.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();
private:
QLabel* xLabel;
QLabel* xValueLabel;
QLabel* yLabel;
QLabel* yValueLabel;
QLabel* FrmLabel;
QLabel* FrmValueLabel;
QLabel* PosLabel;
QLabel* PosValueLabel;
QLabel* GeoLabel;
QLabel* GeoValueLabel;
QLabel* WidthLabel;
QLabel* WidthValueLabel;
QLabel* HeightValueLabel;
QLabel* HeightLabel;
QLabel* RectLabel;
QLabel* RectValueLabel;
QLabel* SizeLabel;
QLabel* SizeValueLabel;
QGridLayout* MainLayout;
protected:
void moveEvent(QMoveEvent*);
void resizeEvent(QResizeEvent*);
};
#endif // GEOMETRY_H
geometry.cpp
#include "geometry.h"
geometry::geometry(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("Geometry"));
xLabel = new QLabel(tr("x():"));
xValueLabel = new QLabel;
yLabel = new QLabel(tr("y():"));
yValueLabel = new QLabel;
FrmLabel = new QLabel(tr("Frame:"));
FrmValueLabel = new QLabel;
PosLabel =new QLabel(tr("pos():"));
PosValueLabel =new QLabel;
WidthLabel = new QLabel("Width():");
WidthValueLabel = new QLabel;
HeightLabel = new QLabel(tr("Height():"));
HeightValueLabel = new QLabel;
RectLabel = new QLabel(tr("rect():"));
RectValueLabel = new QLabel;
SizeLabel = new QLabel(tr("Size():"));
SizeValueLabel =new QLabel;
GeoLabel = new QLabel(tr("Geometry():"));
GeoValueLabel = new QLabel;
MainLayout = new QGridLayout(this);
MainLayout->addWidget(xLabel,0,0);
MainLayout->addWidget(xValueLabel,0,1);
MainLayout->addWidget(yLabel,1,0);
MainLayout->addWidget(yValueLabel,1,1);
MainLayout->addWidget(FrmLabel,2,0);
MainLayout->addWidget(FrmValueLabel,2,1);
MainLayout->addWidget(PosLabel,3,0);
MainLayout->addWidget(PosValueLabel,3,1);
MainLayout->addWidget(WidthLabel,4,0);
MainLayout->addWidget(WidthValueLabel,4,1);
MainLayout->addWidget(HeightLabel,5,0);
MainLayout->addWidget(HeightValueLabel,5,1);
MainLayout->addWidget(RectLabel,6,0);
MainLayout->addWidget(RectValueLabel,6,1);
MainLayout->addWidget(SizeLabel,7,0);
MainLayout->addWidget(SizeValueLabel,7,1);
MainLayout->addWidget(GeoLabel,8,0);
MainLayout->addWidget(GeoValueLabel,8,1);
UpdateLabel();
}
geometry::~geometry() {}
void geometry::UpdateLabel()
{
QString xStr,yStr;
xValueLabel->setText(xStr.setNum(x()));
yValueLabel->setText(yStr.setNum(y()));
QString FrameStr;
QString TmpStr1,TmpStr2,TmpStr3,TmpStr4;
FrameStr = TmpStr1.setNum(frameGeometry().x()) + ","+TmpStr2.setNum(frameGeometry().y())+","+ TmpStr3.setNum(frameGeometry().width()) + ","+TmpStr4.setNum(frameGeometry().height());
FrmValueLabel->setText(FrameStr);
QString PositionStr;
QString TmpStr11,TmpStr12;
PositionStr = TmpStr11.setNum(pos().x()) + "," +TmpStr12.setNum(pos().y());
PosValueLabel->setText(PositionStr);
QString wStr,hStr;
WidthValueLabel->setText(wStr.setNum(width()));
HeightValueLabel->setText(hStr.setNum(height()));
QString RectStr;
QString TmpStr31,TmpStr32,TmpStr33,TmpStr34;
RectStr = TmpStr31.setNum(rect().x()) + "," +TmpStr32.setNum(rect().y())+ "," + TmpStr33.setNum(rect().width()) + ","+TmpStr34.setNum(rect().height());
RectValueLabel->setText(RectStr);
QString SizeStr,TmpStr41,TmpStr42;
SizeStr = TmpStr41.setNum(size().width()) + "," +TmpStr42.setNum(rect().height());
SizeValueLabel->setText(SizeStr);
/*
*/
}
void geometry::moveEvent(QMoveEvent *)
{
UpdateLabel();
}
void geometry::resizeEvent(QResizeEvent *)
{
UpdateLabel();
}
main.cpp
#include "geometry.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
geometry w;
w.show();
return a.exec();
}