使用下面的代码,您可以添加/选择整行,按下编辑按钮时,可以打印屏幕上的当前行。
此外,以前所有的行都被禁用了! 我可以在选中时修改该行,而不是打印当前行。
# -*- coding: utf-8 -*-
import sqlite3 as sql
from PyQt4 import QtCore, QtGui
import sys, os
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.tabs = QtGui.QTabWidget(self)
self.general = QtGui.QWidget()
self.tabs.addTab(self.general, "General")
cWidget = QtGui.QWidget(self)
self.setCentralWidget(cWidget)
self.general_table = QtGui.QTableWidget(self.general)
self.setHeader()
add_record = QtGui.QPushButton("Add Record")
edit = QtGui.QPushButton("Edit")
layout = QtGui.QVBoxLayout(cWidget)
layout.addWidget(self.tabs)
layout.addWidget(add_record)
layout.addWidget(edit)
self.connect(add_record, QtCore.SIGNAL('clicked()'), self.addRecord)
self.connect(edit, QtCore.SIGNAL('clicked()'), self.edit)
self.general_table.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
def addRecord(self):
rcount_general = self.general_table.rowCount()
self.general_table.insertRow(rcount_general)
def edit(self):
indexes = self.general_table.selectionModel().selectedRows()
for index in sorted(indexes):
print("Row %d is selected" % index.row())
def setHeader(self):
# General
self.general_table.setRowCount(1)
self.general_table.setColumnCount(3)
self.general_table.setHorizontalHeaderLabels(["A1", "A2", "A3"])
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
您可以在项目上调用setFlags
功能。
item = QtGui.QTableWidgetItem()
item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )
self.your_table.setItem(row, column, item)
item = QtGui.QTableWidgetItem()
item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable |
QtCore.Qt.ItemIsEnabled)
self.your_table.setItem(row, column, item)