您的位置:首页 > 游戏 > 游戏 > 北京今天最新新闻_搜狗搜索引擎优化指南_郑州官网网站推广优化公司_著名的个人网站

北京今天最新新闻_搜狗搜索引擎优化指南_郑州官网网站推广优化公司_著名的个人网站

2024/12/22 17:31:49 来源:https://blog.csdn.net/Wite_Chen/article/details/143161125  浏览:    关键词:北京今天最新新闻_搜狗搜索引擎优化指南_郑州官网网站推广优化公司_著名的个人网站
北京今天最新新闻_搜狗搜索引擎优化指南_郑州官网网站推广优化公司_著名的个人网站

背景

qt5.15.2自带的MenuItem设置图标,不生效(坑爹)

实现

main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import Qt.labs.platform 1.1Window {id: rootwidth: 640height: 480minimumWidth: 640maximumWidth: 640minimumHeight: 480maximumHeight: 480visible: truetitle: qsTr("TrayDemo")property var menuTray: nullflags: Qt.FramelessWindowsHint | Qt.Window | Qt.CustomizeWindowHint | Qt.WindowMinimizeButtonHintSystemTrayIcon {id: trayAppicon.source: "qrc:/tray.ico"visible: truetooltip: "TrayMenu"onActivated: {if(SystemTrayIcon.Trigger == reason)raiseWindow()else if(SystemTrayIcon.Context == reason) {if(null == menuTray) {var component = Qt.createComponent("qrc:/TrayMenu.qml");if(component.status == Component.Ready) {menuTray = component.createObject(null)}}var jsonPos = qmlHelper.getMousePos()menuTray.x = jsonPos.xmenuTray.y = jsonPos.y - menuTray.height - 10menuTray.showMenu()}}}function raiseWindow() {root.visible = trueroot.raise()root.requestActivate()}
}

TrayMenu.qml

import QtQuick.Window 2.15
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtGraphicalEffects 1.15Window {id: rootheight: 125width: 180minimumWidth: 180maximumWidth: 180minimumHeight: 125maximumHeight: 125property int radiusWnd: 8property int marginSpace: 5property int marginWnd: 10property int heightItem: 32signal trayMenuClick(var nType)enum TrayMenuType{ShowWindow,About,Exit}visible: falsecolor: "white"//flags: Qt.FramelessWindowsHint | Qt.Tool | Qt.CustomizeWindowHint | Qt.WindowMinimizeButtonHintflags: Qt.FramelessWindowsHint | Qt.Tool | Qt.CustomizeWindowHint | Qt.WindowMinimizeButtonHintRectangle{id:basez: 1radius: root.radiusWnd//antialiasing:trueanchors.fill: parentclip: truecolor: "transparent"//color: "#00000000"layer.enabled: truelayer.effect: OpacityMask{maskSource: Rectangle {width: base.widthheight: base.heightradius: base.radius}}Rectangle{radius: radiusWndanchors {left: parent.leftleftMargin: 0right: parent.rightrightMargin: 0top: parent.topbottom: parent.bottom}ListView {id: viewContentmodel: modelContentdelegate: delegateContentclip: truecontentWidth: parent.widthanchors {left: parent.leftleftMargin: 0right: parent.rightrightMargin: 0top: parent.toptopMargin: 0bottom: parent.bottombottomMargin: marginWnd}}// model for contentListModel {id: modelContent}// delegate for contentComponent {id: delegateContentRectangle {width: root.widthheight: heightItemImage {id: imgIconsource: strIconwidth: 16height: 16anchors {left: parent.leftleftMargin: 10verticalCenter: parent.verticalCenter}}Text {id: textDatatext: textMenuanchors {left: imgIcon.rightleftMargin: 5right: parent.rightverticalCenter: parent.verticalCenter}color: "#3B3B3B"font.pointSize: 8}MouseArea {anchors.fill: parentcursorShape: Qt.PointingHandCursorhoverEnabled: trueonEntered: {parent.color = "lightblue"//"#F2F2F2"}onExited: {parent.color = "white"}onClicked: {trayMenuClick(type)hideWindow()}}}}}}Connections {target: eventFilterfunction onFocusOut() {if(!root.visible) returnconsole.log("Focus out..")var jsonPos = qmlHelper.getMousePos()var x = jsonPos.xvar y =jsonPos.yif((x < root.x || x > root.x + root.height)|| (y < root.y || y > root.y + root.height)) {//console.log("Hide window, x: " + x + ", rootX: " + root.x + ", y: " + y + ", rootY: " + root.y + ", visible: " + root.visible)hideWindow()}}}function showMenu(){modelContent.clear()modelContent.append({type: TrayMenu.ShowWindow, strIcon: "",textMenu: "显示主窗口"})modelContent.append({type: TrayMenu.About, strIcon: "qrc:/tray.ico", textMenu: "关于"})modelContent.append({type: TrayMenu.Exit, strIcon: "qrc:/tray.ico", textMenu: "退出"})var currentFlags = root.flagsroot.flags = currentFlags | Qt.WindowStaysOnTopHintroot.requestActivate()root.show()}function hideWindow() {var currentFlags = root.flagsroot.flags = currentFlags & ~Qt.WindowStaysOnTopHintroot.visible = falseroot.hide()}
}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "QmlHelper.h"
#include "CustomObject.h"int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endifQCoreApplication::setAttribute(Qt::AA_Use96Dpi);QGuiApplication app(argc, argv);app.installEventFilter(&EVENT_FILTER_INST);QQmlApplicationEngine engine;engine.rootContext()->setContextProperty("qmlHelper", &QML_CTRL_INST);engine.rootContext()->setContextProperty("eventFilter", &EVENT_FILTER_INST);const QUrl url(QStringLiteral("qrc:/main.qml"));QObject::connect(&engine,&QQmlApplicationEngine::objectCreated,&app,[url](QObject *obj, const QUrl &objUrl) {if (!obj && url == objUrl)QCoreApplication::exit(-1);},Qt::QueuedConnection);engine.load(url);return app.exec();
}

鼠标坐标获取

QVariant QmlHelper::getMousePos()
{QPoint screen_pos = QCursor::pos();QJsonObject jsonObj;jsonObj.insert("x", screen_pos.x());jsonObj.insert("y", screen_pos.y());return jsonObj;
}

存在问题

1、无边框顶部会有点间距
2、弹出的菜单,首次无法点击外部关闭(需要点击主窗口或点击菜单);再次弹出就不会出现此问题

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com