天天看點

QT5 擷取視窗、系統螢幕大小尺寸資訊,Qt 擷取控件位置坐标,螢幕坐标,相對父窗體坐标

一、QT5 擷取視窗大小尺寸資訊

QT視窗尺寸,視窗大小和大小改變引起的事件 QResizeEvent。

QT5 擷取視窗、系統螢幕大小尺寸資訊,Qt 擷取控件位置坐标,螢幕坐标,相對父窗體坐标
QT5 擷取視窗、系統螢幕大小尺寸資訊,Qt 擷取控件位置坐标,螢幕坐标,相對父窗體坐标
//視窗左上角的位置(含邊框)
    qDebug() << this->frameGeometry().x() << this->frameGeometry().y() << ;//1
    qDebug() << this->x()  << this->y();//2
    qDebug() << this->pos().x() << this->pos().y();//3
    //視窗的寬度和高度(含邊框)
    qDebug() << this->frameGeometry().width() << this->frameGeometry().height();
    //視窗左上角的位置(不含邊框)
    qDebug() << this->geometry().x() << this->geometry().y();
    //視窗的寬度和高度(不含邊框)
    qDebug() << this->geometry().width() << this->geometry().height();//1
    qDebug() << this->width() << this->height();//2
    qDebug() << this->rect().width() << this->rect().height();//3
    qDebug() << this->size().width() << this->size().height();//4

           

二、QT擷取系統螢幕大小

QDesktopWidget 提供了詳細的位置資訊,其能夠自動傳回視窗在使用者視窗的位置和應用程式視窗的位置,

QDesktopWidget* pDesktopWidget = QApplication::desktop();
    //擷取可用桌面大小
    QRect deskRect = QApplication::desktop()->availableGeometry();
    //擷取主螢幕分辨率
    QRect screenRect = QApplication::desktop()->screenGeometry();
    //擷取螢幕數量
    int nScreenCount = QApplication::desktop()->screenCount();
           

Qt5開始,QDesktopWidget官方不建議使用,改為QScreen。

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

三、設定窗體大小

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

void setGeometry(const QRect &)

void resize(int w, int h)

void resize(const QSize &)
           

四、Qt 擷取控件位置坐标,螢幕坐标,相對父窗體坐标

這個隻是傳回相對這個widget(重載了QMouseEvent的widget)的位置。

視窗坐标,這個是傳回滑鼠的全局坐标

傳回相對顯示器的全局坐标

将視窗坐标轉換成顯示器坐标

将顯示器坐标轉換成視窗坐标

将視窗坐标獲得的pos轉換成父類widget的坐标

将父類視窗坐标轉換成目前視窗坐标

将目前視窗坐标轉換成指定parent坐标。

QWidget::pos() : QPoint
           

這個屬性獲得的是目前目前控件在父視窗中的位置,

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.

擷取全局坐标

将滑鼠的坐标轉換成全局坐标。

将全局坐标(滑鼠目前坐标,QCursor::pos())直接轉換成目前視窗相對坐标