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

  • 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 != []:

    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):
        if self.project.tranus_project:
        super(OptionsTRANUSDialog, self).show()
    def select_tranus_folder(self):
        folder = QtGui.QFileDialog.getExistingDirectory(self, "Select directory")
        if folder:
            if not self.project.load_tranus_folder(folder):

    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.


  • Qt Champions 2018

    @EJWA Connect a slot to and call your function there

  • Qt Champions 2018

    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( != + Qt::CheckStateRole).toInt()){
    model->setData(index,,Qt::UserRole + Qt::CheckStateRole);
    model->setData(index,QVariant(),Qt::UserRole + Qt::CheckStateRole);

  • @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.

  • Finally, I found a partial solution to my problem, I used the signal itemChanged

    def activate_launch_button(self):
        model = self.scenarios.model()
    def check_configure(self):

    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 ?

  • Lifetime Qt Champion


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

  • Hello,

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

    I will try to understand.

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

    def activate_launch_button(self):
        model = self.scenarios.model()
    def check_configure(self,item):
            model = self.scenarios.model()
            index = model.indexFromItem(item)
            if != + QtCore.Qt.CheckStateRole):
                if!= QtCore.Qt.Unchecked :
                    model.setData(index,,QtCore.Qt.UserRole + QtCore.Qt.CheckStateRole)
                else :
                    model.setData(index,,QtCore.Qt.UserRole + QtCore.Qt.CheckStateRole)
                    print self.count_check

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.