Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. PyQT : How to activate a command link button when at least one element of treeview is checked ?
Forum Update on Monday, May 27th 2025

PyQT : How to activate a command link button when at least one element of treeview is checked ?

Scheduled Pinned Locked Moved Solved Language Bindings
8 Posts 4 Posters 3.4k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 15 Mar 2017, 11:06 last edited by
    #1

    Hello,
    I want to activate command link button if at least one element is checked in a treeview.

    I tried this method but it is not working well :

    def activate_launch_button(self):
    
        model = self.scenarios.model()
        checked_indexes = model.match(model.index(0, 0), QtCore.Qt.CheckStateRole,QtCore.Qt.Checked, -1,QtCore.Qt.MatchExactly | QtCore.Qt.MatchRecursive)
        print checked_indexes
        if checked_indexes != []:
            self.launch_btn.setEnabled(True)
    

    The problem with this method is the button is activated only after closing the window. I don't understand why. For caling this method I put it in two methods :

    def show(self):
        self.project.load()
        if self.project.tranus_project:
            self.tranus_folder.setText(self.project.tranus_project.path)
        self.activate_launch_button()
        self.launch_options_TRANUS()
        super(OptionsTRANUSDialog, self).show()
    
    def select_tranus_folder(self):
    
        folder = QtGui.QFileDialog.getExistingDirectory(self, "Select directory")
        if folder:
            self.tranus_folder.setText(folder)
            if not self.project.load_tranus_folder(folder):
                self.tranus_folder.setText('')
            self.reload_scenarios()
            self.activate_launch_button()
    

    I really want that the command link button should be activated when at least one element of treeview is checked and to be inactive if there is no element checked.

    Thanks.

    J 1 Reply Last reply 15 Mar 2017, 11:51
    0
    • ? A Former User
      15 Mar 2017, 11:06

      Hello,
      I want to activate command link button if at least one element is checked in a treeview.

      I tried this method but it is not working well :

      def activate_launch_button(self):
      
          model = self.scenarios.model()
          checked_indexes = model.match(model.index(0, 0), QtCore.Qt.CheckStateRole,QtCore.Qt.Checked, -1,QtCore.Qt.MatchExactly | QtCore.Qt.MatchRecursive)
          print checked_indexes
          if checked_indexes != []:
              self.launch_btn.setEnabled(True)
      

      The problem with this method is the button is activated only after closing the window. I don't understand why. For caling this method I put it in two methods :

      def show(self):
          self.project.load()
          if self.project.tranus_project:
              self.tranus_folder.setText(self.project.tranus_project.path)
          self.activate_launch_button()
          self.launch_options_TRANUS()
          super(OptionsTRANUSDialog, self).show()
      
      def select_tranus_folder(self):
      
          folder = QtGui.QFileDialog.getExistingDirectory(self, "Select directory")
          if folder:
              self.tranus_folder.setText(folder)
              if not self.project.load_tranus_folder(folder):
                  self.tranus_folder.setText('')
              self.reload_scenarios()
              self.activate_launch_button()
      

      I really want that the command link button should be activated when at least one element of treeview is checked and to be inactive if there is no element checked.

      Thanks.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 15 Mar 2017, 11:51 last edited by
      #2

      @EJWA Connect a slot to http://doc.qt.io/qt-5/qabstractitemview.html#activated and call your function there

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      ? 1 Reply Last reply 16 Mar 2017, 09:08
      0
      • V Offline
        V Offline
        VRonin
        wrote on 15 Mar 2017, 12:00 last edited by
        #3

        I'm awful at Python so I'll just describe what I'd do.

        • add an integer private variable to the class that counts the items checked and initialise to 0 (int countCheck=0;).
        • define a role that will save previous check information (probably something like Qt::UserRole + Qt::CheckStateRole)
        • connect a slot to the dataChanged signal of the model
        • in the slot put something like (sorry if it's C++)
        void dataChangedSlot(const QModelIndex& index){
        if(index.data(Qt::CheckStateRole).toInt() != index.data(Qt::UserRole + Qt::CheckStateRole).toInt()){
        if(index.data(Qt::CheckStateRole).toInt()!=Qt::Unchecked){
        ++countCheck;
        model->setData(index,index.data(Qt::CheckStateRole),Qt::UserRole + Qt::CheckStateRole);
        }
        else{
        --countCheck;
        model->setData(index,QVariant(),Qt::UserRole + Qt::CheckStateRole);
        }
        launch_btn->setEnabled(countCheck>0);
        }
        }
        

        "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
        0
        • J jsulm
          15 Mar 2017, 11:51

          @EJWA Connect a slot to http://doc.qt.io/qt-5/qabstractitemview.html#activated and call your function there

          ? Offline
          ? Offline
          A Former User
          wrote on 16 Mar 2017, 09:08 last edited by
          #4

          @jsulm Hello, thank you for your reply.

          I am sorry but my item is a QStandardItem so I cannot use the method activated of QAbstractItemView.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on 16 Mar 2017, 12:58 last edited by
            #5

            Finally, I found a partial solution to my problem, I used the signal itemChanged http://pyqt.sourceforge.net/Docs/PyQt4/qstandarditemmodel.html#itemChanged

            def activate_launch_button(self):
            
                model = self.scenarios.model()
                model.itemChanged.connect(self.check_configure)
            
            
            def check_configure(self):
                self.launch_btn.setEnabled(True)
            

            It activates the button when there is one element is checked however when I uncheck all, the button doesn't become inactive. How can I solve this issue ?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 16 Mar 2017, 22:51 last edited by
              #6

              Hi,

              Do it like @VRonin suggested. Keep a counter and increment/decrement it when an item is changed.

              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
              • ? Offline
                ? Offline
                A Former User
                wrote on 17 Mar 2017, 08:23 last edited by
                #7

                Hello,

                Sorry, but I dont' understand really C++. If I understand, I must redefine the method setData ??

                I will try to understand.

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on 17 Mar 2017, 10:36 last edited by
                  #8

                  Final solution, it works very well. Thanks to @SGaist and @VRonin :

                  def activate_launch_button(self):
                  
                      model = self.scenarios.model()
                      model.itemChanged.connect(self.check_configure)
                  def check_configure(self,item):
                  
                          model = self.scenarios.model()
                          index = model.indexFromItem(item)
                  
                          if index.data(QtCore.Qt.CheckStateRole) != index.data(QtCore.Qt.UserRole + QtCore.Qt.CheckStateRole):
                  
                              if index.data(QtCore.Qt.CheckStateRole)!= QtCore.Qt.Unchecked :
                  
                                  self.count_check+=1
                                  model.setData(index,index.data(QtCore.Qt.CheckStateRole),QtCore.Qt.UserRole + QtCore.Qt.CheckStateRole)
                  
                              else :
                  
                                  self.count_check-=1
                                  model.setData(index,index.data(QtCore.Qt.CheckStateRole),QtCore.Qt.UserRole + QtCore.Qt.CheckStateRole)
                                  print self.count_check
                  
                              self.launch_btn.setEnabled(self.count_check>0)
                  
                  1 Reply Last reply
                  0

                  3/8

                  15 Mar 2017, 12:00

                  topic:navigator.unread, 5
                  • Login

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