Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using MVC's insertRow() with custom subclassed QStandardItem
Forum Updated to NodeBB v4.3 + New Features

Using MVC's insertRow() with custom subclassed QStandardItem

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 370 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
    Sinarch
    wrote on last edited by
    #1

    Greetings!
    I'm making a project using Python 3.9 / PyQt 5. In it, I'm using the model / view approach to display some data in a QTreeView. Currently I'm trying to implement a method for moving items in a tree up and down (without drag and drop, but by clicking specific buttons). For some mysterious reason, there's no dedicated function for it in PyQt, so I'm doing it from scratch, and for that I need the insertRow() function.
    Apparently it takes two arguments: an int for the row it must insert after, and a list of item objects. Thing is, in my project, I'm using custom item class StandardItem subclassing QStandardItem. It worked just fine with methods like appendRow() which use identical syntax, but for some reason, when I do it with insertRow(), it crashes the app with "TypeError: index 0 has type 'StandardItem' but 'QStandardItem' is expected". The fragment of code I'm using it in goes like this - https://pastebin.com/ghnG0zSz Lines 35 and 39 throw this error. What am I doing wrong?

    JonBJ VRoninV 2 Replies Last reply
    0
    • S Sinarch

      Greetings!
      I'm making a project using Python 3.9 / PyQt 5. In it, I'm using the model / view approach to display some data in a QTreeView. Currently I'm trying to implement a method for moving items in a tree up and down (without drag and drop, but by clicking specific buttons). For some mysterious reason, there's no dedicated function for it in PyQt, so I'm doing it from scratch, and for that I need the insertRow() function.
      Apparently it takes two arguments: an int for the row it must insert after, and a list of item objects. Thing is, in my project, I'm using custom item class StandardItem subclassing QStandardItem. It worked just fine with methods like appendRow() which use identical syntax, but for some reason, when I do it with insertRow(), it crashes the app with "TypeError: index 0 has type 'StandardItem' but 'QStandardItem' is expected". The fragment of code I'm using it in goes like this - https://pastebin.com/ghnG0zSz Lines 35 and 39 throw this error. What am I doing wrong?

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

      @Sinarch
      Hello and welcome.

      Start by scrapping (i.e. not using) all your code for deleting/copying/moving items, selection models, etc. Just create a list of some StandardItems and compare insertRow() against appendRow().

      When you get it working: instead of all your copying of items and deleting rows, try QList<QStandardItem *> QStandardItemModel::takeRow(int row) for passing to those two.

      You can see the C++ source code for both of these methods in https://code.woboq.org/qt5/qtbase/src/gui/itemmodels/qstandarditemmodel.cpp.html (where you can click on function calls to follow the code). I can't spot why appendRow() succeeds while insertRow() fails.

      If you are still stuck: the error message implies it does not like the StandardItems in your row, for some reason. If you have to, why not insert an empty row, which hopefully it won't object to, and then set the columns in that row from your data?

      1 Reply Last reply
      0
      • S Sinarch

        Greetings!
        I'm making a project using Python 3.9 / PyQt 5. In it, I'm using the model / view approach to display some data in a QTreeView. Currently I'm trying to implement a method for moving items in a tree up and down (without drag and drop, but by clicking specific buttons). For some mysterious reason, there's no dedicated function for it in PyQt, so I'm doing it from scratch, and for that I need the insertRow() function.
        Apparently it takes two arguments: an int for the row it must insert after, and a list of item objects. Thing is, in my project, I'm using custom item class StandardItem subclassing QStandardItem. It worked just fine with methods like appendRow() which use identical syntax, but for some reason, when I do it with insertRow(), it crashes the app with "TypeError: index 0 has type 'StandardItem' but 'QStandardItem' is expected". The fragment of code I'm using it in goes like this - https://pastebin.com/ghnG0zSz Lines 35 and 39 throw this error. What am I doing wrong?

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        @Sinarch said in Using MVC's insertRow() with custom subclassed QStandardItem:

        Currently I'm trying to implement a method for moving items in a tree up and down (without drag and drop, but by clicking specific buttons). [..] I'm doing it from scratch, and for that I need the insertRow() function.

        I don't get this. What you need is to either reimplement moveRows of the model or (easier to implement if performance is not a bottleneck) just call layoutAboutToBeChanged, do your reordering and then call layoutChanged

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        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