我QTableView
从QSqlQueryModel
获取信息并实时显示。问题是,QTableView
允许用户从其中一个字段复制和粘贴信息。
projectModel = QtSql.QSqlQueryModel()
projectModel.setQuery("select * from queue",self.db)
self.total_rows = projectModel.rowCount()
projectModel.setHeaderData(0, QtCore.Qt.Horizontal, 'ID cola')
projectModel.setHeaderData(1, QtCore.Qt.Horizontal, 'Código')
self.projectView = QtGui.QTableView()
self.projectView.setModel(projectModel)
self.projectView.resizeColumnsToContents()
self.projectView.horizontalHeader().setStretchLastSection(True)
例如,如何拒绝复制QTableView的内容并将其粘贴到文本编辑器中?
答案 0 :(得分:1)
您可以将整个表格设为只读:
self.projectView.setEditTriggers(QAbstractItemView.NoEditTriggers)
修改强>:
如果您还想阻止复制单元格,则需要终止相关的键盘快捷键。下面是一些示例代码:
from PySide import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self, rows, columns):
super(Window, self).__init__()
self.table = QtGui.QTableView(self)
model = QtGui.QStandardItemModel(rows, columns, self.table)
for row in range(rows):
for column in range(columns):
item = QtGui.QStandardItem('(%d, %d)' % (row, column))
model.setItem(row, column, item)
self.table.setModel(model)
self.table.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.table)
self.table.installEventFilter(self)
def eventFilter(self, source, event):
if (source is self.table and
event.type() == QtCore.QEvent.KeyPress and
event == QtGui.QKeySequence.Copy):
return True
return super(Window, self).eventFilter(source, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window(5, 5)
window.setGeometry(600, 300, 600, 250)
window.show()
sys.exit(app.exec_())