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

几何篇2介绍了窗口的平移方法,这一篇介绍窗口的缩放方法,通过窗口缩放,你可以做窗口慢慢展开和慢慢隐藏的效果,本篇我们先介绍窗口缩放方法,然后介绍一个窗口慢慢展开和慢慢隐藏的实例,窗口缩放的方法有2种,一种是采用resize的方式,另一种是采用setGeometry的方式,下面是QT提供的对应函数接口:

  1. void resize(const QSize &size)  
  2. void resize(int w, int h)  
  3. void setGeometry(int x, int y, int w, int h)  
  4. void setGeometry(const QRect &rect)  
void resize(const QSize &size)
void resize(int w, int h)
void setGeometry(int x, int y, int w, int h)
void setGeometry(const QRect &rect)

(1)resize方法,窗口没有父窗口

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




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

下面是输出结果:

geometry() = QRect(48,88 115x10)

geometry() = QRect(48,88 116x10)

geometry() = QRect(48,88 200x300)

为什么resize(10, 10)以后,geometry的宽度没有变为10呢,这是因为对有边框窗口,窗口上有图标,最小化,最大化,关闭按钮也需要空间,而这里的这下所需的最小空间为115,因此小于115,宽度都会是115。

(2)resize方法,无边框窗口

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




#include <qwidget.h> #include <qdebug.h> void print(QWidget *pWidget) { qDebug() << "geometry() = " << pWidget->geometry(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget widget(NULL, Qt::FramelessWindowHint); widget.show(); widget.resize(10, 10); print(&widget); widget.resize(116, 10); print(&widget); widget.resize(200, 300); print(&widget); return a.exec(); }

下面是输出结果:

geometry() = QRect(256,231 10x10)

geometry() = QRect(256,231 116x10)

geometry() = QRect(256,231 200x300)

 

(3)resize方法,窗口有父窗口

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




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

下面是输出结果:

geometry() = QRect(256,231 10x10)

geometry() = QRect(256,231 116x10)

geometry() = QRect(256,231 200x300)

(4)setGeometry方法,窗口无父窗口

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




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

下面是输出结果:

geometry() = QRect(92,146 115x10)

geometry() = QRect(92,146 116x10)

geometry() = QRect(92,146 200x300)

(5)setGeometry方法,无边框窗口

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




#include <qwidget.h> #include <qdebug.h> void print(QWidget *pWidget) { qDebug() << "geometry() = " << pWidget->geometry(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget widget(NULL, Qt::FramelessWindowHint); widget.show(); int x = widget.geometry().x(); int y = widget.geometry().y(); widget.setGeometry(x, y, 10, 10); print(&widget); widget.setGeometry(x, y, 116, 10); print(&widget); widget.setGeometry(x, y, 200, 300); print(&widget); return a.exec(); }

下面是输出结果:

geometry() = QRect(256,231 10x10)

geometry() = QRect(256,231 116x10)

geometry() = QRect(256,231 200x300)

(6)setGeometry方法,有父窗口

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




#include <qwidget.h> #include <qdebug.h> void print(QWidget *pWidget) { qDebug() << "geometry() = " << pWidget->geometry(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget widget; QPushButton *ppbTest = new QPushButton("test button", &widget); widget.show(); int x = ppbTest->geometry().x(); int y = ppbTest->geometry().y(); widget.resize(200, 300); ppbTest->setGeometry(x, y, 10, 10); print(ppbTest); ppbTest->setGeometry(x, y, 116, 10); print(ppbTest); ppbTest->setGeometry(x, y, 200, 300); print(ppbTest); return a.exec(); }

下面是输出结果:
geometry() = QRect(0,0 10x10)

geometry() = QRect(0,0 116x10)

geometry() = QRect(0,0 200x300)

前面从6个方面讲了通过resize和setGeometry对窗口缩放的方法,接下来给1个窗口展开和收缩的实例:

  1. #ifndef _TEST_H  
  2. #define _TEST_H  
  3. #include <qwidget.h>  
  4. #include <qpushbutton.h>  
  5. #include <QTimerEvent>  
  6. class CExpandCollapseWidget : public QWidget {  
  7.     Q_OBJECT  
  8. public:  
  9.     CExpandCollapseWidget(QWidget *parent = NULL) : QWidget(parent, Qt::FramelessWindowHint)  
  10.     {  
  11.         timerId = 0;  
  12.         bExpand = true;  
  13.         ppbExpandCollapse = new QPushButton(“Expand”, this);  
  14.         connect(ppbExpandCollapse, SIGNAL(clicked()), this, SLOT(rxClicked()));  
  15.     }  
  16. protected:  
  17.     void timerEvent(QTimerEvent *event)  
  18.     {  
  19.         if (event->timerId() == timerId)  
  20.         {  
  21.             int x = geometry().x();  
  22.             int y = geometry().y();  
  23.             int w = geometry().width();  
  24.             int h = geometry().height();  
  25.             setGeometry(x, y, w, h + increment);  
  26.             if (bExpand)  
  27.             {  
  28.                 if (h > 300)  
  29.                 {  
  30.                     killTimer(timerId);  
  31.                     timerId = 0;  
  32.                 }  
  33.             }  
  34.             else  
  35.             {  
  36.                 if (h < 40)  
  37.                 {  
  38.                     killTimer(timerId);  
  39.                     timerId = 0;  
  40.                 }  
  41.             }  
  42.         }  
  43.     }  
  44.   
  45. private slots:  
  46.     void rxClicked()  
  47.     {  
  48.         if (timerId == 0)  
  49.             timerId = startTimer(10);  
  50.         if (ppbExpandCollapse->text() == “Expand”)  
  51.         {  
  52.             bExpand = true;  
  53.             ppbExpandCollapse->setText(“Collapse”);  
  54.             increment = 10;  
  55.             //setSizeIncrement(0, 10);  
  56.         }  
  57.         else  
  58.         {  
  59.             bExpand = false;  
  60.             ppbExpandCollapse->setText(“Expand”);  
  61.             increment = -10;  
  62.         }  
  63.     }  
  64. private:  
  65.     QPushButton *ppbExpandCollapse;  
  66.     bool bExpand;  
  67.     int timerId;  
  68.     int increment;  
  69. };  
  70. #endif  
#ifndef _TEST_H




#define _TEST_H #include <qwidget.h> #include <qpushbutton.h> #include <QTimerEvent> class CExpandCollapseWidget : public QWidget { Q_OBJECT public: CExpandCollapseWidget(QWidget *parent = NULL) : QWidget(parent, Qt::FramelessWindowHint) { timerId = 0; bExpand = true; ppbExpandCollapse = new QPushButton("Expand", this); connect(ppbExpandCollapse, SIGNAL(clicked()), this, SLOT(rxClicked())); } protected: void timerEvent(QTimerEvent *event) { if (event->timerId() == timerId) { int x = geometry().x(); int y = geometry().y(); int w = geometry().width(); int h = geometry().height(); setGeometry(x, y, w, h + increment); if (bExpand) { if (h > 300) { killTimer(timerId); timerId = 0; } } else { if (h < 40) { killTimer(timerId); timerId = 0; } } } } private slots: void rxClicked() { if (timerId == 0) timerId = startTimer(10); if (ppbExpandCollapse->text() == "Expand") { bExpand = true; ppbExpandCollapse->setText("Collapse"); increment = 10; //setSizeIncrement(0, 10); } else { bExpand = false; ppbExpandCollapse->setText("Expand"); increment = -10; } } private: QPushButton *ppbExpandCollapse; bool bExpand; int timerId; int increment; }; #endif

上面是实现展开和收缩的例子,给出调用,

  1. #include <QtGui/QApplication>  
  2. #include “test.h”  
  3. int main(int argc, char *argv[])  
  4. {  
  5.     QApplication a(argc, argv);  
  6.     CExpandCollapseWidget widget;  
  7.     widget.show();  
  8.     return a.exec();  
  9. }  
#include <QtGui/QApplication>




#include "test.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); CExpandCollapseWidget widget; widget.show(); return a.exec(); }

下面是运行结果:

        

当单击Expand按钮时,窗口呈现右边展开时的状态,当单击右边Collapse按钮时,窗口呈现左边收缩时候的状态。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值