当前位置:   article > 正文

QDialog无边框_qt dialog无边框

qt dialog无边框

一.问题概述

问题描述:Qt对话框默认调用windows系统自带边框,部分情况不符合使用需求。
解决思路:重写QDialog。

二.代码

头文件

#ifndef MYDIALOG_H
#define MYDIALOG_H

#include <QDialog>

class QPaintEvent;
class QMouseEvent;

class FramelessDialog : public QDialog
{
public:
    FramelessDialog(QWidget *parent = 0);
protected:
    void changeTitle(QDialog *oldDialog);
protected:
    void paintEvent(QPaintEvent *);
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
private:
    int countFlag(QPoint p);
    void setCursorType(int flag);
private:
    QPoint mPressedPoint;
    bool mPressed;
    int mCurPos;
};
#endif  // MYDIALOG_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

源文件:

#include "framelessDialog.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QLabel>
#include <QLayout>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QPushButton>
#include <QStyleOption>
#include <QVBoxLayout>

#define MARGIN 10  //四个角的长度

FramelessDialog::FramelessDialog(QWidget *parent):QDialog(parent)
{
}
void FramelessDialog::changeTitle(QDialog *oldDialog)
{
    //做新的标头,目前有标题,关闭按钮。 可以自行添加相关按钮
    QString titleStr = oldDialog->windowTitle();
    QLabel *titleLabel = new QLabel(titleStr);
    QFont font("Microsoft YaHei", 12, 75);  //第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75)
    titleLabel->setFont(font);

    QPushButton *closeButton = new QPushButton();  //关闭按钮
    closeButton->setStyleSheet("border:none; ");//:/tiltlebar/res/icon/title/关闭.png
    closeButton->setIcon(QIcon(QString::fromLocal8Bit(":/tiltlebar/res/icon/title/关闭.png")));  //设置关闭按钮图片
    closeButton->setIconSize(QSize(30, 30));     //设置关闭按钮大小

    QHBoxLayout *titleLayout = new QHBoxLayout();
    titleLayout->setMargin(10);
    titleLayout->addWidget(titleLabel);
    titleLayout->addStretch(1);
    titleLayout->addWidget(closeButton);

    //重新布局界面,标头+原始内容界面
    QVBoxLayout *layout = new QVBoxLayout;
    layout->setContentsMargins(0, 7, 0, 0);  //这里决定颜色边框的宽度,顺序为左,上,右,下
    layout->addLayout(titleLayout);
    layout->addWidget(oldDialog);
    setLayout(layout);

    oldDialog->setWindowFlags(Qt::FramelessWindowHint);
    this->setWindowFlags(Qt::Dialog |Qt::FramelessWindowHint);
    resize(oldDialog->width(), oldDialog->height());
    QMetaObject::connectSlotsByName(this);
    connect(closeButton, &QPushButton::clicked, this, &FramelessDialog::reject);
    connect(oldDialog, &QDialog::rejected, this, &FramelessDialog::reject);
    connect(oldDialog, &QDialog::accepted, this, &FramelessDialog::accept);
    //this->setStyleSheet("background-color:#ffffff");
}

void FramelessDialog::paintEvent(QPaintEvent *)
{
    //    QStyleOption opt;
    //    opt.initFrom(this);
    //    QPainter p(this);
    //    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);  //绘制样式
    //绘制边框
    QPen pen; //画笔。绘制图形边线,由颜色、宽度、线风格等参数组成
    pen.setColor(QColor(43, 127, 243)); //设置画笔颜色
    QLayout *mainLayout = layout();
    int left, top, right, bottom;
    mainLayout->getContentsMargins(&left, &top, &right, &bottom);
    int w = width();
    int h = height();
    QBrush brush;
    brush.setColor(QColor(43, 127, 243));  //设置画刷颜色
    brush.setStyle(Qt::SolidPattern);
    QPainter painter(this);
    painter.setPen(pen);
    painter.setBrush(brush);

    if (left)
        painter.drawRect(0, 0, left, h);  //左
    if (right)
        painter.drawRect(w - left, 0, right, h);  //右
    if (top)
        painter.drawRect(-1, 0, w + 2, top);  //上
    if (bottom)
        painter.drawRect(-1, h - top, w + 2, bottom);  //下
}

//以下部分为窗口移动与缩放
void FramelessDialog::mousePressEvent(QMouseEvent *event)  //鼠标按下事件
{
    int poss = countFlag(event->pos());
    setCursorType(poss);
    if (event->button() == Qt::LeftButton)
    {
        this->mPressed = true;
        QPoint temp = event->globalPos();
        mPressedPoint = temp;
        mCurPos = countFlag(event->pos());
    }
    QWidget::mousePressEvent(event);
}

void FramelessDialog::mouseReleaseEvent(QMouseEvent *event)  //鼠标释放事件
{
    if (mPressed)
        mPressed = false;
    QApplication::restoreOverrideCursor();  //恢复鼠标指针性状
    QWidget::mouseReleaseEvent(event);
}

void FramelessDialog::mouseDoubleClickEvent(QMouseEvent *event)  //鼠标双击 全屏
{
    if (event->button() == Qt::LeftButton && countFlag(event->pos()) == 44)
    {
        if (windowState() != Qt::WindowFullScreen)
            setWindowState(Qt::WindowFullScreen);
        else
            setWindowState(Qt::WindowNoState);  //恢复正常模式
    }
    QWidget::mouseDoubleClickEvent(event);
}

void FramelessDialog::mouseMoveEvent(QMouseEvent *event)  //鼠标移动事件
{
    if (mPressed)  //是否左击
    {
        QPoint ptemp = event->globalPos();
        ptemp = ptemp - mPressedPoint;
        if (mCurPos == 44)  //移动窗口 22
        {
            ptemp = ptemp + pos();
            move(ptemp);
        }
        else
        {
            QRect wid = geometry();

            switch (mCurPos)  //改变窗口的大小
            {

            case 11:
                wid.setTopLeft(wid.topLeft() + ptemp);
                break;  //左上角
            case 13:
                wid.setTopRight(wid.topRight() + ptemp);
                break;  //右上角
            case 31:
                wid.setBottomLeft(wid.bottomLeft() + ptemp);
                break;  //左下角
            case 33:
                wid.setBottomRight(wid.bottomRight() + ptemp);
                break;  //右下角
            case 12:
                wid.setTop(wid.top() + ptemp.y());
                break;  //中上角
            case 21:
                wid.setLeft(wid.left() + ptemp.x());
                break;  //中左角
            case 23:
                wid.setRight(wid.right() + ptemp.x());
                break;  //中右角
            case 32:
                wid.setBottom(wid.bottom() + ptemp.y());
                break;  //中下角
            }
            setGeometry(wid);
        }

        mPressedPoint = event->globalPos();  //更新位置
    }
    QWidget::mouseMoveEvent(event);
}

int FramelessDialog::countFlag(QPoint p)  //计算鼠标在哪一列和哪一行
{
    int col = (p.y() < MARGIN) ? 1 : (p.y() > (this->height() - MARGIN) ? 3 : 2);
    int row = (p.x() < MARGIN) ? 1 : (p.x() > (this->width() - MARGIN) ? 3 : 2);
    int pos = col * 10 + row;
    if (QRect(0, 0, width(), 50).contains(p))
        pos = 44;
    return pos;
}

void FramelessDialog::setCursorType(int flag)  //根据鼠标所在位置改变鼠标指针形状
{
    Qt::CursorShape cursor;
    switch (flag)
    {
    case 11:
    case 33:
        cursor = Qt::SizeFDiagCursor;
        break;
    case 13:
    case 31:
        cursor = Qt::SizeBDiagCursor;
        break;
    case 21:
    case 23:
        cursor = Qt::SizeHorCursor;
        break;
    case 12:
    case 32:
        cursor = Qt::SizeVerCursor;
        break;
    //case 22:
    case 44:
        cursor = Qt::OpenHandCursor;
        break;
    default:
        //  QApplication::restoreOverrideCursor();//恢复鼠标指针性状
        break;
    }
    QApplication::setOverrideCursor(cursor);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211

调用

Dlg::Dlg(QWidget *parent) :
    FramelessDialog(parent),
    ui(new Ui::Dlg)
{
    //ui->setupUi(this);
    QDialog *dlg = new QDialog;
    ui->setupUi(dlg);
    changeTitle(dlg);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/94937
推荐阅读
相关标签
  

闽ICP备14008679号