0

QTableWidgetクリックするたびにマウスクリック座標を追加するにはどうすればよいですか?すでにQMouseEvent座標を表示する必要QLabelItemがありますが、クリックするたびに座標を含む行を追加したいと思います。これは可能ですか?使用する必要があることはわかっていますsetItem()が、これを既存のマウスクリックイベントに関連付けるにはどうすればよいですか?

これが私がマウスクリックのために持っているイベントフィルターです:

   def eventFilter(self, obj, event):
        if obj is self.p1 and event.type() == event.GraphicsSceneMousePress:
            if event.button()==Qt.LeftButton:
                pos=event.scenePos()
                x=((pos.x()*(2.486/96))-1)
                y=(pos.y()*(10.28/512))
                self.label.setText("x=%0.01f,y=%0.01f" %(x,y))
       #here is where I get lost with creating an iterator to append to the table with each click
             for row in range(10):
                for column in range(2):
                    self.coordinates.setItem(row,column,(x,y))
4

2 に答える 2

1

値の 2 列のテーブルがあり、x,yクリックするたびに新しい行を追加するとします。

def eventFilter(self, obj, event):
    if obj is self.p1 and event.type() == event.GraphicsSceneMousePress:
        if event.button() == Qt.LeftButton:
            pos = event.scenePos()
            x = QtGui.QTableWidgetItem(
                '%0.01f' % ((pos.x() * 2.486 / 96) - 1))
            y = QtGui.QTableWidgetItem(
                '%0.01f' % (pos.y() * 10.28 / 512))
            row = self.coordinates.rowCount()
            self.coordinates.insertRow(row)
            self.coordinates.setItem(row, 0, x)
            self.coordinates.setItem(row, 1, y)
于 2012-09-02T18:25:11.983 に答える
1

と仮定するとmodel=QTableView.model()、次のような方法で新しい行をテーブルに追加できます。

nbrows = model.rowCount()
model.beginInsertRows(QModelIndex(),nbrows,nbrows)
item = QStandardItem("({0},{1})".format(x,y))
model.insertRow(nbrows, item.index())
model.endInsertRows()

QTableWidgetがあり、 がない場合はQTableView、同じ MO を使用できます。

  • で新しい行を追加しますself.insertRow(self.rowCount())
  • メソッドを使用して.setItem、最後の行のデータを変更します。たとえばQTableWidgetItem("({0},{1})".format(x,y))、または座標のタプルを表すのに好きな文字列を使用できます。

QTableViewただし、の代わりに s の使用を開始することをお勧めしQTableWidgetます。これにより、はるかに柔軟性が向上します。

于 2012-09-02T14:10:57.340 に答える