请问python中用pyqt5的QtableView表格填充数据后行号不显示了,问题在哪?
请问python中用pyqt5的QtableView表格填充数据后行号不显示了,问题在哪?
在填充数据前是有行号显示的
填充数据后左边的行号不见了
填充代码是这样的
self.dfA=df self.myModelA =pandasModelA(self.dfA)
self.tableViewA.setModel(self.myModelA)
self.tableViewA.verticalHeader().setVisible(True)#行号 无效
发现可能是QTabWidget导致的
import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import pandas as pd
class Win(QWidget):
def __init__(self): super().__init__()
self.setGeometry(300, 300,500, 400)
self.setWindowTitle('QTableView的使用')
self.tableView = QTableView()
self.tabWidget = QTabWidget()
BuyWidget = QWidget()
dlgLayout = QGridLayout()
dlgLayout.addWidget(self.tableView)
dlgLayout.addWidget(self.tableView, 1, 0,1,13)
BuyWidget.setLayout(dlgLayout)
#self.setLayout(dlgLayout)
self.tabWidget.addTab(BuyWidget, 'A')
self.model = QStandardItemModel(15,15)
cols=['标题'+str(row) for row in range(15)]
self.model.setHorizontalHeaderLabels(cols)
for row in range(9):
for column in range(15):
item = QStandardItem('row%s,column%s'%(row,column))
self.model.setItem(row,column,item)
data=[[j for j in range(15)] for i in range(9)]
df=pd.DataFrame(data,columns=[i for i in range(15)])
#self.tableView.setModel(self.model)
#self.tableView.setModel(self.model)
self.tableView.setModel(pandasModelA(df))
layout = QGridLayout()
layout.addWidget(self.tabWidget,1,0)
self.setLayout(layout)
class pandasModelA(QtCore.QAbstractTableModel):
def __init__(self, data): QtCore.QAbstractTableModel.__init__(self)
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parnet=None):
return self._data.shape[1]
#self.sm.item(0, 0).setForeground(QtGui.QBrush(QtGui.QColor(255, 0, 0)))
#self.sm.item(0, 0).setFont(QtGui.QFont('Times', 10, QtGui.QFont.Black))
def data(self, index, role=Qt.DisplayRole):
if index.isValid():
if role == Qt.DisplayRole:
val=self._data.iloc[index.row(), index.column()]
#return '%.2f' %(self._data.iloc[index.row(), index.column()])
#return [str(val),'%.2f' %val][isinstance(val,float)]
if isinstance(val,float):
return '%.2f' %val
else:
return str(val)
return None
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[col]
return None
if name == "__main__":
app = QApplication(sys.argv)form = Win()
form.show()
sys.exit(app.exec_())
请高手指教
以上是 请问python中用pyqt5的QtableView表格填充数据后行号不显示了,问题在哪? 的全部内容, 来源链接: utcz.com/p/938116.html