Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to make QRangeModel in Python PySide6 with pure strings?
Qt 6.11 is out! See what's new in the release blog

How to make QRangeModel in Python PySide6 with pure strings?

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 3 Posters 508 Views
  • 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.
  • J Offline
    J Offline
    jayrickaby
    wrote on last edited by
    #1

    Hello!
    I am trying to use QRangeModel to easily create a many-row, 2-column model based off some data
    However, it really does not like strings. Here are a few examples of outputs.

    table = [
        [1, 2, 3, 4],
        [5, 6, 7, 8],
    ]
    
    m = QRangeModel(table)
    

    This prints 2 rows, via .rowCount() and 4 columns, via .columnCount() as expected.

    table = [
        ["foo", "bar", 3, 4],
        ["hi", "bye", 7, 8],
    ]
    
    m = QRangeModel(table)
    

    Likewise, this prints 2 rows and 4 columns.

    table = [
        ["foo", "bar", "rab", "oof"],
        ["hi", "bye", "eyb", "ih"],
    ]
    
    m = QRangeModel(table)
    

    However, this prints 2 rows and 1 column. However, there's clearly 4 columns here? Getting the data shows that it has created: fbro and hbei as the two items in the one column. It pulled the first character from each string, and created a new one from it.

    How on earth do I make a QRangeModel with data of pure strings??

    JonBJ 1 Reply Last reply
    0
    • Axel SpoerlA Axel Spoerl moved this topic from QML and Qt Quick on
    • J jayrickaby

      Hello!
      I am trying to use QRangeModel to easily create a many-row, 2-column model based off some data
      However, it really does not like strings. Here are a few examples of outputs.

      table = [
          [1, 2, 3, 4],
          [5, 6, 7, 8],
      ]
      
      m = QRangeModel(table)
      

      This prints 2 rows, via .rowCount() and 4 columns, via .columnCount() as expected.

      table = [
          ["foo", "bar", 3, 4],
          ["hi", "bye", 7, 8],
      ]
      
      m = QRangeModel(table)
      

      Likewise, this prints 2 rows and 4 columns.

      table = [
          ["foo", "bar", "rab", "oof"],
          ["hi", "bye", "eyb", "ih"],
      ]
      
      m = QRangeModel(table)
      

      However, this prints 2 rows and 1 column. However, there's clearly 4 columns here? Getting the data shows that it has created: fbro and hbei as the two items in the one column. It pulled the first character from each string, and created a new one from it.

      How on earth do I make a QRangeModel with data of pure strings??

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @jayrickaby
      Testing following code with Qt 6.10.2:

      import sys
      from PySide6.QtCore import QCoreApplication, QRangeModel
      #from PyQt6.QtCore import QCoreApplication, QRangeModel
      
      if __name__ == '__main__':
          app = QCoreApplication(sys.argv)
          table = [
              ["foo", "bar", "rab", "oof"],
              ["hi", "bye", "eyb", "ih"],
              #[1, 2, 3, 4],
              #[5, 6, 7, 8],
          ]
          m = QRangeModel(table)
          print(m.rowCount(), m.columnCount())
          print(m.data(m.index(0, 0)))
          print(m.data(m.index(1, 0)))
          # sys.exit(app.exec_())
      

      Firstly: I do not get your

      Getting the data shows that it has created: fbro and hbei as the two items in the one column. It pulled the first character from each string, and created a new one from it.

      Instead I get a single column with the full list ["foo", "bar", "rab", "oof"] in it. As I would expect. With the number lists instead I do get 4 columns. Maybe something to do with QStringList being a known type/QVariant-convertible while QList<int> is not?

      However, in any case I do not see how your lists-as-elements make any sense for your attempted use of QRangeModel here? Tellingly, if I change to use PyQt6 instead of PySide6 both string-list and number-list cases generated error:

      TypeError: QRangeModel(range: QPyAbstractRange|None, parent: QObject|None = None): argument 1 has unexpected type 'list'
      

      So no code with list-elements is acceptable as a QRangeModel to PyQt, which is actually what I would expect, I think (not quite sure how QRangeModel is supposed to be used).

      The fact that PyQt rejects this attempt while PySide accepts it but then behaves oddly "sounds an alarm bell".

      You may need to explain exactly what you are trying to achieve with these "range-models-of-list-elements".

      It is possible that a new class introduced apparently at Qt 6.11, QRangeModelAdapter Class, may be relevant to you. Googling delivers:

      QRangeModelAdapter is a component introduced as a technology preview in Qt 6.11 designed to simplify how you modify data structures linked to a QRangeModel. It provides a bridge that allows you to interact directly with an underlying sequential data structure (like a Python list or a NumPy array) using standard, convenient methods without manually dealing with boilerplate QAbstractItemModel conventions, QModelIndex tracking, or QVariant wrapping

      Behaviour is all tied up with C++ ranges, I don't know whether that may have a problem from Python.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jayrickaby
        wrote on last edited by
        #3

        With your example, it was printing 2 rows and 1 column for me. Try putting the QRangeModel now into a TableView or TreeView's model. You can see that the data is weirdly smushed into that one column. To be fair, I decided to just start porting my backend to C++, but I will look into QRangeModelAdapter in the future because I hate models and want to deal with them easily while in Python

        JonBJ 1 Reply Last reply
        0
        • J jayrickaby

          With your example, it was printing 2 rows and 1 column for me. Try putting the QRangeModel now into a TableView or TreeView's model. You can see that the data is weirdly smushed into that one column. To be fair, I decided to just start porting my backend to C++, but I will look into QRangeModelAdapter in the future because I hate models and want to deal with them easily while in Python

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @jayrickaby said in How to make QRangeModel in Python PySide6 with pure strings?:

          With your example, it was printing 2 rows and 1 column for me. Try putting the QRangeModel now into a TableView or TreeView's model. You can see that the data is weirdly smushed into that one column.

          I don't think it will be wrong for me as mine reports 4 columns where yours reports 1. Your problem for that is at the model side, for whatever reason. My suggestion is that this is working "oddly" in PySide6 and explicitly rejected in PyQt6, so all bets are off. And we don't know what platform/Qt/PySide/Python version you are using, may be different from mine.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            friedemannkleint
            wrote on last edited by
            #5

            The QRangeModel example shows what currently works. The main use case for QRangeModel for us was to get numpy data into a graphs view conveniently; string tables work do not work (yet). QAbstractTableModel would be better suited for this.

            1 Reply Last reply
            1
            • F Offline
              F Offline
              friedemannkleint
              wrote last edited by
              #6

              Support for string tables has been added in 6.12.

              1 Reply Last reply
              1

              • Login

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