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.rhkb.cn/news/105114.html

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

相关文章

云计算为中小企业带来的 10 大好处

云计算的迅速采用并非巧合。中小型企业 (SMB) 现在有机会摆脱传统 IT 基础设施的限制&#xff0c;享受云提供的众多优势。它的发展使公司能够更智能、更快速、更安全地工作。 因此&#xff0c;如果您发现自己质疑是否需要进行这种转变&#xff0c;请不要害怕&#xff01;让我们…

哲讯科技携手无锡华启动SCM定制化项目,共谋数字化转型之路

无锡华光座椅弹簧有限公司启动SCM定制化项目 近日&#xff0c;无锡华光座椅弹簧有限公司顺利举行了SCM定制化项目的启动会。本次启动会作为该项目实施的重要里程碑&#xff0c;吸引了双方项目组核心成员的共同参与&#xff0c;并见证了项目的正式启动。 无锡华光座椅弹簧有限公…

vue直接使用高德api

第一步&#xff1a;在index.html 引入 <script src"https://webapi.amap.com/maps?v2.0&key你的key"></script>第二步&#xff1a;在你需要地图的时候 放入 <template><div style"width: 200px; height: 200px"><div id&q…

三维模拟推演电子沙盘虚拟数字沙盘开发教程第13课

三维模拟推演电子沙盘虚拟数字沙盘开发教程第13课 该数据库中只提供 成都市火车南站附近的数据请注意&#xff0c;104.0648,30.61658 在SDK中为了方便三方数据的接入&#xff0c;引入了一个用户层接口。主要是完成三方数据的接入&#xff0c;含动态数据&#xff08;如GPS&…

Python豆瓣爬虫(最简洁的豆瓣250爬虫,随机选择电影)

案例背景 电影才是世界艺术&#xff0c;所以我一直想看完豆瓣250&#xff0c;那么就重新拾起我的爬虫知识。 以前刚学爬虫那啥也不会&#xff0c;python语法都没弄清楚&#xff0c;现在不一样了&#xff0c;能用最为简洁的代码写出爬虫250的代码。 代码实现 导入包&#xff…

clickhouse ssb-dbgen数据构造 及 clickhouse-benchmark简单压测

一、 测试数据构造 1. 数据样例 官方文档有给出一批数据样例。优点是比较真实&#xff0c;缺点是太大了&#xff0c;动辄上百G不适合简单小测试 Anonymized Yandex.Metrica DatasetStar Schema BenchmarkWikiStatTerabyte of Click Logs from CriteoAMPLab Big Data Benchma…

Jenkins自动化部署Vue项目

1、新建item&#xff0c;选择 Freestyle project 2、源码管理选择git&#xff0c;输入git仓库地址和授权账号&#xff0c;并指明要部署的分支 3、构建选择 Execute shell&#xff0c;输入vue项目打包命令 命令示例&#xff1a; source /etc/profile node -v npm config set re…

R语言快速生成三线表(1)

R语言的优势在于批量处理&#xff0c;常使用到循环和函数&#xff0c;三线表是科研文章中必备的内容。利用函数实现自动判断数据类型和计算。使用R包&#xff08;table1&#xff09;。 # 创建连续性变量 continuous_var1 <- c(1.2, 2.5, 3.7, 4.8, 5.9) continuous_var2 &l…

workbench连接MySQL8.0错误 bad conversion 外部组件 异常

阿里云搭建MySQL实用的版本是8.0 本地安装的版本是: workbench 6.3 需要升级到&#xff1a; workbench 8.0 https://dev.mysql.com/downloads/workbench/

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

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

微调Llama2自我认知

一、概述 最近在学习了解大模型微调相关的内容&#xff0c;在学习的过程中也遇到了很多问题&#xff0c;所以将自己的学习过程记录下来&#xff0c;希望对大模型微调感兴趣的小伙伴提供一点帮助&#xff0c;本文主要介绍一下如何通过SFT微调Llama2的自我认知&#xff0c;先看一…

什么是网络中的服务质量 (QoS),其相关技术和关键指标有哪些?

QoS&#xff08;Quality of Service&#xff0c;服务质量&#xff09;指一个网络能够利用各种基础技术&#xff0c;为指定的网络通信提供更好的服务能力&#xff0c;是网络的一种安全机制&#xff0c;是用来解决网络延迟和阻塞等问题的一种技术。QoS的保证对于容量有限的网络来…

学习笔记230816---vue项目中使用第三方组件{el-dropdown}如何设置禁止事件功能

问题描述 使用第三方组件elementui&#xff0c;在导航菜单el-menu的el-menu-item中嵌入一个下拉菜框el-dropdown。点击...icon弹出下拉菜单el-dropdown-menu&#xff0c;那么这时会触发事件冒泡&#xff0c;el-menu-item菜单项的点击事件也会触发。 解决方法 阻止事件冒泡&am…

Java【手撕双指针】LeetCode 57. “两数之和“, 图文详解思路分析 + 代码

文章目录 前言一、两数之和1, 题目2, 思路分析3, 代码展示 前言 各位读者好, 我是小陈, 这是我的个人主页, 希望我的专栏能够帮助到你: &#x1f4d5; JavaSE基础: 基础语法, 类和对象, 封装继承多态, 接口, 综合小练习图书管理系统等 &#x1f4d7; Java数据结构: 顺序表, 链表…

c++ day3

#include <iostream>using namespace std; class per {string name;int age;int *p;int *q; public:per(string name,int age,int a,int b){this->name(name);this->ageage;pnew int(a);qnew int(b);*qb;*pa;cout << "有参构造"<<endl;}void…

如何提取视频的音频到手机?这个音频提取方法很简单

提取视频中的音频可以帮助您获得视频的声音部分&#xff0c;而无需观看整个视频。这对于那些只想听视频的声音或想将视频的声音与其他音频内容混合使用的人来说非常方便。此外&#xff0c;提取音频也可以为需要创建音频剪辑或混音的音频制作者提供帮助。那么怎么提取呢&#xf…

Ant Design Vue 日期选择器DatePicker传给后台日期参数格式问题

花了一个下午才解决&#xff0c;官方组件文档里面是没有处理方案说明的。 项目版本&#xff1a;Ant Design Vue 2.0.2 前端部分代码&#xff1a; <template><a-modal:visible"visible":width"windowWidth":height"800":title"tit…

Golang Gorm 一对多关系 关系表创建

一对多关系 我们先从一对多开始多表关系的学习因为一对多的关系生活中到处都是&#xff0c;例如&#xff1a; 老板与员工女神和添狗老师和学生班级与学生用户与文章 在创建的时候先将没有依赖的创建。表名称ID就是外键。外键要和关联的外键的数据类型要保持一致。 package ma…

创建和分析二维桁架和梁结构研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…