本文主要是介绍Qt浅谈之窗体缩放(仅增加测试代码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、简介
在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)若有问题或建议,请留言,在此感谢!
这篇关于Qt浅谈之窗体缩放(仅增加测试代码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!