正在发生的事情是,我有MenuItem
来定义背景,它是带有MouseArea
的矩形,可以根据区域是鼠标还是按下来赋予MenuItem
不同的不透明度
我将说明正在发生的事情。
因此,我打开菜单(带有按钮),而没有在第一个选项中使用鼠标箭头的情况:
此示例使用的代码是
main.cpp
#include <QGuiApplication>
#include "dataloopwrapper.h"
#include "gfepanel/firmware.hh"
#include <QIcon>
#include <QtQml>
#include "qmltranslator.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
app.setWindowIcon(QIcon(":/images/iconapp.ico"));
qmlRegisterInterface<GFEPanel::ConnectorFile>("ConnectorFile");
qmlRegisterInterface<GFEPanel::PanelLog>("PanelLog");
qmlRegisterInterface<GFEPanel::PanelLog>("PanelLogEntry");
qmlRegisterInterface<GFEPanel::BootloaderResponse>("BootloaderResponse");
qmlRegisterInterface<GFEPanel::Firmware>("Firmware");
//A QObject singleton type instance returned from a singleton type provider is owned by the QML engine. For this reason, the singleton type provider function should not be implemented as a singleton factory.
qmlRegisterSingletonType<DataloopWrapper>("pt.gfe.connector", 1, 0, "DataloopWrapper",&DataloopWrapper::qmlInstance);
//will register the c++ class type (derived from QObject) as the non-instantiable type with QML type system.
qmlRegisterUncreatableType<GFEPanel::Bootloader>("pt.gfe.connector", 1, 0, "Bootloader", "Can't instantiate Bootloader");
// Add font to project
QFile res(":/fonts/Roboto-Regular.ttf");
QFile res2(":/fonts/Roboto-Bold.ttf");
if(!res.open(QIODevice::ReadOnly))
{
qDebug() << "not able to use roboto regular font";
}
if(!res2.open(QIODevice::ReadOnly))
{
qDebug() << "not able to use roboto bold font";
}
// Create an object to work with translations ...
QQmlApplicationEngine engine;
QmlTranslator qmlTranslator(&engine);
app.installTranslator(qmlTranslator.getTranslator());
// and register it as a context in Qml layer
engine.rootContext()->setContextProperty("qmlTranslator", &qmlTranslator);
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
return app.exec();
}
main.qml
import QtQuick 2.9
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
ApplicationWindow {
id: window
title: "Stack"
visible: true
height: 200
width: 400
Item {
id: page
anchors.fill: parent
width:parent.width
height: parent.height
Button {
id: button
anchors {
top: parent.top
left: parent.left
}
text: "open Menu"
width: 100
height: 30
onClicked: menu.open()
}
Menu {
id: menu
y: 30
MenuItem {
background:
Rectangle {
anchors.fill: parent
color: "#999"
opacity: mouseArea1.pressed ? 1: mouseArea1.containsMouse ? 0.6 : 0.0
MouseArea {
id: mouseArea1
anchors.fill: parent
hoverEnabled: true
}
}
text: "Cut"
}
MenuItem {
background:
Rectangle {
anchors.fill: parent
color: "#999"
opacity: mouseArea2.pressed ? 1: mouseArea2.containsMouse ? 0.6 : 0.0
MouseArea {
id: mouseArea2
anchors.fill: parent
hoverEnabled: true
}
}
text: "Copy"
}
MenuItem {
background:
Rectangle {
anchors.fill: parent
color: "#999"
opacity: mouseArea3.pressed ? 1: mouseArea3.containsMouse ? 0.6 : 0.0
MouseArea {
id: mouseArea3
anchors.fill: parent
hoverEnabled: true
}
}
text: "Paste"
}
}
}
}
可能是问题所在,还是有解决方法?
答案 0 :(得分:0)
我不知道为什么打开菜单时悬停了第一项。
但是,作为一种解决方法,您可以执行以下操作:
onClicked: {
mouseArea1.hoverEnabled = false
menu.open()
mouseArea1.hoverEnabled = true
}