Select table 行(只有一行)并编辑单元格中的内容
Select the table row (only one row) and edit the content in cells
使用下面的代码,您可以 add/select 整行并在按下编辑按钮时在屏幕上打印当前行。
此外,之前所有的行都被禁用了! 我将能够在选中时编辑该行,而不是打印当前行。
# -*- 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_())
您可以在项目上调用 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)
使用下面的代码,您可以 add/select 整行并在按下编辑按钮时在屏幕上打印当前行。
此外,之前所有的行都被禁用了! 我将能够在选中时编辑该行,而不是打印当前行。
# -*- 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_())
您可以在项目上调用 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)