无法在TreeView中选择Checkbox

时间:2011-03-21 05:31:58

标签: python qt treeview pyqt pyqt4

我尝试使用复选框创建树视图,但我无法选中复选框。

在标志方法上我把它作为ItemisuserCheckable提到但是仍然无法使它工作......

我在这里遗漏了一些东西,可以选择复选框。

代码片段是:

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class StbTreeView(QAbstractListModel):
    def __init__(self, args, parent=None):
        super(StbTreeView, self).__init__(parent)

        self.args = args
        print self.args

    def rowCount(self, parent):
        return len(self.args)

    def headerData(self, section, orientation, role):
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return QString("Select STB's")

    def flags(self, index):
        row = index.row()
        if row:
            return  Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsSelectable

    def data(self, index, role=Qt.DisplayRole):

        if role == Qt.DisplayRole:
            row = index.row()
            return self.args[row]

        if role == Qt.CheckStateRole:

            row = index.row()
            return QVariant(Qt.Unchecked)

    def setData(self, index, value, role):

        if role == Qt.CheckStateRole:
            if value == Qt.Checked:
                row = index.row()
                selected_stb = self.args[row]
                print 'selected_stb is %s' % selected_stb
                print 'Value is %s' % value
                self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"),index, index)
                return True
            #return QVariant(Qt.Checked)

def main():
    myapp = QApplication(sys.argv)

    data = ['STB1', 'STB2', 'STB3', 'STB4', 'STB5', 'STB6', 'STB7', 'STB8']

    model = StbTreeView(data)
    tree_view = QTreeView()
    tree_view.show()
    tree_view.setModel(model)

    myapp.exec_()

if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:3)

你需要保持某个当前项状态(选中\未选中)并在调用setdata()方法后更改它。您的商品始终未经过检查,因为您始终在data()方法中为它们返回QVariant(Qt.Unchecked)

我已经改变了你的代码,看它对你有用:

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class TestItem():
    def __init__(self, name, checked):
        self.checked = checked
        self.name = name

class StbTreeView(QAbstractListModel):
    def __init__(self, args, parent=None):
        super(StbTreeView, self).__init__(parent)

        self.args = []
        for item_name in args:
            self.args.append(TestItem(item_name, False))

        for item in self.args:
            print item.name

    def rowCount(self, parent):
        return len(self.args)

    def headerData(self, section, orientation, role):
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return QString("Select STB's")

    def flags(self, index):
        return  Qt.ItemIsUserCheckable | Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsEnabled

    def data(self, index, role=Qt.DisplayRole):
        if role == Qt.DisplayRole:
            row = index.row()
            print self.args[row].name
            return self.args[row].name

        if role == Qt.CheckStateRole:
            row = index.row()
            print self.args[row].checked
            if self.args[row].checked == False:
                return QVariant(Qt.Unchecked)
            else:
                return QVariant(Qt.Checked)

    def setData(self, index, value, role):
        if role == Qt.CheckStateRole:
            row = index.row()
            self.args[row].checked = not self.args[row].checked             
        return True

def main():
    myapp = QApplication(sys.argv)

    data = ['STB1', 'STB2', 'STB3', 'STB4', 'STB5', 'STB6', 'STB7', 'STB8']

    model = StbTreeView(data)
    tree_view = QTreeView()
    tree_view.show()
    tree_view.setModel(model)

    myapp.exec_()

if __name__ == '__main__':
    main()

希望这有帮助,尊重

答案 1 :(得分:0)

谢谢它真的对我有用。我的原始要求是在组合框上调用此视图/模型。我试着调用它但它不起作用...我能够在组合框中看到视图但无法选中任何复选框。我尝试了很多可能但没有成功..

对您的代码进行了轻微修改,以便从组合框调用。

修改后的代码是:

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class TestItem():
    def __init__(self, name, checked):
        self.checked = checked
        self.name = name

class StbTreeView(QAbstractListModel):
    def __init__(self, args, parent = None):
        super(StbTreeView, self).__init__(parent)

        self.args = []
        for item_name in args:
            self.args.append(TestItem(item_name, False))

        for item in self.args:
            print item.name

    #print 'Value of self.args is %s' % self.args

    def rowCount(self, parent):
        return len(self.args)

    def headerData(self, section, orientation, role):
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return QString("Select STB's")

    def flags(self, index):
        return  Qt.ItemIsUserCheckable | Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsEnabled

    def data(self, index, role=Qt.DisplayRole):
        if role == Qt.DisplayRole:
            row = index.row()
            print self.args[row].name
            return self.args[row].name

        if role == Qt.CheckStateRole:
            row = index.row()
            print self.args[row].checked
            if self.args[row].checked == False:
                return QVariant(Qt.Unchecked)
            else:
                return QVariant(Qt.Checked)

    def setData(self, index, value, role):
        if role == Qt.CheckStateRole:
            row = index.row()
            self.args[row].checked = not self.args[row].checked             
        return True

class Template(QTreeView):
    def __init__(self, parent=None):
        super(Template, self).__init__(parent)

        self.data = ['STB1', 'STB2', 'STB3', 'STB4', 'STB5', 'STB6', 'STB7', 'STB8']
        self.MainUI()

    def MainUI(self):
        self.model = StbTreeView(self.data)
        self.setModel(self.model)


def main():
    myapp = QApplication(sys.argv)
    temp = Template()
    temp.show()
    myapp.exec_()

if __name__ == '__main__':
    main()

组合框中的代码:

stb_listview = QComboBox()
view = Template()
stb_listview.setView(view)
stb_listview.setModel(view.model)