Qt跨平台无边框窗口探索记录

一、前言

实现的效果为:通过黑色矩形框预操作,鼠标释放时更新窗口。效果图如下:

1.功能

1.1 已实现功能

  1. 8个方向的缩放
  2. 标题栏拖动
  3. 标题栏双击最大化/正常窗口
  4. 窗口最小尺寸
  5. 预操作框颜色与背景色互补
  6. 多屏幕
  7. 默认标题栏

1.2 待开发功能

  1. 拖动到屏幕四周进行半屏全屏。

1.3 存在的问题

1.QWebEngineView

从测试效果来看,对于普通widget效果较好,但是如果是嵌入了QWebEngineView,且其位于窗口四周,就会出现问题。

其主要原因在于,当事件进入QWebEngineView后,会被其child(QtWebEngineCore::RenderWidgetHostViewQtDelegateWidget)"吃掉",导致后续事件不再进入主窗口,从而无法触发我们的代码。这时我们还可以遍历QWebEngineView的children,将事件安装到主窗口

        auto childs =  view->children();
        for(auto child:childs){
						child->installEventFilter(this);
        }

但是这样显得太笨重了。而且,即使如此,鼠标样式的修改,也会被QWebEngineView刷掉。因此对于QWebEngineView下是存在一些问题的。

2.多屏幕拖动时,从低缩放比拖到高缩放比释放时,会闪烁一次,目前没有进一步分析原因。

2.探索的过程

2.1 关于无边框缩放的实现方式

目前网上无非两种方式:

a)通过nativeEvent()实现。c++ - Qt/Windows, resizable frameless window - Stack Overflow

b)通过窗口的eventFilter()直接实现。

但是问题是前者基于windows接口不跨平台,而后者则会出现抖动。

本文则是基于第二种方式,并参考windows窗口的拖动方式,通过预操作框避免抖动的问题。

2.2 实现思路

  1. 首先关于无边框窗口,通过Qt标志处理:
setWindowFlags(Qt::FramelessWindowHint);
  1. 然后关于缩放和拖动,与网上大多数实现一致,通过九宫格法实现:

当鼠标位于不同区域时,触发不同的操作。

  1. 而对于预处理框,则是通过一个覆盖整个屏幕的透明窗口,并重写paintEvent在其中绘制一个rect。

2.3 实现难点

1.首先是九宫格的思路,了解了思路之后,实现就比较简单了

2.对于预处理框,首先是需要以全屏幕作为geometry,否则会导致拖不到非主屏幕,或者位置错误灯问题

3.其次是预处理框要实现window类似的与背景色互补(而非纯色),需要获取全屏幕图片作为背景绘制到预操作框,然后再通过QPainter::RasterOp_SourceAndNotDestination模式取反。

4.最后就是对于多屏幕的位置,缩放比灯。

二、代码

目录结构如下:

其中:

  • cursorTransform是用于鼠标位置和方向的计算
  • framelessData则是一些全局变量
  • framelessHelper则是对外的接口,
  • titleBar表示默认标题栏
  • transparentRect则是实现预操作框

详细代码如下:

#include "cursorTransform.h"
#include <QDebug>


AreaType CursorTransform::calculateCursorType(const QSize &size, const QPoint &pos)
{
    //TODO:minsize
    auto w = size.width();
    auto h = size.height();
    auto x = pos.x();
    auto y = pos.y();
    //left
    if(x<=FrameLessData::borderThickness){
        if(y<=FrameLessData::borderThickness)
            return AreaType::LeftTopArea;
        else if(y>=h-FrameLessData::borderThickness)
            return AreaType::LeftBottomArea;
        else
            return AreaType::LeftArea;
    }//right
    else if(x>=w-FrameLessData::borderThickness){
        if(y<=FrameLessData::borderThickness)
            return AreaType::RightTopArea;
        else if(y>=h-FrameLessData::borderThickness)
            return AreaType::RightBottomArea;
        else
            return AreaType::RightArea;
    }
    else{//center
        if(y<=FrameLessData::borderThickness)
            return AreaType::TopArea;
        else if(y<=FrameLessData::titleHeight)
                return AreaType::TitleArea;
        else if(y>=h-FrameLessData::borderThickness)
            return AreaType::BottomArea;
        else
            return AreaType::CenterArea;
    }

}

Qt::CursorShape CursorTransform::AreaType2CursorShape(AreaType type)
{
    Qt::CursorShape cursorType(Qt::ArrowCursor);
    switch (type) {
    case AreaType::LeftTopArea:
    case AreaType::RightBottomArea:
        cursorType=Qt::SizeFDiagCursor;
        break;
    case AreaType::RightTopArea:
    case AreaType::LeftBottomArea:
        cursorType=Qt::SizeBDiagCursor;
        break;
    case AreaType::TopArea:
    case AreaType::BottomArea:
        cursorType=Qt::SizeVerCursor;
        break;
    case AreaType::LeftArea:
    case AreaType::RightArea:
        cursorType=Qt::SizeHorCursor;
        break;
    default:
        break;
    }

    return cursorType;
}

void CursorTransform::calculateRect(AreaType type,QRect &rect, const QPoint &lastPos,const QPoint &newPos)
{
    int xOff=0,yOff=0,wOff=0,hOff=0;
    int posXOff=newPos.x()-lastPos.x();
    int posYOff = newPos.y()-lastPos.y();
    switch (type) {
    case AreaType::LeftTopArea:
        xOff = posXOff;
        yOff = posYOff;
        break;
    case AreaType::TopArea:
        yOff = posYOff;
        break;
    case AreaType::RightTopArea:
        yOff = posYOff;
        wOff = posXOff;
        break;
    case AreaType::LeftArea:
        xOff = posXOff;
        break;
    case AreaType::RightArea:
        wOff = posXOff;
        break;
    case AreaType::LeftBottomArea:
        xOff = posXOff;
        hOff = posYOff;
        break;
    case AreaType::BottomArea:
        hOff = posYOff;
        break;
    case AreaType::RightBottomArea:
        wOff = posXOff;
        hOff = posYOff;
        break;
    case AreaType::TitleArea:   //move
        rect.moveTopLeft(rect.topLeft()+QPoint(posXOff,posYOff));
        break;
    default:
        break;
    }
    //min judget:
    if(rect.width()-xOff<=FrameLessData::minWidth)
        xOff=0;
    else if(rect.left()<newPos.x() && xOff<0)
        xOff=0;
    if(rect.width()+wOff<=FrameLessData::minWidth)
        wOff=0;
    else if(rect.right()>newPos.x() && wOff>0)
        wOff=0;

    if(rect.height()-yOff<=FrameLessData::minHeight)
        yOff=0;
    else if(rect.top()<newPos.y() && yOff<0)
        yOff=0;
    if(rect.height()+hOff<=FrameLessData::minHeight)
        hOff=0;
    else if(rect.bottom()>newPos.y() && hOff>0)
        hOff=0;

    //resize:
    rect.setTop(rect.top()+yOff);
    rect.setLeft(rect.left()+xOff);
    rect.setWidth(rect.width()+wOff);
    rect.setHeight(rect.height()+hOff);
}

#ifndef CURSORTRANSFORM_H
#define CURSORTRANSFORM_H
#include "framelessData.h"
#include <QRect>

class CursorTransform
{
public:
    static AreaType calculateCursorType(const QSize &size, const QPoint &pos);
    static enum Qt::CursorShape AreaType2CursorShape(AreaType type);

    static void calculateRect(AreaType type,QRect &rect,const QPoint &lastPos,const QPoint &newPos);
private:
};

#endif // CURSORTRANSFORM_H
#include "framelessData.h"

int FrameLessData::borderThickness=4;
int FrameLessData::titleHeight=30;
int FrameLessData::minHeight=355;
int FrameLessData::minWidth=220;
QColor FrameLessData::borderColor=(Qt::white);
QColor FrameLessData::transparentRectBkground=(Qt::transparent);
enum Qt::PenStyle FrameLessData::borderStyle=(Qt::SolidLine);
#ifndef FRAMELESSDATA_H
#define FRAMELESSDATA_H
#include <QColor>
#include <QPoint>
#include <QSize>

//透明矩形的属性

class FrameLessData{
public:
    static int borderThickness;
    static int titleHeight;
    static int minHeight;
    static int minWidth;
    static QColor borderColor;
    static QColor transparentRectBkground;
    static enum Qt::PenStyle borderStyle;
};




enum class AreaType
{
    LeftTopArea,
    TopArea,
    RightTopArea,
    LeftArea,
    RightArea,
    LeftBottomArea,
    BottomArea,
    RightBottomArea,
    CenterArea,
    TitleArea,
    None
};

enum class WidgetOperator
{
    None,
    Resize,
    Move
};

#endif // FRAMELESSDATA_H
#include "framelessHelper.h"
#include "cursorTransform.h"
#include <QEvent>
#include <QSize>
#include <QPoint>
#include <QCursor>
#include <QWidget>
#include <QPainter>
#include <QDebug>



FrameLessHelper::FrameLessHelper(QWidget *widget)
    : m_widget(widget)
    , m_transparentRect(new TransparentRect())
    , m_widgetOperator(WidgetOperator::None)
    , m_isMove(false)
#ifdef TITLEBAR
    , m_titleBar(nullptr)
#endif
{
    m_transparentRect->setRect(m_widget->frameGeometry());
#ifdef TITLEBAR
    //if use title
    m_titleBar = new TitleBar(m_widget);

    connect(m_titleBar,&TitleBar::minimumSizeClick,this,&FrameLessHelper::onMinClick);
    connect(m_titleBar,&TitleBar::showNormalClick,this,&FrameLessHelper::onMaxClick);
    connect(m_titleBar,&TitleBar::closeClick,this,&FrameLessHelper::onCloseClick);
#endif
}

bool FrameLessHelper::eventFilter(QObject *watched, QEvent *event)
{
    int ret=false;
    switch (event->type()) {
    case QEvent::MouseButtonDblClick:   //double click
        ret = onDbClickEvent(static_cast<QMouseEvent *>(event));
        break;
    case QEvent::MouseButtonPress:
        ret = onMousePressEvent(static_cast<QMouseEvent *>(event));
        break;
    case QEvent::MouseButtonRelease:
        ret = onMouseReleaseEvent(static_cast<QMouseEvent *>(event));
        break;
    case QEvent::MouseMove:
        ret = onMoveEvent(static_cast<QMouseEvent *>(event));
        break;
    case QEvent::HoverMove:
        case QEvent::HoverEnter:
        ret = onHoverEvent(watched,static_cast<QHoverEvent *>(event));
        break;
    default:
        break;
    }
    //触发返回true
    if(ret)
        return ret;

    return QObject::eventFilter(watched,event);
}

void FrameLessHelper::setBorderThickness(int thickness)
{
    FrameLessData::borderThickness = thickness;
}

void FrameLessHelper::setTitleHeight(int height)
{
    FrameLessData::titleHeight = height;
}

void FrameLessHelper::setMinHeight(int height)
{
    FrameLessData::minHeight = height;
}

void FrameLessHelper::setMinWidth(int width)
{
    FrameLessData::minWidth = width;
}
#ifdef TITLEBAR
QWidget *FrameLessHelper::getTitleBar()
{
    return m_titleBar;
}

void FrameLessHelper::onMinClick()
{
    m_widget->showMinimized();
}

void FrameLessHelper::onMaxClick()
{
    if(m_widget->isFullScreen())
        m_widget->showNormal();
    else
        m_widget->showFullScreen();
}

void FrameLessHelper::onCloseClick()
{
    m_widget->close();
}
#endif
bool FrameLessHelper::onHoverEvent(QObject *watched,QHoverEvent *event)
{
    //操作的时候不响应hover
    if(m_widgetOperator!=WidgetOperator::None || m_widget->isFullScreen())
        return false;
    //hover move
    auto areatype = CursorTransform::calculateCursorType(m_widget->size(),event->pos());
    auto w=static_cast<QWidget *>(watched);
    if(w)
        w->setCursor(CursorTransform::AreaType2CursorShape(areatype));
    else
        m_widget->setCursor(CursorTransform::AreaType2CursorShape(areatype));
    return true;
    //hover leave -> Qt::ArrowCursor
    //not need
}

bool FrameLessHelper::onDbClickEvent(QMouseEvent *event)
{
    if(event->button()!=Qt::LeftButton){
        return false;
    }
    auto areatype = CursorTransform::calculateCursorType(m_widget->size(),event->globalPos()-m_widget->pos());
    if(areatype==AreaType::TitleArea){
        if(m_widget->isFullScreen()){
            m_widget->showNormal();
        }
        else
        {
            m_widget->showFullScreen();
        }
#ifdef TITLEBAR
        m_titleBar->onMaxClick();
#endif
    }
    return true;
}

bool FrameLessHelper::onMousePressEvent(QMouseEvent *event)
{
    if(event->button()!=Qt::LeftButton){
        return false;
    }

    auto type = CursorTransform::calculateCursorType(m_widget->size(),event->globalPos()-m_widget->pos());

    if(type<AreaType::CenterArea){
        //resize
        m_widgetOperator = WidgetOperator::Resize;

        // not start show rect
        m_transparentRect->setRect(m_widget->frameGeometry());
        m_transparentRect->onMousePressEvent(event,type);

        return true;
    }
    else if(type==AreaType::TitleArea){
        //move
        m_widgetOperator = WidgetOperator::Move;
        if(!m_widget->isFullScreen())
            m_transparentRect->setRect(m_widget->geometry());
        else{
            m_transparentRect->setRect(calMaxMoveRect(event->globalPos()));
        }

        m_transparentRect->onMousePressEvent(event,type);
        return true;
    }
    return false;
}

bool FrameLessHelper::onMoveEvent(QMouseEvent *event)
{
    if(m_widgetOperator==WidgetOperator::None ||
        (m_widgetOperator == WidgetOperator::Resize && m_widget->isFullScreen())){
        return false;
    }

#ifdef TITLEBAR
    if(m_widget->isFullScreen())
        m_titleBar->onMaxClick();
#endif

    m_isMove =true;
    m_transparentRect->show();
    m_transparentRect->onMoveEvent(event,event->globalPos()-m_widget->pos());
    return false;
}

bool FrameLessHelper::onMouseReleaseEvent(QMouseEvent *event)
{
    if(event->button()!=Qt::LeftButton || m_widgetOperator==WidgetOperator::None){
        return false;
    }
    
    m_transparentRect->onMouseReleaseEvent(event);

	m_transparentRect->hide();
    if(m_isMove){
        if(m_widget->isFullScreen())
            m_widget->showNormal();
        auto &&rect = m_transparentRect->getRect();
		m_widget->move(rect.topLeft());
        m_widget->resize(rect.size());
    }
    m_isMove = false;
    m_widgetOperator = WidgetOperator::None;
    return false;
}

QRect FrameLessHelper::calMaxMoveRect(const QPoint &evPos)
{
    //最大化时拖动标题栏的规则:宽度分为3份,鼠标位于左侧则向左上角对齐,右侧则向右上角对齐,中间部分则按中间对齐
    auto rect = m_widget->normalGeometry();
    rect.moveTop(0);
    double gPosX=evPos.x();
    double step = m_widget->geometry().width()/3.0;
    double left=0;
    if(gPosX>step*2){
        left=step*3-rect.width();
    }
    else if(gPosX>step && gPosX<=step*2){
        left=gPosX-rect.width()/2.0;
    }
    rect.moveLeft(left);
    return rect;
}

#ifndef FRAMELESSHELPER_H
#define FRAMELESSHELPER_H
#include "transparentRect.h"
#include "titleBar.h"
#include "cursorTransform.h"

#include <QObject>
#include <QMouseEvent>
#include <QHoverEvent>
#include <QRect>
#include <QWidget>

/*
FrameLessHelper
1.管理窗口无边框功能
2.输入为QWidget(构造)和QEvent(eventFilter)
*/
class FrameLessHelper : public QObject
{
    Q_OBJECT
public:
    FrameLessHelper(QWidget *widget);
    bool eventFilter(QObject *watched, QEvent *event) override;
    void setBorderThickness(int thickness);
    void setTitleHeight(int height);
    void setMinHeight(int height);
    void setMinWidth(int width);
#ifdef TITLEBAR
    QWidget *getTitleBar();

    void onMinClick();
    void onMaxClick();
    void onCloseClick();
#endif
private:
    bool onHoverEvent(QObject *watched,QHoverEvent *event);
    bool onDbClickEvent(QMouseEvent *event);
    bool onMousePressEvent(QMouseEvent *event);
    bool onMoveEvent(QMouseEvent *event);
    bool onMouseReleaseEvent(QMouseEvent *event);

    QRect calMaxMoveRect(const QPoint &evPos);
    //代理窗口
    QWidget *m_widget;
    //预拖动窗口
    TransparentRect *m_transparentRect;
    enum WidgetOperator m_widgetOperator;
    bool m_isMove;
#ifdef TITLEBAR
    TitleBar *m_titleBar;
#endif
};
#endif // FRAMELESSHELPER_H
#include "titleBar.h"
#include <QIcon>
#include <QPalette>

int iconSize=30;


TitleBar::TitleBar(QWidget *parent,bool isMax)
    : QWidget{parent}
    , m_minBtn{new QPushButton(this)}
    , m_maxBtn{new QPushButton(this)}
    , m_closeBtn{new QPushButton(this)}
    , m_hLayout{new QHBoxLayout(this)}
    , m_spacer{new QSpacerItem(500,30,QSizePolicy::Expanding)}
    , m_isMax{isMax}
{
    m_minBtn->setIcon(QIcon(":/img/min.png"));
    QString maxPic = m_isMax?":/img/normal.png":":/img/max.png";
    m_maxBtn->setIcon(QIcon(maxPic));
    m_closeBtn->setIcon(QIcon(":/img/close.png"));

    connect(m_minBtn,&QPushButton::clicked,this,&TitleBar::minimumSizeClick);
    connect(m_maxBtn,&QPushButton::clicked,this,&TitleBar::showNormalClick);
    connect(m_maxBtn,&QPushButton::clicked,this,&TitleBar::onMaxClick);
    connect(m_closeBtn,&QPushButton::clicked,this,&TitleBar::closeClick);


    m_minBtn->resize(iconSize,iconSize);
    m_maxBtn->resize(iconSize,iconSize);
    m_maxBtn->resize(iconSize,iconSize);


    m_hLayout->addSpacerItem(m_spacer);
    m_hLayout->addWidget(m_minBtn);
    m_hLayout->addWidget(m_maxBtn);
    m_hLayout->addWidget(m_closeBtn);
    m_hLayout->setSpacing(0);
    m_hLayout->setContentsMargins(0,0,0,0);

    QPalette palette(this->palette());
    palette.setColor(QPalette::Background, QColor(100,100,100));
    this->setAutoFillBackground(true);
    this->setPalette(palette);
}

void TitleBar::onMaxClick()
{
    if(m_isMax){
        m_maxBtn->setIcon(QIcon(":/img/max.png"));
        m_isMax=false;
    }
    else{
        m_maxBtn->setIcon(QIcon(":/img/normal.png"));
        m_isMax=true;
    }
}
#ifndef TITLEBAR_H
#define TITLEBAR_H

#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
#include <QSpacerItem>


class TitleBar : public QWidget
{
    Q_OBJECT
public:
    explicit TitleBar(QWidget *parent = nullptr,bool isMax=false);
    void onMaxClick();
signals:
    void minimumSizeClick();
    void showNormalClick();
    void closeClick();

private:
    QPushButton *m_minBtn;
    QPushButton *m_maxBtn;
    QPushButton *m_closeBtn;

    QHBoxLayout *m_hLayout;
    QSpacerItem *m_spacer;
    bool m_isMax;
};

#endif // TITLEBAR_H
#include "transparentRect.h"
#include "framelessData.h"
#include <QMouseEvent>
#include <QDesktopWidget>
#include <QDebug>
#include <QPainter>
#include <QPen>
#include <QScreen>
#include <QGuiApplication>
#include <QList>




TransparentRect::TransparentRect(QWidget *parent)
    : QWidget(parent)
    , m_areaType(AreaType::None)
{
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
}

void TransparentRect::setRect(const QRect &rect)
{
    m_rect=rect;
}

QRect TransparentRect::getRect() const
{
    return m_rect;
}

void TransparentRect::onMousePressEvent(QMouseEvent *event,AreaType type)
{
    m_lastPos = event->pos();
    //
    auto screens = QGuiApplication::screens();
    QRect rect;
	//QPainter painter(&m_pixmap);
    for(auto s:screens){
		auto geom = s->geometry();
        rect |= geom;
		//auto pixmap = s->grabWindow(0, geom.x(), geom.y(), geom.width(), geom.height());
		//static int i = 0;
		//QFile file(QString("./test%1.png").arg(i++));
		//file.open(QIODevice::WriteOnly);
		//pixmap.save(&file);
		//file.close();
		//painter.drawPixmap(s->geometry(), pixmap);
    }
	//painter.end();
	m_startInch = QGuiApplication::screenAt(event->globalPos())->logicalDotsPerInch();
    setGeometry(rect);
	m_pixmap= QGuiApplication::primaryScreen()->grabWindow(0, rect.x(), rect.y(), rect.width(), rect.height());
    m_areaType = type;

#if  0
	//
	QFile file("./test.png");
	file.open(QIODevice::WriteOnly);
	m_pixmap.save(&file);
	file.close();
#endif //  0
}

void TransparentRect::onMoveEvent(QMouseEvent *event,const QPoint &newPos)
{
	
	m_scaled = QGuiApplication::screenAt(event->globalPos())->logicalDotsPerInch()/ m_startInch;

//    auto newPos = event->pos();
    int xOff=0,yOff=0,wOff=0,hOff=0;
    int posXOff=newPos.x()-m_lastPos.x();
    int posYOff = newPos.y()-m_lastPos.y();
    switch (m_areaType) {
    case AreaType::LeftTopArea:
        xOff = posXOff;
        yOff = posYOff;
        break;
    case AreaType::TopArea:
        yOff = posYOff;
        break;
    case AreaType::RightTopArea:
        yOff = posYOff;
        wOff = posXOff;
        break;
    case AreaType::LeftArea:
        xOff = posXOff;
        break;
    case AreaType::RightArea:
        wOff = posXOff;
        break;
    case AreaType::LeftBottomArea:
        xOff = posXOff;
        hOff = posYOff;
        break;
    case AreaType::BottomArea:
        hOff = posYOff;
        break;
    case AreaType::RightBottomArea:
        wOff = posXOff;
        hOff = posYOff;
        break;
    case AreaType::TitleArea:   //move
        m_rect.moveTopLeft(m_rect.topLeft()+QPoint(posXOff,posYOff));
        break;
    default:
        break;
    }
    //min judget:
    if(m_rect.width()-xOff<=FrameLessData::minWidth)
        xOff=0;
    else if(m_rect.left()<event->globalPos().x() && xOff<0)
        xOff=0;
    if(m_rect.width()+wOff<=FrameLessData::minWidth)
        wOff=0;
    else if(m_rect.right()>event->globalPos().x() && wOff>0)
        wOff=0;

    if(m_rect.height()-yOff<=FrameLessData::minHeight)
        yOff=0;
    else if(m_rect.top()<event->globalPos().y() && yOff<0)
        yOff=0;
    if(m_rect.height()+hOff<=FrameLessData::minHeight)
        hOff=0;
    else if(m_rect.bottom()>event->globalPos().y() && hOff>0)
        hOff=0;

    //resize:
    m_rect.setTop(m_rect.top()+yOff);
    m_rect.setLeft(m_rect.left()+xOff);
    m_rect.setWidth(m_rect.width()+wOff);
    m_rect.setHeight(m_rect.height()+hOff);
    m_lastPos = newPos;
    update();
}

void TransparentRect::onMouseReleaseEvent(QMouseEvent *event)
{
    m_areaType=AreaType::None;
	//释放pixmap,
	m_pixmap = QPixmap();



	//重绘,触发paintEvent,否则会有闪动,
	//注意要在hide之前调用。
	repaint();
}

void TransparentRect::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
	painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing | QPainter::Antialiasing);
    painter.setBackground(FrameLessData::transparentRectBkground);
	painter.drawPixmap(0, 0, m_pixmap);
    QPen pen(FrameLessData::borderStyle);
    pen.setWidth(FrameLessData::borderThickness);
    pen.setColor(FrameLessData::borderColor);
    painter.setPen(pen);
	//背景色取反,要求color必须是white
    painter.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination);
    //rect位置相对与this的geometry
    auto rect=m_rect;
    rect.moveTopLeft(rect.topLeft()-geometry().topLeft());

	rect.setWidth(rect.width()*m_scaled);
	rect.setHeight(rect.height()*m_scaled);

    painter.drawRect(rect);
    painter.end();
}
#ifndef TRANSPARENTRECT_H
#define TRANSPARENTRECT_H
#include <QWidget>
#include "framelessData.h"

class TransparentRect : public QWidget
{
    Q_OBJECT
public:
    TransparentRect(QWidget *parent=nullptr);

    void setRect(const QRect &rect);
    QRect getRect() const;
    void onMousePressEvent(QMouseEvent *event,AreaType type);
    void onMoveEvent(QMouseEvent *event,const QPoint &pos);
    void onMouseReleaseEvent(QMouseEvent *event);
protected:
    virtual void paintEvent(QPaintEvent *event) override;
private:

    QRect m_rect;

    QPoint m_lastPos;
    AreaType m_areaType;
    QPixmap m_pixmap;
	double m_scaled=1.0;
	double m_startInch;
};

#endif // TRANSPARENTRECT_H
cmake_minimum_required(VERSION 3.5)

project(frameLessHelper VERSION 0.1 LANGUAGES CXX)


add_definitions(-DTITLEBAR)
#add_definitions(-DRUBBER)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets WebEngineWidgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets WebEngineWidgets)

file(GLOB  PROJECT_SOURCES  "*.cpp" "*.h" "*.ui" "framelessHelper/*.*")

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(frameLessHelper
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET frameLessHelper APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(frameLessHelper SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(frameLessHelper
            ${PROJECT_SOURCES}
            framelessHelper/titleBarImg.qrc
            webview.h webview.cpp



        )
    endif()
endif()

target_link_libraries(frameLessHelper PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::WebEngineWidgets)

set_target_properties(frameLessHelper PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

install(TARGETS frameLessHelper
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(frameLessHelper)
endif()
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QGridLayout>
#include <QWebEngineView>
#include <QFrame>
#include "webview.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_frameHelper(new FrameLessHelper(this))
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    installEventFilter(this);
#ifdef TITLEBAR
    //titleBar
    auto titleBar = m_frameHelper->getTitleBar();
    if(titleBar){
        QGridLayout *layout = new QGridLayout(centralWidget());
        layout->setSpacing(0);
        layout->setContentsMargins(0,0,0,0);
        layout->addWidget(titleBar,0,0,0,0,Qt::AlignTop);
        titleBar->setFixedHeight(32);

#if 0
        WebView *view = new WebView();
        view->load(QUrl("https:www.baidu.com"));
        auto childs =  view->children();
        for(auto child:childs){
//            if(child->metaObject()->className()==QString("QtWebEngineCore::RenderWidgetHostViewQtDelegateWidget"))
                child->installEventFilter(this);
        }
        view->installEventFilter(this);
#else
        QFrame *view = new QFrame();
#endif
        layout->addWidget(view,1,0);
    }

    setMouseTracking(true);
#endif
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    return m_frameHelper->eventFilter(watched,event);
}

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "framelessHelper/framelessHelper.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    bool eventFilter(QObject *watched, QEvent *event) override;
private:
    Ui::MainWindow *ui;
    FrameLessHelper *m_frameHelper;
};
#endif // MAINWINDOW_H

 源码:

https://download.csdn.net/download/weixin_40953784/88248380

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/89133.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Python自带单元测试框架UnitTest,如何生成独立的测试报告?

前言 当我们在公司跑UI自动化的时候&#xff0c;一般都会选择晚上或者工作日休息时进行运行。那么当程序这时运行&#xff0c;如果自动化出现错误&#xff0c;我们又不知道当时页面是什么原因导致测试用例失败&#xff0c;怎么办&#xff1f; 这个时候我们就想到在其测试用例…

搭建CFimagehost私人图床,实现公网远程访问的详细指南

文章目录 1.前言2. CFImagehost网站搭建2.1 CFImagehost下载和安装2.2 CFImagehost网页测试2.3 cpolar的安装和注册 3.本地网页发布3.1 Cpolar临时数据隧道3.2 Cpolar稳定隧道&#xff08;云端设置&#xff09;3.3.Cpolar稳定隧道&#xff08;本地设置&#xff09; 4.公网访问测…

java八股文面试[数据结构]——ConcurrentHashMap原理

HashMap不是线程安全&#xff1a; 在并发环境下&#xff0c;可能会形成环状链表&#xff08;扩容时可能造成&#xff0c;具体原因自行百度google或查看源码分析&#xff09;&#xff0c;导致get操作时&#xff0c;cpu空转&#xff0c;所以&#xff0c;在并发环境中使用HashMap是…

Go 1.21中值得关注的几个变化

美国时间2023年8月8日&#xff0c;Go团队在Go官博上正式发布了1.21版本[2]&#xff01; 早在今年4月末&#xff0c;我就撰写了文章《Go 1.21新特性前瞻[3]》&#xff0c;对Go 1.21可能引入的新特性、新优化和新标准库包做了粗略梳理。 在6月初举办的GopherChina 2023大会上[4]&…

[Docker] Windows 下基于WSL2 安装

Docker 必须部署在 Linux 内核的系统上。如果其他系统想部署 Docker 就必须安装一个虚拟 Linux 环境。 1. 开启虚拟化 进入系统BIOS&#xff08;AMD 为 SVM&#xff1b;Intel 为 Intel-vt&#xff09;改为启用(enable) 2. 开启WSL 系统设置->应用->程序和功能->…

微信小程序,封装身高体重选择器组件

wxml代码&#xff1a; // 微信小程序的插值语法不支持直接使用Math <wxs src"./ruler.wxs" module"math"></wxs> <view class"ruler-container"><scroll-view scroll-left"{{scrollLeft}}" enhanced"{{tru…

中国芯,寻找新赛道迫在眉睫

北京华兴万邦管理咨询有限公司 商瑞 陈皓 近期国内半导体行业的热点可以用两个“有点多”来描述&#xff0c;一个是中国芯群体中上市公司股价闪崩的有点多&#xff0c;另一个是行业和企业的活动有点多。前者说明了许多国内芯片设计企业&#xff08;fabless商业模式&#xff09;…

pandas 将单元格是列表的DataFrame拆成多列

方法&#xff1a; pd.DataFrame(df[col].values.tolist()) 将单元格元素是列表的列拆成多列 如果要与原来的其他列合并 pd.concat([pd.DataFrame(df[col].values.tolist()), df[其他列]], axis1) 示例&#xff1a; points数组如下&#xff1a; 生成DataFrame如下 处理结…

【面试】线上 CPU 100% 问题排查

回答套路一般为&#xff1a;线上服务器没有排查过&#xff0c;线上服务器只有运维才有操作权限。在平时开发的时候&#xff0c;在测试服务器上排查过。 一、复现代码 public class Test {public static void main( String[] args ){int a 0;while (a < 100) {a * 10;}} }…

SpingMVC拦截器-异常处理的思路,用户体验不好的地方

1、异常处理机制 1.1 原先我们的异常都是手动的try..catch() 2、他存在着一些缺陷&#xff1a;这里创建了一个Demo的controller,内部有一个show方法&#xff1a; 3、访问内部&#xff0c;我要实现demoshow方法&#xff0c;我们来调用show1和show2的方法&#xff1a; 4、有一…

SCCB与IIC的异同及FPGA实现的注意事项

文章目录 前言一、信号线二、SCCB数据传输格式三、SCCB写&#xff08;与IIC完全一致&#xff09;四、SCCB读五、SCCB和IIC的区别 前言 IIC接口有比较广泛的应用&#xff0c;而SCCB&#xff08;Serial Camera Control Bus&#xff0c;串行摄像头控制总线&#xff09;是由OV&…

uniapp踩坑合集

1、onPullDownRefresh下拉刷新不生效 pages.json对应的style中enablePullDownRefresh设置为true&#xff0c;开启下拉刷新 {"path" : "pages/list/list","style" :{"navigationBarTitleText": "页面标题名称","enable…

mysql 、sql server 临时表、表变量、

sql server 临时表 、表变量 mysql 临时表 创建临时表 create temporary table 表名 select 字段 [&#xff0c;字段2…&#xff0c;字段n] from 表

时序预测 | MATLAB实现SO-CNN-BiLSTM蛇群算法优化卷积双向长短期记忆神经网络时间序列预测

时序预测 | MATLAB实现SO-CNN-BiLSTM蛇群算法优化卷积双向长短期记忆神经网络时间序列预测 目录 时序预测 | MATLAB实现SO-CNN-BiLSTM蛇群算法优化卷积双向长短期记忆神经网络时间序列预测预测效果基本介绍程序设计参考资料 预测效果 基本介绍 时序预测 | MATLAB实现SO-CNN-BiL…

发布一个开源的新闻api(整理后就开源)

目录 说明: 基础说明 其他说明: 通用接口&#xff1a; 登录: 注册: 更改密码(需要token) 更换头像(需要token) 获取用户列表(需要token): 上传文件(5000端口): 获取文件(5000端口)源码文件&#xff0c;db文件均不能获取: 验证token(需要token): 获取系统时间: 文件…

什么是数据中心IP,优缺点是什么?

如果根据拥有者或者说发送地址来分类的话&#xff0c;可以将代理分为三类&#xff1a;数据中心ip,住宅ip,移动ip 本文我们来了解数据中心ip的原理以及他们的优势劣势&#xff0c;才能选择适合自己的代理。 一、什么是数据中心ip代理&#xff1f; 数据中心ip是由数据中心拥有…

C语言练习2(巩固提升)

C语言练习2 选择题 前言 “志之所趋&#xff0c;无远弗届&#xff0c;穷山距海&#xff0c;不能限也。”对想做爱做的事要敢试敢为&#xff0c;努力从无到有、从小到大&#xff0c;把理想变为现实。要敢于做先锋&#xff0c;而不做过客、当看客&#xff0c;让创新成为青春远航的…

java+springboot+mysql村务档案管理系统

项目介绍&#xff1a; 使用javaspringbootmysql开发的村务档案管理系统&#xff0c;系统包含超级管理员、工作人员角色&#xff0c;功能如下&#xff1a; 超级管理员&#xff1a;系统用户管理&#xff08;工作人员管理&#xff09;&#xff1b;公开资料&#xff1b;会议记录&…

ensp-Ipv6配置配置

ensp-Ipv6配置配置 &#x1f4ce;ipv6.zip&#x1f4ce;Ipv6 网络.docx

什么牌子的运动耳机好、好的运动耳机排行榜

运动与健康息息相关&#xff0c;人们越来越认识到运动带来的益处&#xff0c;例如增强心肺功能、提高新陈代谢率&#xff0c;并能预防多种慢性疾病。随着社交媒体上越来越多人分享自己的运动心得&#xff0c;更多的人也被激发起参与其中的热情。在运动过程中&#xff0c;音乐的…