深度探索QT窗口系统---几何篇2

上一篇我们介绍了如何获取QT窗口的几何特性,这一篇介绍如何设置窗口的几何特性,窗口的平移,窗口的缩放,如何去掉窗口的边框,如何去掉窗口的标题,如何限定窗口的尺寸,下面逐一介绍:

1.窗口的平移,窗口平移可以用如下两种方式来实现,一是通过move的方法,二是通过setGeometry的方法

  1. void move(int x, int y)  
  2. void move(const QPoint &pos)  
  3. void setGeometry(int x, int y, int w, int h )  
  4. void setGeometry(const QRect &rect);  
void move(int x, int y)
void move(const QPoint &pos)
void setGeometry(int x, int y, int w, int h )
void setGeometry(const QRect &rect);

 对于采用move的方法来移动窗口,只能改变窗口的位置,不能改变窗口的大小,窗口的位置包括窗口边框,若窗口无父窗口,则位置坐标(x,y),相对于电脑屏幕来说的,若窗口有父窗口,则位置坐标(x,y)是相对于父窗口来说的;对于采用setGeometry的方法来移动窗口,可以改变窗口的位置的同时,改变窗口的尺寸,窗口的位置不包括窗口边框,若窗口无父窗口,则位置坐标(x,y)相对于电脑屏幕来说的,若窗口有父窗口,则位置坐标(x,y)相对于父窗口来说的;我们来看实例:

(1)采用move的方法,窗口无父窗口

  1. #include <QtGui/QApplication>  
  2. #include <qwidget.h>  
  3. #include <qdebug.h>  
  4. void print(QWidget *pWidget)  
  5. {  
  6.     qDebug() << “pos() = ” << pWidget->pos();  
  7. }  
  8. int main(int argc, char *argv[])  
  9. {  
  10.     QApplication a(argc, argv);  
  11.     QWidget widget;  
  12.     widget.show();  
  13.     widget.resize(200, 300);  
  14.     widget.move(0, 0);  
  15.     print(&widget);  
  16.     widget.move(100, 100);  
  17.     print(&widget);  
  18.     return a.exec();  
  19. }  
#include <QtGui/QApplication>




#include <qwidget.h> #include <qdebug.h> void print(QWidget *pWidget) { qDebug() << "pos() = " << pWidget->pos(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget widget; widget.show(); widget.resize(200, 300); widget.move(0, 0); print(&widget); widget.move(100, 100); print(&widget); return a.exec(); }

widget先move(0,0)到屏幕的左上角,窗口系统的坐标轴x轴正向向右,y轴的正向向下,所以坐标原点在左上角;然后widget移动到move(100,100)的位置,下面是打印出的位置:

pos() = QPoint(0,0)

pos() = QPoint(100,100)

(2)采用setGeometry 的方法,窗口无父窗口

  1. #include <QtGui/QApplication>  
  2. #include <qwidget.h>  
  3. #include <qdebug.h>  
  4. void print(QWidget *pWidget)  
  5. {  
  6.     qDebug() << “pos() = ” << pWidget->pos();  
  7. }  
  8. int main(int argc, char *argv[])  
  9. {  
  10.     QApplication a(argc, argv);  
  11.     QWidget widget;  
  12.     widget.show();  
  13.     widget.resize(200, 300);  
  14.     widget.setGeometry(0, 0, 200, 300);  
  15.     print(&widget);  
  16.     widget.setGeometry(100, 100, 200, 300);  
  17.     print(&widget);  
  18.     return a.exec();  
  19. }  
#include <QtGui/QApplication>




#include <qwidget.h> #include <qdebug.h> void print(QWidget *pWidget) { qDebug() << "pos() = " << pWidget->pos(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget widget; widget.show(); widget.resize(200, 300); widget.setGeometry(0, 0, 200, 300); print(&widget); widget.setGeometry(100, 100, 200, 300); print(&widget); return a.exec(); }

我们先来看打印出的结果:

pos() = QPoint(-4,-30)

pos() = QPoint(96,70)

为什么第一次不是移动到(0, 0),第二次不是移动到(100,100)呢?因为通过setGeometry设置的移动位置不包括边框,而我们打印出来的是边框的位置,参照几何篇一,很快你就知道,4是边框宽度,30是窗口标题高度+边框宽度。

(3)采用move的方法,窗口有父窗口

  1. #include <QtGui/QApplication>  
  2. #include <qwidget.h>  
  3. #include <qdebug.h>  
  4. void print(QWidget *pWidget)  
  5. {  
  6.     qDebug() << “pos() = ” << pWidget->pos();  
  7. }  
  8. int main(int argc, char *argv[])  
  9. {  
  10.     QApplication a(argc, argv);  
  11.     QWidget parentWidget;  
  12.     QPushButton *ppbTest = new QPushButton(“test button”, &parentWidget);  
  13.     parentWidget.show();  
  14.     parentWidget.resize(200, 300);  
  15.     ppbTest->move(100, 100);  
  16.     print(ppbTest);  
  17.     ppbTest->move(0, 0);  
  18.     print(ppbTest);  
  19.     return a.exec();  
  20. }  
#include <QtGui/QApplication>




#include <qwidget.h> #include <qdebug.h> void print(QWidget *pWidget) { qDebug() << "pos() = " << pWidget->pos(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget parentWidget; QPushButton *ppbTest = new QPushButton("test button", &parentWidget); parentWidget.show(); parentWidget.resize(200, 300); ppbTest->move(100, 100); print(ppbTest); ppbTest->move(0, 0); print(ppbTest); return a.exec(); }

ppbTest是parentWidget的子窗口,这次移动ppbTest,首先移动位置(100,100)处,然后移动到(0,0)处,先来看看输出结果:

pos() = QPoint(100,100)

pos() = QPoint(0,0)

通过上面的输出结果,我们不难得出结论,ppbTest子窗口move位置是相对父窗口parentWidget来说的,且是相对父窗口的内容矩形来讲的。

(4)采用setGeometry的方法,窗口有父窗口

  1. #include <QtGui/QApplication>  
  2. #include <qwidget.h>  
  3. #include <qdebug.h>  
  4. void print(QWidget *pWidget)  
  5. {  
  6.     qDebug() << “pos() = ” << pWidget->pos();  
  7. }  
  8. int main(int argc, char *argv[])  
  9. {  
  10.     QApplication a(argc, argv);  
  11.     QWidget parentWidget;  
  12.     QPushButton *ppbTest = new QPushButton(“test button”, &parentWidget);  
  13.     parentWidget.show();  
  14.     parentWidget.resize(200, 300);  
  15.   
  16.     ppbTest->setGeometry(100, 100, ppbTest->width(), ppbTest->height());  
  17.     print(ppbTest);  
  18.     ppbTest->setGeometry(0, 0, ppbTest->width(), ppbTest->height());  
  19.     print(ppbTest);  
  20.     return a.exec();  
  21. }  
#include <QtGui/QApplication>




#include <qwidget.h> #include <qdebug.h> void print(QWidget *pWidget) { qDebug() << "pos() = " << pWidget->pos(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget parentWidget; QPushButton *ppbTest = new QPushButton("test button", &parentWidget); parentWidget.show(); parentWidget.resize(200, 300); ppbTest->setGeometry(100, 100, ppbTest->width(), ppbTest->height()); print(ppbTest); ppbTest->setGeometry(0, 0, ppbTest->width(), ppbTest->height()); print(ppbTest); return a.exec(); }

输出结果同(3),为什么呢?不是说setGeometry的移动起始位置不包括边框吗?怎么输出结果和(3)相同呢?这是在这里ppbTest没有边框的原因,好了窗口的移动就介绍到这里了,下节我们介绍窗口的缩放。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值