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. Dynamically set flags of a QAbstractTableModel
Forum Updated to NodeBB v4.3 + New Features

Dynamically set flags of a QAbstractTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.2k 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.
  • V Offline
    V Offline
    valenrw
    wrote on last edited by valenrw
    #1

    Hi, I am writing an application where a QAbstractTableModel is being used with a QTableView to display some data.

    At some point, the user will modify the data that is being displayed, and I would like to disable the row corresponding to the data in the view.

    What I am doing right now is to simply call QAbstractTableModel.dataChanged.emit() when the data is changed somewhere in the program, so that the flags() function (un)sets the flags of the row depending on the data, thus disabling it.

    e.g.:

    class Whatever():
    
        def disable():
           
           my_data_element.enabled = False;
           self.table_model.dataChanged.emit(self.image_model.index(my_data_element.row , 0), #topleft
                                             self.image_model.index(my_data_element.row, 1), #bottomright  
                                             {Qt.DisplayRole, Qt.EditRole, Qt.CheckStateRole})        
             ...
    
    
    class ImageLayerModel(QAbstractTableModel):
    
       def flags(self, index: QModelIndex) -> Qt.ItemFlags:
            #Disabled Row
            if not self.my_data[index.row()].enabled:
                return Qt.NoItemFlags
            ...
    

    This is working as expected but I'm still confused about the correct functionality of the class and want to avoid future problems.
    Is this a safe way to do it, or should I be using the setData() funtion with a UserRole to change my_data_element and set the flag automatically?
    Or is there any better way at all to do this?

    Im a newbie so I appreciate any comments.
    Thanks in advance.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      The disabling should happen in the setData method directly since it is supposed to happen when you change some data.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      V 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi and welcome to devnet,

        The disabling should happen in the setData method directly since it is supposed to happen when you change some data.

        V Offline
        V Offline
        valenrw
        wrote on last edited by
        #3

        @SGaist thanks for your reply. Does this mean that I don't have to implement the flag change in flags(), or only that the initial value that is changed should be changed from the setData() function?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          No, you still have to implement the flag method since the flag values depend on a special event in your application. What I meant is that you want to disable a row when something was edited in it. The edit happens through setData where you modify your data structure so there you have all the information needed to disable the whole row. No need for an "external" call.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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