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:minsizeauto w = size.width();auto h = size.height();auto x = pos.x();auto y = pos.y();//leftif(x<=FrameLessData::borderThickness){if(y<=FrameLessData::borderThickness)return AreaType::LeftTopArea;else if(y>=h-FrameLessData::borderThickness)return AreaType::LeftBottomArea;elsereturn AreaType::LeftArea;}//rightelse if(x>=w-FrameLessData::borderThickness){if(y<=FrameLessData::borderThickness)return AreaType::RightTopArea;else if(y>=h-FrameLessData::borderThickness)return AreaType::RightBottomArea;elsereturn AreaType::RightArea;}else{//centerif(y<=FrameLessData::borderThickness)return AreaType::TopArea;else if(y<=FrameLessData::titleHeight)return AreaType::TitleArea;else if(y>=h-FrameLessData::borderThickness)return AreaType::BottomArea;elsereturn 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:   //moverect.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 titlem_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 clickret = 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;}//触发返回trueif(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();elsem_widget->showFullScreen();
}void FrameLessHelper::onCloseClick()
{m_widget->close();
}
#endif
bool FrameLessHelper::onHoverEvent(QObject *watched,QHoverEvent *event)
{//操作的时候不响应hoverif(m_widgetOperator!=WidgetOperator::None || m_widget->isFullScreen())return false;//hover moveauto areatype = CursorTransform::calculateCursorType(m_widget->size(),event->pos());auto w=static_cast<QWidget *>(watched);if(w)w->setCursor(CursorTransform::AreaType2CursorShape(areatype));elsem_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 TITLEBARm_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){//resizem_widgetOperator = WidgetOperator::Resize;// not start show rectm_transparentRect->setRect(m_widget->frameGeometry());m_transparentRect->onMousePressEvent(event,type);return true;}else if(type==AreaType::TitleArea){//movem_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 TITLEBARif(m_widget->isFullScreen())m_titleBar->onMaxClick();
#endifm_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 TITLEBARQWidget *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 TITLEBARTitleBar *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:   //movem_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必须是whitepainter.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination);//rect位置相对与this的geometryauto 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(frameLessHelperMANUAL_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.qrcwebview.h webview.cpp)endif()
endif()target_link_libraries(frameLessHelper PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::WebEngineWidgets)set_target_properties(frameLessHelper PROPERTIESMACOSX_BUNDLE_GUI_IDENTIFIER my.example.comMACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}MACOSX_BUNDLE TRUEWIN32_EXECUTABLE TRUE
)install(TARGETS frameLessHelperBUNDLE 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//titleBarauto 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 0WebView *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);
#elseQFrame *view = new QFrame();
#endiflayout->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_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic: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

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SQL-Injection

文章目录 引入columns表tables表schemata表以sqli-labs靶场为例路径获取常见方法文件读取函数文件写入函数防注入 数字型注入(post)字符型注入(get)搜索型注入xx型注入 引入 在MYSQL5.0以上版本中&#xff0c;mysql存在一个自带数据库名为information_schema,它是一个存储记录…

YOLOV1

YOU ONLY LOOK ONCE

学习ts(六)数据类型(元组、枚举、Symbol、never)与类型推论

1.元组 元组&#xff08;Tuple&#xff09;是固定数量的不同类型的元素的组合。是数组的变种。 元组与集合的不同之处在于&#xff0c;元组中的元素类型可以是不同的&#xff0c;而且数量固定。元组的好处在于可以把多个元素作为一个单元传递。如果一个方法需要返回多个值&…

生成式人工智能的潜在有害影响与未来之路(三)

产品责任法的潜在适用 背景和风险 产品责任是整个二十世纪发展起来的一个法律领域&#xff0c;旨在应对大规模生产的产品可能对社会造成的伤害。这一法律领域侧重于三个主要危害&#xff1a;设计缺陷的产品、制造缺陷的产品和营销缺陷的产品。产品责任法的特点有两个要素&…

R包开发1:RStudio 与 GitHub建立连接

目录 1.安装Git 2-配置Git&#xff08;只需配置一次&#xff09; 3-用SSH连接GitHub(只需配置一次) 4-创建Github远程仓库 5-克隆仓库到本地 目标&#xff1a;创建的R包&#xff0c;包含Git版本控制&#xff0c;并且能在远程Github仓库同步&#xff0c;相当于发布在Github。…

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

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

图为科技-边缘计算在智慧医疗领域的作用

边缘计算在智慧医疗领域的作用 随着科技的进步&#xff0c;智慧医疗已成为医疗行业的重要发展趋势。边缘计算作为新兴技术&#xff0c;在智慧医疗领域发挥着越来越重要的作用。本文将介绍边缘计算在智慧医疗领域的应用及其优势&#xff0c;并探讨未来发展方向。 一、边缘计算…

matlab使用教程(19)—曲线拟合与一元方程求根

1.多项式曲线拟合 此示例说明如何使用 polyfit 函数将多项式曲线与一组数据点拟合。您可以按照以下语法&#xff0c;使用 polyfit 求出以最小二乘方式与一组数据拟合的多项式的系数 p polyfit(x,y,n), 其中&#xff1a; • x 和 y 是包含数据点的 x 和 y 坐标的向量 …

uniapp 安卓平台签名证书(.keystore)生成

安装JRE环境 下载jre安装包&#xff1a;https://www.oracle.com/java/technologies/downloads/#java8安装jre安装包时&#xff0c;记录安装目录(例:C:\Program Files\Java\jdk-20)打开命令行&#xff08;cmd&#xff09;&#xff0c;将JRE安装路径添加到系统环境变量 d: se…

PostgreSQL基本操作总结

安装按PostgreSQL数据库后&#xff0c;会默认创建用户postgres和数据库postgres&#xff0c;这个用户是超级用户&#xff0c;权限最高&#xff0c;可以创建其他用户和权限&#xff0c;在实际开发过程中&#xff0c;会新创建用户和业务数据库&#xff0c;本文主要介绍用户权限和…

Redis Pub/Sub 指南

Redis 不仅仅是一个数据库&#xff0c;还可以作为支持发布和订阅&#xff08;Pub/Sub&#xff09;操作的消息代理。本文将使用 Navicat for Redis 简要概述 Redis 的 Pub/Sub 功能。 关于发布或订阅消息范式 Pub/Sub 是一种模式&#xff0c;发送者&#xff08;广播者&#xf…

分类预测 | MATLAB实现SCNGO-CNN-LSTM-Attention数据分类预测

分类预测 | MATLAB实现SCNGO-CNN-LSTM-Attention数据分类预测 目录 分类预测 | MATLAB实现SCNGO-CNN-LSTM-Attention数据分类预测分类效果基本描述程序设计参考资料 分类效果 基本描述 1.SCNGO-CNN-LSTM-Attention数据分类预测程序&#xff0c;改进算法&#xff0c;融合正余弦和…

最新ChatGPT网站程序源码+AI系统+详细图文搭建教程/支持GPT4.0/AI绘画/H5端/Prompt知识库

一、前言 SparkAi系统是基于国外很火的ChatGPT进行开发的Ai智能问答系统。本期针对源码系统整体测试下来非常完美&#xff0c;可以说SparkAi是目前国内一款的ChatGPT对接OpenAI软件系统。 那么如何搭建部署AI创作ChatGPT&#xff1f;小编这里写一个详细图文教程吧&#xff01…

集合相关知识

1.为什么使用集合 如果让你统计公司每个月的销售额&#xff0c;你会用数组吗&#xff1f;由于数组创建时需要指定其长度&#xff0c;而且不能改变。集合不需要指定长度&#xff0c;空间不够集合自己会调整。集合里有很多见名知意的方法。 java1.8之后新有的removeIf用法 remove…

MySQL 日志

目录 一、日志概述 二、二进制日志 1、开启二进制日志 2、查看二进制文件 3、删除二进制日志文件 4、恢复二进制日志 5、暂时停止二进制日志功能 三、错误日志 1、启动和设置错误日志 2、查看错误日志 3、删除错误日志 四、通用查询日志 五、慢查询日志 一、日志概…

数组分割(2023省蓝桥杯)n种讨论 JAVA

目录 1、题目描述&#xff1a;2、前言&#xff1a;3、动态规划&#xff08;bug)&#xff1a;3、递归 剪枝&#xff08;超时&#xff09;&#xff1a;4、数学&#xff08;正解&#xff09;&#xff1a; 1、题目描述&#xff1a; 小蓝有一个长度为 N 的数组 A [A0, A1,…, AN−…

【⑭MySQL | 数据类型(二)】字符串 | 二进制类型

前言 ✨欢迎来到小K的MySQL专栏&#xff0c;本节将为大家带来MySQL字符串 | 二进制类型类型的分享✨ 目录 前言5 字符串类型6 二进制类型总结 5 字符串类型 字符串类型用来存储字符串数据&#xff0c;还可以存储图片和声音的二进制数据。字符串可以区分或者不区分大小写的串比…

PDF如何转ppt?PDF转ppt的方法

PDF是一种广泛应用于文档传输和存储的格式&#xff0c;然而&#xff0c;在某些情况下&#xff0c;我们可能需要将PDF文件转换为PPT&#xff0c;以便更加灵活地编辑和展示内容。那么&#xff0c;PDF如何转ppt呢?在本文中&#xff0c;我们将介绍几种常用的方法和工具&#xff0c…

总结:Git 撤销操作

1、还未添加到暂存区&#xff1a;git checkout -- filename 执行命令后&#xff0c;会回退到未修改之前的状态 2、已经添加到暂存区&#xff1a;git reset HEAD filename 执行命令后&#xff0c;会回退到工作区之前的状态 3、已经 commit&#xff0c;但是还未 push git reset…

VLOOKUP

VLOOKUP简单应用 VLOOKUP(A1,B:B,1,FALSE) 是查询A1这子格子的数据在B这一列里面有没有找到相同数据的值,如果有的话就放在当前格子里面去 如果没有的话就是#NA VLOOKUP(A1,F:G,2,FALSE) 是查询A1这子格子的数据在F列查相同的数据,然后再取G列这一行后面的这个格子的数据放到…