Qt浅谈之窗体缩放(仅增加测试代码)

来源:互联网 发布:ubuntu怎么读音标 编辑:程序博客网 时间:2024/06/07 20:52

一、简介

        在csdn上看到了一篇窗体动态缩放的文章,觉得很有意思,在作者的代码上加上了测试程序,并把所有代码放在csdn上供下载。程序运行的效果:

二、详解

1、测试代码(完整代码csdn:http://download.csdn.net/detail/taiyang1987912/9434679)

(1)widgetscale.h
#ifndef RIGHTPOP_H#define RIGHTPOP_H#include "epushbutton.h"#include <QtGui>class WidgetScale : public QWidget{    Q_OBJECTpublic:    WidgetScale(QWidget *parent = 0);    ~WidgetScale();protected:    void paintEvent(QPaintEvent *event);    void resizeEvent(QResizeEvent * event);private:    QPixmap backGroundPix;    EPushButton *closeButton;};#endif // RIGHTPOP_H
(2)widgetscale.cpp
#include "widgetscale.h"#include "framelesshelper.h"WidgetScale::WidgetScale(QWidget *parent)    : QWidget(parent, Qt::FramelessWindowHint){    resize(300, 200);    backGroundPix.load(":/background.png");    backGroundPix = backGroundPix.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);    closeButton = new EPushButton(this);    closeButton->setPixName(":/close");    closeButton->setToolTip(tr("close"));    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));    //this指的是要处理的窗体    FramelessHelper *pHelper = new FramelessHelper(this);    pHelper->activateOn(this);  //激活当前窗体    pHelper->setTitleHeight(30);  //设置窗体的标题栏高度    pHelper->setWidgetMovable(true);  //设置窗体可移动    pHelper->setWidgetResizable(true);  //设置窗体可缩放    pHelper->setRubberBandOnMove(true);  //设置橡皮筋效果-可移动    pHelper->setRubberBandOnResize(true);  //设置橡皮筋效果-可缩放    move((QApplication::desktop()->width() - width())/2,  (QApplication::desktop()->height() - height())/2);}WidgetScale::~WidgetScale(){}void WidgetScale::paintEvent(QPaintEvent *event){    QPainter painter(this);    painter.drawPixmap(0, 0, width(), height(), backGroundPix);    painter.setFont(QFont("arial", 10, QFont::Bold));    painter.setPen(QColor("#FFFFFF"));    painter.drawText(QRectF(5, 5, 100, 35), tr("Happy Coder"));    painter.setPen(Qt::NoPen);    painter.setBrush(QColor("#2EB49E"));    painter.drawRect(QRectF(0, 30, width(), height() - 30));    QWidget::paintEvent(event);}void WidgetScale::resizeEvent(QResizeEvent *event){    closeButton->move(width() - 27, 0);    QWidget::resizeEvent(event);}

三、总结

(1)代码中还有很多自己不懂的地方,待以后慢慢体会,核心代码参考:http://blog.csdn.net/liang19890820/article/details/50557240。
(2)若有问题或建议,请留言,在此感谢!

2 0
原创粉丝点击