Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] set (access,highlight) ListView currentItem/currentIndex from C++
Forum Updated to NodeBB v4.3 + New Features

[Solved] set (access,highlight) ListView currentItem/currentIndex from C++

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simonbolek
    wrote on last edited by
    #1

    hi:)
    I would like to highlight the currentItem of the ListView from C++ code according to C++ events, in particular a USB controller input (never mind). I have read many forum posts but did not find a suitable solution. Could you help me?

    My list is simple for the start:
    @
    ListView {
    focus: true
    id: categories
    anchors.fill: parent
    model: myModel
    delegate:
    Row {
    id: foo
    width: foo.ListView.view.width; height: 60
    Text {
    id: t
    text: model.display //I set the model in c++ with setContextProperty
    color: foo.ListView.isCurrentItem ? "white" : "black"
    font { family: "Helvetica"; pixelSize: 16; bold: true }
    anchors {
    left: parent.left; leftMargin: 15
    verticalCenter: parent.verticalCenter
    }
    MouseArea{
    anchors.fill: parent
    }
    }

            MouseArea {
                anchors.fill: foo
                onClicked: {
                    foo.ListView.view.currentIndex = index
                    myModel.doSth(t.text)  //works
                }
            }
        }
    
        highlight: Rectangle { color: "green" }
        highlightMoveSpeed: 500
    }
    

    @

    cheers
    simon:)

    cheers
    simon:)

    http://arcom-ivi.de

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      You can use Connections QML item to connect to signals from C++ object (that you can set via setContextProperty as you've set model object).

      1 Reply Last reply
      0
      • M Offline
        M Offline
        minimoog77
        wrote on last edited by
        #3

        Somethink like:

        @Connections {
        target: myModel
        onChangeCurrentIndex: { ... }
        }@

        Of course, your model in C++ should have changeCurrentIndex signal.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          simonbolek
          wrote on last edited by
          #4

          ...I should actually be banned and cast out for such question for not beeing able to look in help accordingly, shouldn't I? ...I still hope i won't be and I am happy that there are people like you patiently answering such questions...

          Of course:
          @
          Connections {
          target: myModel
          onChangeCurrentIndex: {
          console.log("yo")
          categories.currentIndex = idx
          }
          }
          @

          in c++ h/cpp
          @
          signals:
          void changeCurrentIndex(int idx);

          ...

          emit changeCurrentIndex(1);
          @

          this does the job.

          thakns+cheers
          simon:)

          cheers
          simon:)

          http://arcom-ivi.de

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved