8000 Qt获取控件位置 · Issue #123 · holdyounger/ScopeBlog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Qt获取控件位置 #123
Open
Open
@holdyounger

Description

@holdyounger

Qt获取控件位置

> Qt 获取窗口、系统屏幕大小尺寸信息,Qt获取控件位置坐标、屏幕坐标,相对父窗体坐标

[toc]

一、获取窗口大小尺寸信息

Qt 窗口尺寸,窗口大小和大小改变引起的事件 QResizeEvent

<img src="Qt%E8%8E%B7%E5%8F%96%E6%8E%A7%E4%BB%B6%E4%BD%8D%E7%BD%AE/eccab31c4780c93f48c512dc844765a6.png" alt="在这里插入图片描述" style="zoom:80%;float:left;" />

<img src="Qt%E8%8E%B7%E5%8F%96%E6%8E%A7%E4%BB%B6%E4%BD%8D%E7%BD%AE/ea08f7c9bdd1808b4d8fbafcb8ad70b2.png" alt="在这里插入图片描述" style="float: left;" />

涉及坐标、长宽的接口

    //窗口左上角的位置(含边框)
    qDebug() &lt;&lt; this-&gt;frameGeometry().x() &lt;&lt; this-&gt;frameGeometry().y() &lt;&lt; ;//1
    qDebug() &lt;&lt; this-&gt;x()  &lt;&lt; this-&gt;y();//2
    qDebug() &lt;&lt; this-&gt;pos().x() &lt;&lt; this-&gt;pos().y();//3
    //窗口的宽度和高度(含边框)
    qDebug() &lt;&lt; this-&gt;frameGeometry().width() &lt;&lt; this-&gt;frameGeometry().height();
    //窗口左上角的位置(不含边框)
    qDebug() &lt;&lt; this-&gt;geometry().x() &lt;&lt; this-&gt;geometry().y();
    //窗口的宽度和高度(不含边框)
    qDebug() &lt;&lt; this-&gt;geometry().width() &lt;&lt; this-&gt;geometry().height();//1
    qDebug() &lt;&lt; this-&gt;width() &lt;&lt; this-&gt;height();//2
    qDebug() &lt;&lt; this-&gt;rect().width() &lt;&lt; this-&gt;rect().height();//3
    qDebug() &lt;&lt; this-&gt;size().width() &lt;&lt; this-&gt;size().height();//4

Qt获取系统屏幕大小

QDesktopWidget 提供了详细的位置信息,其能够自动返回窗口在用户窗口的位置和应用程序窗口的位置。

    QDesktopWidget* pDesktopWidget = QApplication::desktop();
    //获取可用桌面大小
    QRect deskRect = QApplication::desktop()-&gt;availableGeometry();
    //获取主屏幕分辨率
    QRect screenRect = QApplication::desktop()-&gt;screenGeometry();
    //获取屏幕数量
    int nScreenCount = QApplication::desktop()-&gt;screenCount();

Qt5开始, QDesktopWidget官方不建议使用,改为 QScreen

#include&lt;QScreen&gt;
#include&lt;QRect&gt;
 
QList&lt;QScreen *&gt; list_screen =  QGuiApplication::screens();  //多显示器
QRect rect = list_screen.at(0)-&gt;geometry();
desktop_width = rect.width();
desktop_height = rect.height();
qDebug() &lt;&lt; desktop_width &lt;&lt;desktop_height;

三、设置窗体大小

void setGeometry(int x, int y, int w, int h)

void setGeometry(const QRect &amp;)

void resize(int w, int h)

void resize(const QSize &amp;)

四、Qt获取控件位置坐标、屏幕坐标、相对父窗体坐标

  1. 这个只是返回相对这个 widget (重载了QMouseEventwidget)的位置。
QPoint QMouseEvent::pos()
  1. 窗口坐标,这个是返回鼠标的全局坐标
QPoint QMouseEvent::globalPos()
  1. 相对显示器的全局坐标
QPoint QCursor::pos() [static]
  1. 将窗口坐标转换成显示器坐标
QPoint QWidget::mapToGlobal(const QPoint &amp; pos)  const
  1. 将显示器坐标转换成窗口坐标
QPoint QWidget::mapFromGlobal(const QPoint &amp; pos) const
  1. 将窗口坐标获得的pos转成父类widget的坐标
QPoint QWidget::mapFromParent(const QPoint &amp; pos) const
  1. 将当前窗口坐标转换成指定parent坐标
QPoint QWidget::mapTo(const QWidget * parent, const QPoint &amp;pos) const
  1. 这个属性获得的是当前目前控件在父窗口中的位置
const QPointF &amp;QMouseEvent::screenPos() const
  1. Returns the position of the mouse cursor asa QPointF, relative to the screen that received the event.
    QPoint QMouseEvent::globalPos() 值相同,但是类型更高精度的 QPointF
    This function was introduced in Qt 5.0.
const QPointF &amp;QMouseEvent::screenPos() const
  1. 获取全局坐标
QCursor::pos() == QMouseEvent::globalPos() 
  1. 将鼠标的坐标转换成全局坐标
QMouseEvent::globalPos() == ui.posBtn-&gt;mapToGlobal(ui.posBtn-&gt;pos());
  1. 将鼠标坐标(鼠标当前坐标,QCursor::pos())直接转换成当前窗口相对坐标
ui.posBtn-&gt;mapFromGlobal(QCursor::pos());

blog link Qt获取控件位置

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0