转载自:http://blog.csdn.net/taiyang1987912/article/details/50682090
一、简介
在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_OBJECT
- public:
- WidgetScale(QWidget *parent = 0);
- ~WidgetScale();
- protected:
- void paintEvent(QPaintEvent *event);
- void resizeEvent(QResizeEvent * event);
- private:
- QPixmap backGroundPix;
- EPushButton *closeButton;
- };
- #endif // RIGHTPOP_H
- #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)若有问题或建议,请留言,在此感谢!
转载自:http://blog.csdn.net/taiyang1987912/article/details/50682090
一、简介
最近看到一篇Qt实现伸缩滑动的窗口的文章,但其代码不完整。便在此重新书写了完整的代码,并开源出来。窗口的中央有滑动条可以动态改变子窗口的大小,隐藏的按钮可以快速伸缩子窗口。其效果图如下:二、详解
1、代码
(1)slidingwindow.h- #ifndef SLIDINGWINDOW_H
- #define SLIDINGWINDOW_H
- #include <QtGui>
- class SlidingWindow : public QWidget
- {
- Q_OBJECT
- public:
- SlidingWindow(QWidget *parent = 0);
- ~SlidingWindow();
- protected:
- void mousePressEvent ( QMouseEvent * event);
- void mouseMoveEvent(QMouseEvent *event);
- void mouseReleaseEvent(QMouseEvent *event);
- void resizeEvent(QResizeEvent *event);
- bool eventFilter(QObject *obj, QEvent *event);
- private:
- void setBtnPos();
- void setBtnIcon();
- private slots:
- void slotClickedBtn();
- void slotSplitterMoved(int pos, int index);
- private:
- QPoint dragPosition;
- bool bPressFlag;
- QSplitter *splitter;
- QFrame *contentFrame;
- QFrame *listFrame;
- QPushButton *pushButton;
- QIcon leftIcon;
- QIcon rightIcon;
- };
- #endif // SLIDINGWINDOW_H
- #include "slidingwindow.h"
- SlidingWindow::SlidingWindow(QWidget *parent)
- : QWidget(parent, Qt::FramelessWindowHint)
- , bPressFlag(false)
- {
- resize(660, 460);
- splitter = new QSplitter(Qt::Horizontal, this);
- splitter->setHandleWidth(1);
- splitter->setStyleSheet("QSplitter::handle{background:#FFFFFF}");
- contentFrame = new QFrame(splitter);
- contentFrame->setStyleSheet("background:#000000");
- contentFrame->resize(475, height());
- contentFrame->setMinimumWidth(405);
- listFrame = new QFrame(splitter);
- listFrame->setStyleSheet("background:#323232");
- listFrame->resize(660 - 475, height());
- listFrame->setMaximumWidth(660 - 405);
- connect(splitter, SIGNAL(splitterMoved(int,int)), this, SLOT(slotSplitterMoved(int,int)));
- leftIcon = QIcon(":/left.png");
- rightIcon = QIcon(":/right.png");
- pushButton = new QPushButton(this);
- pushButton->setFocusPolicy(Qt::NoFocus);
- pushButton->hide();
- pushButton->setFixedSize(13, 42);
- pushButton->setIconSize(pushButton->size());
- pushButton->setStyleSheet("border:none;");
- pushButton->setIcon(rightIcon);
- pushButton->move(contentFrame->width() - pushButton->width()-2, (contentFrame->height() - pushButton->height())/2);
- connect(pushButton,SIGNAL(clicked()),this,SLOT(slotClickedBtn()));
- contentFrame->setMouseTracking(true);
- listFrame->setMouseTracking(true);
- contentFrame->installEventFilter(this);
- listFrame->installEventFilter(this);
- }
- SlidingWindow::~SlidingWindow()
- {
- }
- void SlidingWindow::resizeEvent(QResizeEvent *event)
- {
- splitter->setGeometry(0, 0, width(), height());
- move((QApplication::desktop()->width() - width())/2, (QApplication::desktop()->height() - height())/2);
- QWidget::resizeEvent(event);
- }
- bool SlidingWindow::eventFilter(QObject *obj, QEvent *event)
- {
- if (event->type() == QEvent::MouseMove) {
- QMouseEvent *mouseMove = static_cast<QMouseEvent*>(event);
- QRect rect = pushButton->frameGeometry();
- if (rect.contains(mouseMove->pos())) {
- pushButton->show();
- }
- else {
- pushButton->hide();
- }
- }
- return QWidget::eventFilter(obj, event);
- }
- void SlidingWindow::setBtnPos()
- {
- pushButton->move(contentFrame->width() - pushButton->width(), (contentFrame->height() - pushButton->height())/2);
- }
- void SlidingWindow::setBtnIcon()
- {
- if (listFrame->width() != 0) {
- pushButton->setIcon(rightIcon);
- }
- else {
- pushButton->setIcon(leftIcon);
- }
- }
- void SlidingWindow::slotClickedBtn()
- {
- QList <int> sizeList;
- sizeList.clear();
- if (listFrame->width() != 0) {
- sizeList.append(660);
- sizeList.append(0);
- }
- else {
- sizeList.append(475);
- sizeList.append(660 - 475);
- }
- splitter->setSizes(sizeList);
- setBtnIcon();
- setBtnPos();
- pushButton->hide();
- }
- void SlidingWindow::slotSplitterMoved(int pos, int index)
- {
- Q_UNUSED(pos)
- Q_UNUSED(index)
- setBtnIcon();
- setBtnPos();
- }
- void SlidingWindow::mousePressEvent ( QMouseEvent * event)
- {
- bPressFlag = true;
- dragPosition = event->pos();
- QWidget::mousePressEvent(event);
- }
- void SlidingWindow::mouseMoveEvent(QMouseEvent *event)
- {
- if (bPressFlag) {
- QPoint relaPos(QCursor::pos() - dragPosition);
- move(relaPos);
- }
- QWidget::mouseMoveEvent(event);
- }
- void SlidingWindow::mouseReleaseEvent(QMouseEvent *event)
- {
- bPressFlag = false;
- QWidget::mouseReleaseEvent(event);
- }
- #include "slidingwindow.h"
- #include <QApplication>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- SlidingWindow w;
- w.show();
- return a.exec();
- }
三、总结
(1)获取位置坐标可以尝试QPoint realPos(QCursor::pos() - this->pos());,这样不用类型转换。主要设置setMouseTracking(true);才能当鼠标不点击时移动鼠标进入mouseMoveEvent事件函数。(2)源码已上传到csdn:http://download.csdn.net/detail/taiyang1987912/9439233。
(3)若有问题或建议,请留言,在此感谢!