首页 > 代码库 > qt实现类似qq的阴影窗体
qt实现类似qq的阴影窗体
#ifndef BFWIDGET_H #define BFWIDGET_H #include <QWidget> #include <QMouseEvent> #include <QResizeEvent> /** * 窗口可以拖动 * 窗口增加阴影效果 * @brief The BFWidget class */ class BFWidget : public QWidget { Q_OBJECT public: explicit BFWidget(QWidget *parent = 0); /** * 重写空间排版设置 * 把空间排版到central_widget上面 * @brief setLayout * @param layout */ void setLayout(QLayout * layout); signals: public slots: protected: void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); //void resizeEvent(QResizeEvent *event); private: //显示内容的区域 QWidget *central_widget; QPoint move_point; //移动的距离 bool mouse_press; //按下鼠标左键 }; #endif // BFWIDGET_H
<pre name="code" class="cpp">#include <QBoxLayout> #include <QPalette> #include <QGraphicsDropShadowEffect> #include <QDebug> #include <QSizePolicy> #include "bfwidget.h" BFWidget::BFWidget(QWidget *parent) : QWidget(parent) { setWindowFlags(Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground); central_widget = new QWidget(); central_widget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); ////水平方向和垂直方向任意伸展 QPalette palette; palette.setBrush(QPalette::Background,QBrush(QColor(255,255,255))); central_widget->setPalette(palette); central_widget->setAutoFillBackground(true); QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(); shadow_effect->setBlurRadius(10.0); shadow_effect->setColor(QColor(0, 0, 0, 100)); shadow_effect->setOffset(1.0); central_widget->setGraphicsEffect(shadow_effect); QVBoxLayout * main_layout = new QVBoxLayout(); main_layout->addWidget(central_widget, Qt::AlignCenter); main_layout->addSpacing(0); main_layout->setContentsMargins(5,5,5,5); QWidget::setLayout(main_layout); } void BFWidget::setLayout(QLayout *layout) { central_widget->setLayout(layout); } void BFWidget::mousePressEvent( QMouseEvent * event ) { //只能是鼠标左键移动和改变大小 if(event->button() == Qt::LeftButton) { mouse_press = true; } //窗口移动距离 move_point = event->globalPos() - pos(); } void BFWidget::mouseReleaseEvent(QMouseEvent *) { mouse_press = false; } void BFWidget::mouseMoveEvent(QMouseEvent *event) { //移动窗口 if(mouse_press) { QPoint move_pos = event->globalPos(); move(move_pos - move_point); } } //void BFWidget::resizeEvent(QResizeEvent *event) //{ // QSize size = event->size(); // central_widget->setGeometry(5, 5, size.width() - 10, size.height() - 10); // qDebug() << central_widget->rect(); //}
qt实现类似qq的阴影窗体
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。