Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

PySide2 - Checkbox in TreeView



  • Hiii ;)

    I need to create a "if" based in boolean of checkbox "option 1". The following code is my program, someone can help me to create the condition?

    1e380bf2-10e0-4cc1-a183-bece2554cc5e-image.png

    import sys
    from PySide2 import QtCore, QtGui, QtWidgets
    from option import Ui_MainWindow
    from PySide2.QtCore import (QCoreApplication, QDate, QDateTime, QMetaObject,
        QObject, QPoint, QRect, QSize, QTime, QUrl, Qt)
    from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
        QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter,
        QPixmap, QRadialGradient)
    from PySide2.QtWidgets import *
    
    
    
    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUi(self)
    
            ####### here example ###########
    
            #if checkboxtree_example == True:
            #    print ('test')
    
            #################################
            
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            if not MainWindow.objectName():
                MainWindow.setObjectName(u"MainWindow")
            MainWindow.resize(805, 601)
            self.centralwidget = QWidget(MainWindow)
            self.centralwidget.setObjectName(u"centralwidget")
            self.treeWidget = QTreeWidget(self.centralwidget)
            __qtreewidgetitem = QTreeWidgetItem(self.treeWidget)
            __qtreewidgetitem.setCheckState(0, Qt.Unchecked);
            self.treeWidget.setObjectName(u"treeWidget")
            self.treeWidget.setGeometry(QRect(90, 120, 256, 192))
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QMenuBar(MainWindow)
            self.menubar.setObjectName(u"menubar")
            self.menubar.setGeometry(QRect(0, 0, 805, 21))
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QStatusBar(MainWindow)
            self.statusbar.setObjectName(u"statusbar")
            MainWindow.setStatusBar(self.statusbar)
            self.retranslateUi(MainWindow)
            QMetaObject.connectSlotsByName(MainWindow)
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())
    
    

  • Qt Champions 2019

    @Julie-Db said in PySide2 - Checkbox in TreeView:

    I need to create a "if" based in boolean of checkbox "option 1"

    This is not clear to me. Can you please explain more clearly what you want to do?



  • @Julie-Db
    Guessing what you might be asking. You set that checkbox via

    __qtreewidgetitem.setCheckState(0, Qt.Unchecked);
    

    If the user checks it as per your pic you can read the state via

    isChecked = (__qtreewidgetitem.checkState(0) == Qt.Checked)
    


  • @JonB said in PySide2 - Checkbox in TreeView:

    __qtreewidgetitem.setCheckState(0, Qt.Unchecked);

    thank youu <3 :)


Log in to reply