1

160行のテーブルを作成しようとしていて、QCheckBox奇数行ごとに、特に列10に挿入しています。問題は、80行を作成する必要がQCheckBoxあることです(各行に1つずつ、ユーザー)...

QCheckBox私がしなければならない 9 つのプロジェクト用に80 個のオブジェクトを 1 つずつ作成するのはナンセンスです。

ループでそれを行う方法はありますか?何も考えられず、答えを探しても何も見つかりませんでした。

[...]
# importing PySide
from PySide import QtGui, QtCore
[...]
# Creating a Table
class Table(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Table, self).__init__(parent)
        self.table = QtGui.QTableWidget()
        self.table.setRowCount(160)
        self.table.setColumnCount(10)
# This is the tricky part:
        chkBoxItem = QtGui.QTableWidgetItem()
        chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled)
        chkBoxItem.setCheckState(QtCore.Qt.Unchecked)

        chkBoxItem2 = QtGui.QTableWidgetItem()
        chkBoxItem2.setFlags(QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled)
        chkBoxItem2.setCheckState(QtCore.Qt.Unchecked)

        chkBoxItem3 = QtGui.QTableWidgetItem()
        chkBoxItem3.setFlags(QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled)
        chkBoxItem3.setCheckState(QtCore.Qt.Unchecked)
[...]
# Then insert all of them in the Table:
        self.table.setItem(0, 10, chkBoxItem)
        self.table.setItem(2, 10, chkBoxItem2)
        self.table.setItem(4, 10, chkBoxItem3)
        self.table.setItem(6, 10, chkBoxItem4)
        self.table.setItem(8, 10, chkBoxItem5)
        self.table.setItem(10, 10, chkBoxItem6)
        self.table.setItem(12, 10, chkBoxItem7)
[...]
4

1 に答える 1

1

この基本的なスクリプトは、160*10 の QTable と QPushButton を含む UI を作成します。奇数行ごとに、10 列目のセルにチェックボックスが追加されます。ボタンをクリックすると、すべてのチェックボックスの状態のリストが表示されます。

:

  • 0: チェックなし
  • 2: チェックあり
  • 状態 1 がありますが、何に使用されているか覚えていません。ドキュメントを確認します。

ノート:

これは PyQt を使用して作成されました

コード:

import math, sys
from PyQt4.QtCore import Qt, QTimer
from PyQt4.QtGui import *

class MainWindow(QMainWindow):
    def __init__(self, parent = None):
        QMainWindow.__init__(self, parent)

        #Create Basic UI
        self.mainWidget = QWidget(self)
        self.table = QTableWidget()
        self.table.setRowCount(160)
        self.table.setColumnCount(10)

        self.button = QPushButton("Print stuff")

        layout = QVBoxLayout(self.mainWidget)
        layout.addWidget(self.table)
        layout.addWidget(self.button)

        self.setCentralWidget(self.mainWidget)

        self.button.clicked.connect(self.printStuff)
        #################

        #Fill the table 
        self.rowRange = range(0, self.table.rowCount(), 2)
        for i in self.rowRange:
            chkBoxItem = QTableWidgetItem()
            chkBoxItem.setFlags(Qt.ItemIsUserCheckable|Qt.ItemIsEnabled)
            chkBoxItem.setCheckState(Qt.Unchecked)
            self.table.setItem(i, 9, chkBoxItem)
        ###############

    def printStuff(self): #You can remove this, this is for testing purpose only
        print [(i+1, self.table.item(i, 9).checkState()) for i in self.rowRange]

if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
于 2015-05-29T16:14:17.017 に答える