我是靠谱客的博主 发嗲百合,这篇文章主要介绍QDialog无边框一.问题概述二.代码,现在分享给大家,希望可以做个参考。

一.问题概述

问题描述: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

源文件:

#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);
}

调用

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

最后

以上就是发嗲百合最近收集整理的关于QDialog无边框一.问题概述二.代码的全部内容,更多相关QDialog无边框一内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(125)

评论列表共有 0 条评论

立即
投稿
返回
顶部