Skip to content

Instantly share code, notes, and snippets.

@paulmasri
Last active April 15, 2019 14:40
Show Gist options
  • Select an option

  • Save paulmasri/d9aadda8d249f9d2328bdcccc9a97e7a to your computer and use it in GitHub Desktop.

Select an option

Save paulmasri/d9aadda8d249f9d2328bdcccc9a97e7a to your computer and use it in GitHub Desktop.
QML MouseArea.containsMouse error
/.qmake.cache
/.qmake.stash
*.pro.user
*.pro.user.*
*.qbs.user
*.qbs.user.*
*.moc
moc_*.cpp
qrc_*.cpp
ui_*.h
Makefile*
*-build-*
*.autosave
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
import QtQuick 2.9
Rectangle {
id: root
property string name
signal triggered()
width: 200
height: 100
color: mouseArea.containsMouse? "#cccc33": "#aaccff"
border.width: 1
border.color: "#dddddd"
Text {
id: hoverIndicator
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
text: root.name +
(mouseArea.containsMouse? "\n(hovered)": "") +
(mouseArea.containsPress? "\n(pressed)": "")
}
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
Component.onCompleted: clicked.connect(root.triggered)
}
}
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQml.Models 2.2
Window {
id: root
visible: true
width: 700
height: 800
title: qsTr("QML MouseArea.containsMouse error")
Hoverable {
id: initiatorForCorrect
x: 100
y: 200
name: "Click this to show\ncorrect MouseArea behaviour"
onTriggered: {
visible = false
correctOuter.visible = true
}
}
Hoverable {
id: initiatorForBuggy
x: 400
y: 200
name: "Click this to show\nbuggy MouseArea behaviour"
onTriggered: {
visible = false
buggyOuter.visible = true
}
}
Column {
id: correctOuter
visible: false
x: 100
Text {
id: correctHeading
width: 200
height: 100
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignLeft
wrapMode: Text.WordWrap
text: "This one is correct."
}
Column {
id: correctUpper
Hoverable { name: "Upper A" }
Hoverable { name: "Upper B" }
Hoverable { name: "Upper C" }
}
Column {
id: correctLower
Hoverable { name: "Lower D" }
Hoverable { name: "Lower E" }
Hoverable { name: "Lower F" }
}
}
Column {
id: buggyOuter
visible: false
x: 400
Text {
id: buggyHeading
width: 200
height: 100
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignLeft
wrapMode: Text.WordWrap
text: "This one is buggy.\nIts lower Column's height is dependent on its visibility."
}
Column {
id: buggyUpper
Hoverable { name: "Upper A" }
Hoverable { name: "Upper B" }
Hoverable { name: "Upper C" }
}
Column {
id: buggyLower
height: visible? 300: 0
Hoverable { name: "Lower D" }
Hoverable { name: "Lower E" }
Hoverable { name: "Lower F" }
}
}
Rectangle {
id: correctUpperBorder
visible: correctUpper.visible
x: 100
y: correctUpper.y
width: 200
height: correctUpper.height
color: "transparent"
border.width: 2
border.color: "#8866ff"
}
Rectangle {
id: correctLowerBorder
visible: correctLower.visible
x: 100
y: correctLower.y
width: 200
height: correctLower.height
color: "transparent"
border.width: 2
border.color: "#00ff66"
}
Rectangle {
id: buggyUpperBorder
visible: buggyUpper.visible
x: 400
y: buggyUpper.y
width: 200
height: buggyUpper.height
color: "transparent"
border.width: 2
border.color: "#8866ff"
}
Rectangle {
id: buggyLowerBorder
visible: buggyLower.visible
x: 400
y: buggyLower.y
width: 200
height: buggyLower.height
color: "transparent"
border.width: 2
border.color: "#ff6600"
}
}
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>Hoverable.qml</file>
</qresource>
</RCC>
QT += quick
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment