Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved Howto set property for delegate

    QML and Qt Quick
    2
    8
    2625
    Loading More Posts
    • 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.
    • A
      Asperamanca last edited by

      I'm a Qt (C++) vet, but new to QML. I'm displaying a simple model in a TableView. Some of my columns use custom delegates, and I'd like to set some properties on them:

      Delegate:

          Component
          {
              id: buttonDelegate
              Item
              {
                  anchors.fill: parent
                  Button
                  {
                      id: actionButton
                      anchors.centerIn: parent
                      width: parent.width * 0.9
                      height: parent.height - 2
                  }
              }
          }
      

      Use of delegate:

              TableViewColumn
              {
                  id: startButtonColumn
                  title: "someTitle"
                  movable: false
                  resizable: false
                  width: tableView.width * 0.15
                  delegate: buttonDelegate
                  // I would like to set the text to the delegate
              }
      

      As per the comment, I'd like to set the text for the button within the delegate from the TableViewColumn, because I use the same delegate for multiple columns (and the button needs different texts).
      Preferably, I'd like to set the text as a top-level property of the delegate, something like:

      buttonDelegate
      {
          buttonText: "SomeText"
      }
      

      I have found a couple of posts on similar topics (e.g. on stackoverflow), but frankly, without the necessary background, I don't understand them, and they don't simply work for me through copy-paste.

      J.Hilk 1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @Asperamanca last edited by

        hi, @Asperamanca

        I haven't jet touch a TableView myself, but I try to explain the principles, as I understand them, on a very simply ListView

        lets asume an array/List of strings as your model:

        ListView{
            model: ["Item1", "Item2", "Item3"]
        }
        

        and a custom delegate that as a text-property you want to set to the correlating item: You can access the current model-item via modelData

        ListView{
            model: ["Item1", "Item2", "Item3"]
        
            delegate: FancyViewItem{
                 text:modelData
           }
        }
        

        A bit more complex example:
        Lets asume you create a QList<QObjects> in a cpp BackEnd and you want to create a Listview based on that List. Also let's asume QObject is a QObject derived class with the Q_PROPERTYs filePath, fileName and createdAt

        ListView{
            model: cppBackEnd.fileModel//fileModel == Q_PROPERTY(QVariant fileModel READ .... WRITE.... NOTIFY....)
        
            delegate: FancyViewItem{
                 textPath: modelData.filePath
                 textFileName: modelData.fileName 
                 timeStamp: modelData.createdAt
           }
        }
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        A 1 Reply Last reply Reply Quote 0
        • A
          Asperamanca @J.Hilk last edited by

          @J.Hilk said in Howto set property for delegate:

          FancyViewItem

          Thanks for your reply.
          Could I re-use FancyViewItem in multiple columns? I want to avoid defining a lengthy delegate multiple times, with 90 % identical definition, and only one or two properties that differ by column.

          J.Hilk 1 Reply Last reply Reply Quote 0
          • J.Hilk
            J.Hilk Moderators @Asperamanca last edited by

            @Asperamanca said in Howto set property for delegate:

            Could I re-use FancyViewItem in multiple columns? I want to avoid defining a lengthy delegate multiple times, with 90 % identical definition, and only one or two properties that differ by column.

            sure, thats what I usually do.

            I create an "external" .qml file with the name FancyViewItem

            //FancyViewItem.qml
            import QtQuick 2.9
            
            Item{
                 property text: "This is a text property"
                 .....
            }
            

            as long as your qml file is in the same directory as the main file you can simply write

            FancyViewItem{
            
            }
            

            to create a new "instance" of that item

            if its in a different directory, you'll have to import that directory

            import "qrc:/qml/FancyViewItemDir"
            

            make sure you use a Capital letter in the beginning of your *.qml file!!!!

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

            Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply Reply Quote 0
            • A
              Asperamanca last edited by

              Thanks again.
              Does the filename have to match the item name? Because that runs counter to the naming conventions I follow. Or is there a way I define the the name 'FancyViewItem' somewhere within the file, and still import and use it?

              J.Hilk 1 Reply Last reply Reply Quote 0
              • J.Hilk
                J.Hilk Moderators @Asperamanca last edited by J.Hilk

                @Asperamanca said in Howto set property for delegate:

                Thanks again.
                Does the filename have to match the item name? Because that runs counter to the naming conventions I follow. Or is there a way I define the the name 'FancyViewItem' somewhere within the file, and still import and use it?

                Ok, here's where my knowledge ends x)

                But, as far as I know:

                • Filename = Itemname (-.qml)

                and

                • you would have to copy&past the definition everytime if you do it inside a single file.

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply Reply Quote 0
                • A
                  Asperamanca last edited by Asperamanca

                  So I solved the initial problem:

                  Delegate in separate file lpad_buttondelegate.qml:

                  Item
                  {
                      anchors.fill: parent
                      Button
                      {
                          id: actionButton
                          anchors.centerIn: parent
                          width: parent.width * 0.9
                          text: buttonText
                      }
                  }
                  

                  Use of delegate:

                          TableViewColumn
                          {
                              id: startButtonColumn
                              title: "SomeTitle"
                              movable: false
                              resizable: false
                              width: tableView.width * 0.15
                              delegate: Loader
                              {
                                  property string buttonText: "Start"
                                  source: "lpad_buttondelegate.qml"
                              }
                          }
                  

                  New problems abound...but not in this thread.

                  J.Hilk 1 Reply Last reply Reply Quote 0
                  • J.Hilk
                    J.Hilk Moderators @Asperamanca last edited by

                    @Asperamanca
                    great, keep on coding! and good luck ;-)

                    Because I noticed increased reminders about this, don't forget to set the topic to solved :)

                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                    Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post