From widgets to quick2
-
mousePressEvent() is not an "invokable method":http://qt-project.org/doc/qt-5/qobject.html#Q_INVOKABLE, so it is not accessible from QML.
The MouseArea is a separate object from the ViewportWidget, so it's not connected to ViewportWidget::mousePressEvent().
@
MouseArea { anchors.fill: parent }
@
The code above says: "Create a new (invisible) MouseArea object, and stretch it to cover the parent object". Mouse events are received by the MouseArea object, NOT the parent object.I'm purely guessing here, but try this:
Remove the MouseArea completely
Use qDebug() to print a message in your C++ implementation of ViewportWidget::mousePressEvent()
Build your application, and click on the QML-rendered ViewportWidget
-
I think I follow your line of thought but it didn't work. I also tried declaring,
@
protected:
void keyPressEvent(QKeyEvent* event);
Q_INVOKABLE void mousePressEvent(QMouseEvent* event);
@What puzzles me is that if I add at the same level as the MouseArea,
@
TextInput {
id: kbdinput
anchors.fill: parent
focus: true
}
@a text string entered anywhere in the viewport does get passed to keyPressEvent(...)
-
Does your ViewportWidget call setAcceptedMouseButtons()? It is needed for mousePressEvent to be called.
For keypressEvent your item needs to have active focus, or a child item must have active focus (and not accept the event).
See also "keyboard focus in QtQuick":http://qt-project.org/doc/qt-5/qtquick-input-focus.html
-
Hi Torgier
In the QML MouseArea I have
@
acceptedButtons: Qt.AllButtons
@And the problem remains,
keyPressEvents work; mousePressEvents do not. -
[quote author="zing0000" date="1388768258"]
In the QML MouseArea I have
@
acceptedButtons: Qt.AllButtons
@
[/quote]Please try adding that to the ViewportWidget instead of the MouseArea.
-
Just tried it, but same result.
Do you have any thoughts about the experimental code line,
@viewportwidget.mousePressEvent(mouse)
@My understanding is that this should call the method directly, and it seems to try, but with the error message,
@
hello
qrc:/qml/MainPanelLandscape.qml:45: Error: Unknown method parameter type: QMouseEvent*
@
But the QML property "mouse" is of type MouseEvent, and this does not match the method argument QMouseEvent*. -
Sorry for the confusion. My response was to this problem:
[quote author="zing0000" date="1388688947"]That would be a good future step. But right now I am blocked because I cannot get simple mouse events to propagate to my handling class (below).
[/quote]If you call setAcceptedMouseButtons() in the constructor of ViewportWidget the signal handler in ViewportWidget should be called. That worked for me.
Then you don't need any MouseArea nor do you need to declare Q_INVOKABLE for the mousePressEvent().
All you need then in QML is:
@ViewportWidget {
id: viewportwidget
width: 600; height: 600
@On the other hand if you want to handle it with a MouseArea and forward the events, that is possible too. However you can't forward the 'mouse' parameter as it is of type QQuickMouseEvent* and your mousePressEvent() handler needs QMouseEvent*.
You could forward mouse.x and mouse.y to a Q_INVOKABLE that takes two int parameters for example.
-
Thank you!
The problem was that I misunderstood Qt::AllButtons in the MouseArea - which was correctly consuming all mouse events instead of my intention of passing them on.
With Qt::NoButtons it now works.But in any case, you are right, the MouseArea is not needed.
However, I am also trying to pass keystroke events, and I am still looking for a way of passing events for individual characters.
Thanks again for nudging me in the right direction.
-
[quote author="zing0000" date="1388885058"]
However, I am also trying to pass keystroke events, and I am still looking for a way of passing events for individual characters.
[/quote]To receive key press and release events your ViewportWidget item needs to have active focus. It may be enough to set
@focus: true@
on it, or you may have to call forceActiveFocus() on it.
When the item has active focus, the event handlers keyPressEvent(QKeyEvent *) and keyReleaseEvent(QKeyEvent *) will be called for individual key presses.
-
Yes, it needed both.
my ViewportWidget is part of a ScrollView, so I had to set focus on that.
and it did need forceActiveFocus() in the ViewportWidget constructor.
Clearly I need to read up a bit more about all this....