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. (Un)ChechkingAll from Tree View
Forum Updated to NodeBB v4.3 + New Features

(Un)ChechkingAll from Tree View

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
28 Posts 3 Posters 5.0k 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.
  • Karoluss96K Karoluss96

    Now i have in function checkall that:

    checkBoxList= QStandardItem() 
     checkBoxList.setCheckState(Qt.Checked)
    

    no error, but when I unchecked some boxes in TreeView they don't chacked after cliking this button

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #10

    @Karoluss96
    Why would you name a single QStandardItem() as checkBoxList? Won't that just confuse you and me?

    You seem to be creating a QStandardItem into a local variable. I really don't know what you are doing with that item, if anything. You are supposed to be calling setCheckState() on each item in your model, which is what the view is displaying.

    While I notice it, you showed originally:

            for checks in checkBoxList:
                if  checkBoxList.CheckState():
                    checkBoxList.setCheckState(Qt.Checked)
                    print(checkBoxList)
    

    What is the point of a for checks in checkBoxList loop if you only access checkBoxList inside it?

    1 Reply Last reply
    0
    • Karoluss96K Offline
      Karoluss96K Offline
      Karoluss96
      wrote on last edited by Karoluss96
      #11

      I need an access for data in tree View, which is make in a different function.
      I'll show on attached photo: Przechwytywanie.PNG
      is in polish so: "zaznacz wszystko"="check all"
      odznacz wszystko ="un check all".
      Right, I thought that loop will be better, but all times
      but I have an error: " 'QStandardItem' object is not iterable"

      JonBJ 1 Reply Last reply
      0
      • Karoluss96K Karoluss96

        I need an access for data in tree View, which is make in a different function.
        I'll show on attached photo: Przechwytywanie.PNG
        is in polish so: "zaznacz wszystko"="check all"
        odznacz wszystko ="un check all".
        Right, I thought that loop will be better, but all times
        but I have an error: " 'QStandardItem' object is not iterable"

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #12

        @Karoluss96 said in (Un)ChechkingAll from Tree View:

        but I have an error: " 'QStandardItem' object is not iterable"

        Exactly, it is not. If a single QStandardItem, or a single checkbox, is named checkBoxList it's not helpful. There is no list and nothing to loop over.

        I don't know what to say that @SGaist has not stated clearly in:

        My suggestion is that you use that QStandardItemModel directly and go through all the items you want/need to modify their value.

        calling setCheckState(Qt.Checked) on each item.

        1 Reply Last reply
        0
        • Karoluss96K Offline
          Karoluss96K Offline
          Karoluss96
          wrote on last edited by
          #13

          yes, the variable name 'checkBoxList' is confusing, but it doesn't matter for that I need a loop for all Checkboxes as I shown in the print screen above

          JonBJ 1 Reply Last reply
          0
          • Karoluss96K Karoluss96

            yes, the variable name 'checkBoxList' is confusing, but it doesn't matter for that I need a loop for all Checkboxes as I shown in the print screen above

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #14

            @Karoluss96 said in (Un)ChechkingAll from Tree View:

            yes, the variable name 'checkBoxList' is confusing,

            Then change it, and get rid of " " 'QStandardItem' object is not iterable"" error.

            I need a loop for all Checkboxes as I shown in the print screen above

            And I referred you to @SGaist's answer to that, which is what you need to do.

            1 Reply Last reply
            0
            • Karoluss96K Offline
              Karoluss96K Offline
              Karoluss96
              wrote on last edited by
              #15

              @JonB said in (Un)ChechkingAll from Tree View:

              @SGaist can you explain more what do you mean?

              JonBJ 1 Reply Last reply
              0
              • Karoluss96K Karoluss96

                @JonB said in (Un)ChechkingAll from Tree View:

                @SGaist can you explain more what do you mean?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #16

                @Karoluss96

                @SGaist said in (Un)ChechkingAll from Tree View:

                When you want to change them all, go through all the items in your model and call setCheckState on them.

                1. Do you know how to go through all the items/rows in your model? Suppose you wanted to print out (or change) the text of each item, like Kontrola atrybutowe, Kontrola 1, Kontrola 2, etc. How would you do that?

                2. Once you have the loop for 1, presumably you know how to call setCheckState(Qt.Checked) on each item visited.

                1 Reply Last reply
                1
                • Karoluss96K Offline
                  Karoluss96K Offline
                  Karoluss96
                  wrote on last edited by Karoluss96
                  #17
                  1. If put print (QStandardItemModel()) I gain: <PyQt5.QtGui.QStandardItemModel object at 0x00000229EA8F7160>
                    all data in Tree View come from parsed xml (to variable tree) file (I can't publish them), by
                  root = tree.getroot() 
                  

                  and later

                  for grupaKontroli in root:
                              parent = QStandardItem(grupaKontroli.get('name')) 
                              QStandardItemModel.appendRow(parent)
                              parent.setCheckable(True)
                              setCheckState(Qt.Checked)
                  
                  1. Answer for that is in function above

                  This function in the end of main post

                  JonBJ 1 Reply Last reply
                  0
                  • Karoluss96K Karoluss96
                    1. If put print (QStandardItemModel()) I gain: <PyQt5.QtGui.QStandardItemModel object at 0x00000229EA8F7160>
                      all data in Tree View come from parsed xml (to variable tree) file (I can't publish them), by
                    root = tree.getroot() 
                    

                    and later

                    for grupaKontroli in root:
                                parent = QStandardItem(grupaKontroli.get('name')) 
                                QStandardItemModel.appendRow(parent)
                                parent.setCheckable(True)
                                setCheckState(Qt.Checked)
                    
                    1. Answer for that is in function above

                    This function in the end of main post

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #18

                    @Karoluss96
                    No. I'm afraid you do not seem to have a grasp of models/what is going on. You might benefit from reading up on this in the documentation.

                    The answer to, for example, how do I set every checkbox in my model would be:

                    for i in range(model.rowCount()):
                        model.item(i).setCheckState(Qt.Checked)
                    

                    If you want to only check items in and below some one particular node you would need to recurse down its descendants. But you asked for check every item.

                    1 Reply Last reply
                    0
                    • Karoluss96K Offline
                      Karoluss96K Offline
                      Karoluss96
                      wrote on last edited by
                      #19

                      Looks useful, pretty and short, but I still didn't see its results- It need to take data from treeView!

                      JonBJ 1 Reply Last reply
                      0
                      • Karoluss96K Karoluss96

                        Looks useful, pretty and short, but I still didn't see its results- It need to take data from treeView!

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #20

                        @Karoluss96 said in (Un)ChechkingAll from Tree View:

                        It need to take data from treeView!

                        A QTreeView does not store any data. It's a view onto a model. The model is where you store the data. @SGaist has said this, I have said this. I suggested you might like to read up on Qt model/view architecture.

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

                          Please provide a minimal script a minimal script that creates your tree view and its content. We will then be able to reason on it properly to help you achieve your goal.

                          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
                          0
                          • Karoluss96K Offline
                            Karoluss96K Offline
                            Karoluss96
                            wrote on last edited by
                            #22

                            @SGaist said in (Un)ChechkingAll from Tree View:

                            Please provide a minimal script a minimal script that creates your tree view and its content. We will then be able to reason on it properly to help you achieve your goal.

                            OK, so I attached it;

                             def kontrolBDOTread(self): #BDOT is a name of database
                                    path = str(self.controlPath).replace('\\','/') + '/' + self.dlg.comboBox_4.currentText()
                                    
                                    tree = et.parse(path)#parsing XML
                                    root = tree.getroot()   
                                    wersjaSzablonuKontroli = root.get('version')
                                    self.dlg.label_8.setText('Wersja szablonu: '+str(wersjaSzablonuKontroli)
                                    model = QStandardItemModel()
                                    model.setHorizontalHeaderLabels(['Lista kontroli'])#Nagłówek
                                    self.dlg.treeView.setModel(model)
                                    self.dlg.treeView.setUniformRowHeights(True)
                                    
                                    i = 0
                                    for grupaKontroli in root:
                                        parent = QStandardItem(grupaKontroli.get('name')) 
                                        model.appendRow(parent)
                                        parent.setCheckable(True)
                                        parent.setCheckState(Qt.Checked)
                                        parent.setUserTristate(True)
                                        for kontrola in grupaKontroli:
                                            child = QStandardItem(kontrola.get('name'))
                                            child.setCheckable(True)
                                            child.setCheckState(Qt.Checked)
                                            parent.appendRow([child]) 
                                        self.dlg.treeView.setFirstColumnSpanned(i, self.dlg.treeView.rootIndex(), True)
                                        i = i + 1   
                                    self.dlg.treeView.show()
                            
                            1 Reply Last reply
                            0
                            • Karoluss96K Offline
                              Karoluss96K Offline
                              Karoluss96
                              wrote on last edited by
                              #23

                              And how its looks like
                              Przechwytywanie.PNG

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

                                By minimal script, I meant a script that we can run ourselves on our machines which is not possible with what you posted.

                                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
                                0
                                • Karoluss96K Offline
                                  Karoluss96K Offline
                                  Karoluss96
                                  wrote on last edited by
                                  #25

                                  Well, it's a UI plugin. It need to be a part of QGIS program (it's free on download by: https://qgis.org/pl/site/forusers/download.html)

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

                                    The fact that you need to write a plugin for QGIS is currently unrelated to your issue hence why I am requesting a minimal script that creates a QTreeView with the model and the buttons you want. With that we can fix the current issue you have and you can then update your plugin code with that.

                                    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
                                    0
                                    • Karoluss96K Offline
                                      Karoluss96K Offline
                                      Karoluss96
                                      wrote on last edited by
                                      #27

                                      aaaa Ok,
                                      The whole code is too long, as this tab is one of 5 which make this plugin, so I put only that what is here important:

                                       def __init__(self, iface):
                                              """Constructor.
                                      
                                              :param iface: An interface instance that will be passed to this class
                                                  which provides the hook by which you can manipulate the QGIS
                                                  application at run time.
                                              :type iface: QgsInterface
                                              """
                                              # Save reference to the QGIS interface
                                              self.iface = iface
                                              # initialize plugin directory
                                              self.plugin_dir = os.path.dirname(__file__)
                                              # initialize locale
                                              locale = QSettings().value('locale/userLocale')[0:2]
                                              locale_path = os.path.join(
                                                  self.plugin_dir,
                                                  'i18n',
                                                  'SystemBDOT10k{}.qm'.format(locale))
                                      
                                              if os.path.exists(locale_path):
                                                  self.translator = QTranslator()
                                                  self.translator.load(locale_path)
                                                  QCoreApplication.installTranslator(self.translator)
                                                  
                                              # Declare instance attributes
                                              self.actions = []
                                              self.menu = self.tr(u'&SystemBDOT10k')
                                      
                                              # Check if plugin was started the first time in current QGIS session
                                              # Must be set in initGui() to survive plugin reloads
                                              self.first_start = None
                                      
                                          # noinspection PyMethodMayBeStatic
                                          def tr(self, message):
                                              # noinspection PyTypeChecker,PyArgumentList,PyCallByClass
                                              return QCoreApplication.translate('SystemBDOT10k', message)
                                      
                                      
                                          def add_action(
                                              self,
                                              icon_path,
                                              text,
                                              callback,
                                              enabled_flag=True,
                                              add_to_menu=True,
                                              add_to_toolbar=True,
                                              status_tip=None,
                                              whats_this=None,
                                              parent=None):
                                      
                                              icon = QIcon(icon_path)
                                              action = QAction(icon, text, parent)
                                              action.triggered.connect(callback)
                                              action.setEnabled(enabled_flag)
                                      
                                              if status_tip is not None:
                                                  action.setStatusTip(status_tip)
                                      
                                              if whats_this is not None:
                                                  action.setWhatsThis(whats_this)
                                      
                                              if add_to_toolbar:
                                                  # Adds plugin icon to Plugins toolbar
                                                  self.iface.addToolBarIcon(action)
                                      
                                              if add_to_menu:
                                                  self.iface.addPluginToMenu(
                                                      self.menu,
                                                      action)
                                      
                                              self.actions.append(action)
                                      
                                              return action
                                      
                                          def initGui(self):
                                              """Create the menu entries and toolbar icons inside the QGIS GUI."""
                                      
                                              icon_path = ':/plugins/SystemBDOT10k/icon.png'
                                              self.add_action(
                                                  icon_path,
                                                  text=self.tr(u'SystemBDOT10k'),
                                                  callback=self.run,
                                                  parent=self.iface.mainWindow())
                                      
                                              # will be set False in run()
                                              self.first_start = True
                                      
                                      
                                          def unload(self):
                                              """Removes the plugin menu item and icon from QGIS GUI."""
                                              for action in self.actions:
                                                  self.iface.removePluginMenu(
                                                      self.tr(u'&SystemBDOT10k'),
                                                      action)
                                                  self.iface.removeToolBarIcon(action)
                                      
                                      
                                        def run(self):
                                              #sources paths
                                              self.qmlPath = Path(QgsApplication.qgisSettingsDirPath())/Path("python/plugins/SystemBDOT10k/BDOT10k_QML/")
                                              self.mainPath = Path(QgsApplication.qgisSettingsDirPath())/Path("python/plugins/SystemBDOT10k/")
                                              self.controlPath = Path(QgsApplication.qgisSettingsDirPath())/Path("python/plugins/SystemBDOT10k/Kontrole/")
                                              
                                              self.dlg = SystemBDOT10k_dialog()
                                              self.dlg.rejected.connect(self.close)
                                              self.dlg.pushButton_3.clicked.connect(self.CheckAll)
                                              self.dlg.pushButton_4.clicked.connect(self.UncheckAll)
                                      
                                      #download the names of template of controls
                                      filenames = next(walk(self.controlPath), (None, None, []))[2]
                                              for filename in filenames:
                                                  self.dlg.comboBox_4.addItem(filename)
                                              self.dlg.comboBox_4.currentTextChanged.connect(self.kontrolBDOTread)
                                              
                                              self.kontrolBDOTread()
                                      

                                      The function kontrolBDOTread attached above my print screen.

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

                                        Please, a minimal script

                                        import sys
                                        
                                        from QtXxx.QtWidgets import QTableView
                                        # additional imports
                                        
                                        app = QApplication(sys.argv)
                                        
                                        model = QStandardItemModel()
                                        # populate your model
                                        table_view = QTableView()
                                        table_view.setModel(model)
                                        # finish the ui
                                        
                                        sys.exit(app.exec())
                                        

                                        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