A strange behavior of using onClicked event handler of MouseArea

In our QML, we usually use context property to get data from C++ by calling a function written by conventional Qt codes. In our use case of popping a message box, we need to specify the box width/height/content in runtime, based on the type of the message box. Our way to do this is to call a C++ function from QML, and dynamically create a QML component in that function, then add this component to the QML tree to let it show up.

In the QML message box, we define some MouseArea in it to handle the mouse click event on different buttons. However, if the message box was created in the onClicked event handler, a strange behavior happened after the component is created: the code of creation will be executed again on one more mouse click, until we press and move the mouse on the message box!

So the situation will be this:

  1. In onClicked event handler, QML calls C++, C++ creates the QML message box. The message box is shown on the screen
  2. Click on any place of the QML message box, another new message box with same type is created again
  3. Press the mouse on the message box but not release it, then move the cursor just like drag it. Release the mouse after this action
After step #3, we found that the MouseArea in the message box can be correctly handled.

This is a very strange behavior and we cannot find any solutions. The work around of it is to use onPressed event handler instead of onClicked.

What a strange thing!

留言

熱門文章