几何篇2介绍了窗口的平移方法,这一篇介绍窗口的缩放方法,通过窗口缩放,你可以做窗口慢慢展开和慢慢隐藏的效果,本篇我们先介绍窗口缩放方法,然后介绍一个窗口慢慢展开和慢慢隐藏的实例,窗口缩放的方法有2种,一种是采用resize的方式,另一种是采用setGeometry的方式,下面是QT提供的对应函数接口:
- 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)
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方法,窗口没有父窗口
- #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();
- }
#include <QtGui/QApplication>
下面是输出结果:
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方法,无边框窗口
- #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();
- }
#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方法,窗口有父窗口
- #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();
- }
#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方法,窗口无父窗口
- #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();
- }
#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方法,无边框窗口
- #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();
- }
#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方法,有父窗口
- #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();
- }
#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个窗口展开和收缩的实例:
- #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
#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
上面是实现展开和收缩的例子,给出调用,
- #include <QtGui/QApplication>
- #include “test.h”
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- CExpandCollapseWidget widget;
- widget.show();
- return a.exec();
- }
#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按钮时,窗口呈现左边收缩时候的状态。