无法选择 TreeView 内的复选框

发布于 2024-10-24 09:25:56 字数 1837 浏览 1 评论 0原文

我尝试创建带有复选框的树视图,但无法选择复选框。

在标志方法上,我将其提到为 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()

I tried creating a treeview with checkboxes but I'm unable to select the checkboxes.

on the flag method I had mentioned it as ItemisuserCheckable but still could not get it working...

am I missing something here to enable the selection of checkboxes.

A snippet of the code is:

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

策马西风 2024-10-31 09:25:56

您需要在某处保存当前项目状态(已选中\未选中),并在调用 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()

希望这有帮助,问候

you need to hold somewhere current item state (checked\unchecked) and change it once setdata() method is called. Your items are always unchecked because you're always returning QVariant(Qt.Unchecked) for them in the data() method.

I've changed a bit your code, see it would work for you:

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()

hope this helps, regards

茶底世界 2024-10-31 09:25:56

谢谢它真的对我有用。我最初的要求是在组合框中调用此视图/模型。我尝试调用它,但它不起作用...我能够看到组合框中的视图,但无法选择任何复选框。我尝试了相当多的可能性,但没有成功。

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

修改后的代码是:

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)

Thanks it really worked for me. My Original requirement was to call this view/model on a combo box. I tried calling this but it did not work ... I'm able to see the view inside the combo box but unable to select any of the check boxes. I tried quite a few possibility but did not succeed..

Did a slight modification on your code to call from combo box.

the modified code is:

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()

The code from combo box:

stb_listview = QComboBox()
view = Template()
stb_listview.setView(view)
stb_listview.setModel(view.model)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文