gpt4 book ai didi

python - 如何检查复选框是否已选中或未选中? (在 QTreeWidget 中)

转载 作者:太空宇宙 更新时间:2023-11-03 18:24:30 24 4
gpt4 key购买 nike

我编写了以下代码:

from PyQt4 import QtCore, QtGui
import sys

class window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(window, self).__init__(parent)
self.TreeWidget = QtGui.QTreeWidget()
self.TreeWidget.setColumnCount(1)
item1 = QtGui.QTreeWidgetItem(["Item 1"])
item1.setCheckState(0, QtCore.Qt.Checked)
item2 = QtGui.QTreeWidgetItem(["Item 2"])
item2.setCheckState(0, QtCore.Qt.Unchecked)
item3 = QtGui.QTreeWidgetItem(["Item 3"])
item3.setCheckState(0, QtCore.Qt.Unchecked)
self.TreeWidget.addTopLevelItem(item1)
self.TreeWidget.addTopLevelItem(item2)
self.TreeWidget.addTopLevelItem(item3)
self.setCentralWidget(self.TreeWidget)

if item1.checkState(0) == QtCore.Qt.Checked:
print('item 1 is checked')
if item2.checkState(0) == QtCore.Qt.Checked:
print('item 2 is checked')
if item3.checkState(0) == QtCore.Qt.Checked:
print('item 3 is checked')

if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = window()
ui.show()
sys.exit(app.exec_())

通过上面的代码,我成功创建了复选框。但是,当我运行该程序时,我想打印用户检查的项目。据我了解,我需要使用事件信号,但我不确定如何设置信号。我找到了一个显示 MousePressEvent 用法的示例代码,但我不确定如何使用 python 实现该代码。

示例如下:

#include <QtGui>

class TreeWidget : public QTreeWidget
{
public:
TreeWidget()
{
setColumnCount(1);
item1 = new QTreeWidgetItem(this);
item1->setExpanded(true);
item1->setFlags(item1->flags() | Qt::ItemIsUserCheckable);
item1->setCheckState(0, Qt::Checked);
item1->setText(0, "item 1");

item2 = new QTreeWidgetItem(item1);
item2->setFlags(item2->flags() | Qt::ItemIsUserCheckable);
item2->setCheckState(0, Qt::Checked);
item2->setText(0, "item 2");

item3 = new QTreeWidgetItem(this);
item3->setText(0, "item 3");
item3->setFlags(item3->flags() | Qt::ItemIsUserCheckable);
item3->setCheckState(0, Qt::Checked);

}
void mousePressEvent(QMouseEvent *event)
{
QModelIndex indexClicked = indexAt(event->pos());
if(indexClicked.isValid()) {
QRect vrect = visualRect(indexClicked);
int itemIndentation = vrect.x() - visualRect(rootIndex()).x();
QRect rect = QRect(header()->sectionViewportPosition(0) + itemIndentation
, vrect.y(), style()->pixelMetric(QStyle::PM_IndicatorWidth), vrect.height());
if(rect.contains(event->pos())) {
qDebug() << "checkbox clicked";
QTreeWidget::mousePressEvent(event);
return;
} else {
QTreeWidget::mousePressEvent(event);
}}}
private:
QTreeWidgetItem *item1;
QTreeWidgetItem *item2;
QTreeWidgetItem *item3;
QTreeWidgetItem *item4;
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
TreeWidget box;
box.show();
return app.exec();
}

最佳答案

使用itemClicked信号:

class window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(window, self).__init__(parent)
self.TreeWidget = QtGui.QTreeWidget()
self.TreeWidget.setColumnCount(1)
# self.item1/2/3 = .... save reference to the items
# to access them in the callback (check_status)
item1 = self.item1 = QtGui.QTreeWidgetItem(["Item 1"])
item1.setCheckState(0, QtCore.Qt.Checked)
item2 = self.item2 = QtGui.QTreeWidgetItem(["Item 2"])
item2.setCheckState(0, QtCore.Qt.Unchecked)
item3 = self.item3 = QtGui.QTreeWidgetItem(["Item 3"])
item3.setCheckState(0, QtCore.Qt.Unchecked)
self.TreeWidget.addTopLevelItem(item1)
self.TreeWidget.addTopLevelItem(item2)
self.TreeWidget.addTopLevelItem(item3)
self.setCentralWidget(self.TreeWidget)

# connect the itemClicked signal to the callback check_status.
self.TreeWidget.itemClicked.connect(self.check_status)

def check_status(self):
if self.item1.checkState(0) == QtCore.Qt.Checked:
print('item 1 is checked')
if self.item2.checkState(0) == QtCore.Qt.Checked:
print('item 2 is checked')
if self.item3.checkState(0) == QtCore.Qt.Checked:
print('item 3 is checked')

关于python - 如何检查复选框是否已选中或未选中? (在 QTreeWidget 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23477625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com