首页 > 代码库 > tableViewBug
tableViewBug
#!/usr/bin/env python
# -*- coding: utf8 -*-
# some imports :
from __future__ import print_function
import sys
# display infos :
PYTHONVERSION = sys.version_info[0] * 10 + sys.version_info[1]
print(‘PYTHONVERSION :‘, PYTHONVERSION)
# PyQt or PySide ?
PYSIDE = False
if not("PYQT" in sys.argv):
try:
from PySide import QtCore, QtGui
PYSIDE = True
except:
pass
if PYSIDE:
import PySide
print(‘PYSIDE‘, PySide.__version__)
else:
import sip
sip.setapi(‘QString‘, 2)
sip.setapi(‘QVariant‘, 2)
from PyQt4 import QtCore, QtGui
print(‘PYQT‘, QtCore.PYQT_VERSION_STR)
# display Qt version :
QTVERSION = QtCore.qVersion()
print(‘QTVERSION :‘, QTVERSION)
class TestModel(QtGui.QStandardItemModel):
def __init__(self, parent=None):
super(TestModel, self).__init__(parent)
self.headers = [‘id‘, ‘firstname‘, ‘lastname‘]
self.datas = []
self.datas.append([‘101‘, ‘Danny‘, ‘Young‘])
self.datas.append([‘102‘, ‘Christine‘, ‘Holand‘])
self.datas.append([‘103‘, ‘Lars‘, ‘Gordon‘])
self.datas.append([‘104‘, ‘Roberto‘, ‘Robitaille‘])
self.datas.append([‘105‘, ‘Maria‘, ‘Papadopoulos‘])
self.emptyRow = []
nbCols = len(self.headers)
self.setColumnCount(nbCols)
nbRows = len(self.datas)
self.setRowCount(nbRows)
for col in range(nbCols):
self.setHeaderData(col, QtCore.Qt.Horizontal, self.headers[col])
for row in range(nbRows):
for col in range(nbCols):
data = self.datas[row][col]
self.setData(self.index(row, col, QtCore.QModelIndex()), data)
def data(self, index, role=QtCore.Qt.DisplayRole):
column, row = index.column(), index.row()
if role == QtCore.Qt.TextAlignmentRole:
if column > 0:
return QtCore.Qt.AlignLeft
else:
return QtCore.Qt.AlignRight
elif role == QtCore.Qt.DisplayRole:
return self.datas[row][column]
return
def columnCount(self, parent=QtCore.QModelIndex()):
return len(self.headers)
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.datas)
class MainForm(QtGui.QDialog):
def __init__(self, parent=None):
super(MainForm, self).__init__(parent)
self.model = TestModel()
self.tableView = QtGui.QTableView()
self.tableView.setModel(self.model)
insertRowButton = QtGui.QPushButton("InsertRow")
insertRowButton.clicked.connect(self.insertRow)
removeRowBugButton = QtGui.QPushButton("RemoveRowBug")
removeRowBugButton.clicked.connect(self.removeRowBug)
removeRowButton = QtGui.QPushButton("RemoveRow")
removeRowButton.clicked.connect(self.removeRow)
quitButton = QtGui.QPushButton("Quit")
quitButton.clicked.connect(self.accept)
buttonLayout = QtGui.QHBoxLayout()
buttonLayout.addWidget(insertRowButton)
buttonLayout.addWidget(removeRowBugButton)
buttonLayout.addWidget(removeRowButton)
buttonLayout.addStretch()
buttonLayout.addWidget(quitButton)
splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
self.vbox = QtGui.QVBoxLayout()
self.vbox.addWidget(self.tableView)
widget = QtGui.QWidget()
widget.setLayout(self.vbox)
splitter.addWidget(widget)
layout = QtGui.QVBoxLayout()
layout.addWidget(splitter)
layout.addLayout(buttonLayout)
self.setLayout(layout)
self.setMinimumWidth(400)
def insertRow(self):
index = self.tableView.currentIndex()
if index.isValid():
row = index.row()
column = index.column()
else:
row, column = 0, 0
if self.model.insertRow(row):
emptyRow = [‘999‘, ‘AAA‘, ‘Zzzzzzzzzzz‘]
row += 1
self.model.datas.insert(row, emptyRow)
index = self.model.index(row, column)
self.tableView.setFocus()
self.tableView.setCurrentIndex(index)
def removeRowBug(self):
index = self.tableView.currentIndex()
if not index.isValid():
return
row = index.row()
column = index.column()
if (self.model.removeRow(row)):
self.model.datas.pop(row)
if row > self.model.rowCount() - 1:
row -= 1
# QTableView.verticalHeader is not repaint
# this for verify verticalHeader :
print(self.model.rowCount(), self.tableView.verticalHeader().count())
# I test that but nothing is ok :
#self.tableView.resizeRowsToContents()
#self.tableView.verticalHeader().reset()
#self.tableView.verticalHeader().repaint()
QtCore.QCoreApplication.processEvents()
index = self.model.index(row, column)
self.tableView.setFocus()
self.tableView.setCurrentIndex(index)
def removeRow(self):
index = self.tableView.currentIndex()
if not index.isValid():
return
row = index.row()
column = index.column()
if (self.model.removeRow(row)):
self.model.datas.pop(row)
if row > self.model.rowCount() - 1:
row -= 1
# recreate the tableView solve the bug, but is not good :
self.vbox.removeWidget(self.tableView)
self.tableView.setModel(None)
del self.tableView
self.tableView = QtGui.QTableView()
self.tableView.setModel(self.model)
self.vbox.addWidget(self.tableView)
index = self.model.index(row, column)
self.tableView.setFocus()
self.tableView.setCurrentIndex(index)
if __name__ == ‘__main__‘:
app = QtGui.QApplication(sys.argv)
form = MainForm()
form.show()
sys.exit(app.exec_())
tableViewBug