QT獲取窗口大小和位置等信息


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

 

 

來源:http://blog.csdn.net/dbzhang800/article/details/6741344?reload
操作:


frameGeometry()

幾何尺寸(位置+大小)

對於窗口,包含窗口裝飾器

x()y()pos()

只包含位置信息(左上角坐標)

move()

只移動位置

geometry()

幾何尺寸(位置+大小)

不包含窗口裝飾器

width()height()rect()size()

只包含大小信息

setGeometry()

改變 位置+大小

resize()

只改變大小



一直在考慮怎么使中心窗口在主窗口全屏之后,中心窗口也按比例放大。
其中包括,中心窗口的大小適應,中心窗口
1.創建你的UI,其中使用弄好你的頂級布局,選中widget窗口 - 點擊右鍵 - 布局 - 柵格布局。

2.構造函數中

 

centralWidget = new QWidget;
QWidget * a = new QWidget(centralWidget);
ui1->setupUI(a);
a->hide();
QWidget * b = new QWidget(centralWidget);
ui2->setupUI(b);
b->hide();
setCentralWidget(centralWidget)
//這樣的好處在於,你可以弄很多ui,想使用哪個UI直接uin->setupUI(widgetn),再widgetn->show()就可以了。

 

3.但是如果你想拖動主窗口的時候,能夠讓窗口中的組件隨着窗口也能縮放的話,需要重寫resizeEvent,原因在於,在構造之后,子窗口的大小就是固定的。

void resizeEvent(QresizeEvent* size)
{ centralWidget
->resize(frameGeometry().size()); //是采用size()還是frameGeometry.size()根據自己的需求。 }

有什么問題,請留言說明,大家一起交流並且解決。

附加設備相關的屏幕尺寸信息,如果你重寫resizeEvent的話,我認為這是不必要的。
QDesktopWidget* desktopWidget = QApplication::desktop();
//得到客戶區矩形
QRect clientRect = desktopWidget->availableGeometry();
//得到應用程序矩形
QRect applicationRect = desktopWidget->screenGeometry();

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM