1

行ごとに N 行と M QTableWidgetItems (チェックボックスとしてのみ使用される) を持つ動的に作成されたテーブルがあります。チェックボックスがオンまたはオフになるたびに、行と列を認識するコードを実行する必要があります。

私の CheckBox サブクラスは次のようになります。

class CheckBox(QTableWidgetItem):
    def __init__(self):
        QTableWidgetItem.__init__(self,1000)
        self.setTextAlignment(Qt.AlignVCenter | Qt.AlignJustify)
        self.setFlags(Qt.ItemFlags(
            Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled ))
def stateChanged(self):
    do_something(self.row(),self.column())
    ...

明らかに、これは SIGNAL('stateChanged(int)')-thingy が発生したときに呼び出される関数を再定義しません。なぜなら、何も起こらないからです。

しかし、もしそうなら:

item = CheckBox()
self.connect(item, SIGNAL('stateChanged(int)'), item.stateChanged)

テーブルを作成するループで、エラーが発生します。

TypeError: arguments did not match any overloaded call:
  QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'CheckBox'
  QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'CheckBox'
  QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'CheckBox

編集:私も再定義を試みましたが、アイテムがチェックされているかチェックされていないときに呼び出されないようsetCheckState()です

編集2:さらに、接続を変更する

self.connect(self.table, SIGNAL('itemClicked(item)'),
               self.table.stateChanged)

どこtable = QTableWidget()でも役に立ちません。

これを正しい方法で行うにはどうすればよいですか?

4

1 に答える 1

2

最も簡単な解決策は、おそらく;のcellChanged(int, int)信号に接続することです。QTableWidget次の例を見てください。

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

#signal handler
def myCellChanged(row, col):
    print row, col

#just a helper function to setup the table
def createCheckItem(table, row, col):
    check = QTableWidgetItem("Test")
    check.setCheckState(Qt.Checked)
    table.setItem(row,col,check)

app = QApplication(sys.argv)

#create the 5x5 table...
table = QTableWidget(5,5)
map(lambda (row,col): createCheckItem(table, row, col),
   [(row, col) for row in range(0, 5) for col in range(0, 5)])
table.show()

#...and connect our signal handler to the cellChanged(int, int) signal
QObject.connect(table, SIGNAL("cellChanged(int, int)"), myCellChanged)
app.exec_()

チェックボックスの 5x5 テーブルを作成します。それらのいずれかがチェック/チェック解除されるたびにmyCellChanged呼び出され、変更されたチェックボックスの行と列を出力します。もちろん、QTableWidget.item(someRow, someColumn).checkState()それがチェックされているかチェックされていないかを確認するために使用できます。

于 2010-09-30T13:03:05.870 に答える