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

几何篇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)

(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();
}

下面是输出结果:

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();
}

下面是输出结果:

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();
}

下面是输出结果:

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();
}

下面是输出结果:

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();
}

下面是输出结果:

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();
}

下面是输出结果:
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

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

#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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RabinSong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值