当前位置:网站首页>Qt QWidget嵌套相对位置获取 (qt 画线 嵌套)
Qt QWidget嵌套相对位置获取 (qt 画线 嵌套)
2022-06-23 03:56:00 【超自然祈祷】
使用函数
QPoint QWidget::mapToGlobal(const QPoint &pos) const
.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
void paintEvent(QPaintEvent *);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.setPen(Qt::blue);
p.drawLine(ui->pushButton->pos(),ui->pushButton_3->pos());
p.setPen(Qt::red);
p.drawLine(ui->pushButton->pos(),ui->pushButton_2->pos());
p.setPen(Qt::green);
p.drawLine(ui->pushButton->pos(), ui->pushButton_2->mapToGlobal( QPoint(0,0) - this->mapToGlobal( QPoint(0,0)) ));
}
.ui文件 和 运行结果 截图

按代码上的颜色分辨
pushButton_3、pushButton 是同级,都在界面上(蓝色线位置正确)
pushButton_2 是在一个widget里嵌套的 使用pushButton_2->pos()得到的是它在方框中的位置,所以连线位置出错(红色线)
而绿色线是使用mapToGlobal函数,窗口、pushButton_2都相对于显示器的位置,这样计算的位置就正确了。
边栏推荐
- 【微服务|Nacos】Nacos版本相关问题一览
- LeetCode-1757. 可回收且低脂的产品_SQL
- Zygote process
- Post processing of multisensor data fusion using Px4 ECL
- Hard core, become a high-quality tester: learn to communicate with products
- C'est dur de trouver un emploi? Ali des trois côtés, heureusement qu'il s'est bien préparé et qu'il a pris un produit.
- JVM原理之完整的一次GC流程
- 九九乘法表.bat
- PRCS-1016 : Failed to resolve Single Client Access Name
- 关于重放攻击和防御
猜你喜欢

How to conduct exploratory data analysis

【Laravel系列7.8】广播系统

Zygote process

Beyond chips and AI, why is hard technology capital becoming more and more "hard core"?

PRCS-1016 : Failed to resolve Single Client Access Name

Rtklib new version 2.4.3 B34 test comparison

Missing essential plugin

Baidu PaddlePaddle's "universal gravitation" first stop in 2022 landed in Suzhou, comprehensively launching the SME empowerment plan

Introduction to unityshader -- rendering optimization technology in unity (IV)

牛B程序员在“创建索引”时都会注意啥?
随机推荐
Mysql入门学习(一)之语法
1010 Radix
【微服务|Nacos】Nacos版本相关问题一览
关于DOS/DDOS攻击和防御
Web 应用程序安全测试指南
Jetpack compose menubar Desktop Menu from door opening to entry
Servlet self study notes
Sift特征点提取
pygame音乐相关的功能实现
Hard core, become a high-quality tester: learn to communicate with products
Introduction and use of precise ephemeris
弱者易怒如虎,强者平静如水,真正厉害的人早已戒掉了情绪
One or more lines of text overflow, ellipsis instead
大環境不好難找工作?三面阿裏,幸好做足了准備,已拿offer
618如何冲出重围?海尔智家:做好用户的数字化
Economic development is driven by new technology
Error related to synchronizing domestic AOSP code
C language stack implementation
Master shell, one article is enough!
HCIP 作业 BGP总结