1

I have ImageButton.qml which should change image when user holds the button.

import QtQuick 2.0

Image {
    id: svg

    property string idleImage
    property string hoverImage

    signal clicked

    state: "idle"

    source: state === "idle" ? idleImage : hoverImage

    onSourceChanged: {
        console.log("source = " + source)
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton

        onPressedChanged: {
            svg.state = pressed ? "hover" : "idle"
        }

        onClicked: svg.clicked()
    }
}

But the image is not changed immediately. It's changed only when I hold the button for several seconds. When I press and release mouse button immediately I never see the hover image. onSourceChanged is executed immediately and outputs to console the right image source. This strange bug happens only when I use QQuickWidget. When I don't use widgets, but qml only everything works as expected.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Semyon Tikhonenko
  • 3,872
  • 6
  • 36
  • 61
  • You have to define `states` as well as `state`. having `state` only have no sense. btw definition of signal in your code is incorrect too. – folibis May 31 '18 at 14:04
  • I tried to define states. It doesn't help. It happens only when the moise button is down, when i release the button the image is updated. – Semyon Tikhonenko May 31 '18 at 14:05
  • I even tried to define a repeating timer, which toggles image. It works well, but when i move cursor to the item and press mouse button, the timer stops working for several seconds. Its strange cause i removed mouse area in this experiment. – Semyon Tikhonenko May 31 '18 at 14:08
  • And I also tried to call update from cpp bridge after state is changed, and it doesn't help too. – Semyon Tikhonenko May 31 '18 at 14:12
  • Can you please post your entire code? With working and not working example. Your example works fine for me. There is no problem with your signal declaration or using `state` (even if `state` is indeed usually meant to be used with `states` and `PropertyChanges`). – augre May 31 '18 at 16:23
  • @augre Does it work for you when you put qml inside QQuickWidget? – Semyon Tikhonenko May 31 '18 at 16:24
  • Yes. Maybe I can help but I need to see your code. – augre May 31 '18 at 16:44

1 Answers1

0

I found the issue. I am using QOpenGLWidget in the MainWindow. And I called update method in paintGL.

void Workspace::paintGL() {
    QOpenGLWidget::paintGL();
    workspaceDrawer.draw();
    update();
}

I replaced update call with a timer, running every 1000 / 60 milliseconds. And the issue was gone. It's strange that it was reproduced only when a mouse button was pressed, otherwise everything was updated correctly.

Semyon Tikhonenko
  • 3,872
  • 6
  • 36
  • 61