pyqt6如何从单击中获取tableview标题名称/文本
我在PYQT6中有一个GUI应用程序,其中只有一个按钮和表观视图。按下后,按钮连接tablemodel
,该按钮从qtcore.qabstracttablemodel
继承到表视图填充数据填充数据a有些格式化。伟大的。
我现在想要的是能够访问标题名称(只是单击表中的单元格时,只是标题中的文本)。我已经成功连接了一个功能,该功能只能打印出item.Row()
and item.column()
,并且这正常运行。我一生无法获得标题信息。 任何帮助都很棒的
到目前为止,
代码我只是在张贴代码中最相关的部分,因为它很长,并且分布在多个文件上。如果您需要查看更多,请使用LMK。
class PrimaryWindow(QtWidgets.QMainWindow):
"""docstring for PrimaryWindow"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.dbloc=r'test.sqlite'
self.conn=sqlite3.connect(self.dbloc)
#do all the connections
self.ui.DataButton.clicked.connect(self.get_data)
self.ui.DataTable.clicked.connect(self.Get_new_view)
def get_data(self):
#get some data from db
self.tmodel = TableModel(self.data)
self.ui.DataTable.setModel(self.tmodel)
def Get_new_view(self, item):
print('clicked',item.row(),item.column())
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data):
super(TableModel, self).__init__()
self._data = data
def data(self, index, role):
if role == Qt.ItemDataRole.BackgroundRole:
colors={
20:'#da172e', #red
15:'#ede60b', #yellow
10:'#0bed10', #green
99:'#0b6ded', #blue
5: '#FFFFFF' #white
}
value = self._data.iloc[index.row(),index.column()]
if value == 0:
return #don't color 0 values
else:
return QtGui.QColor(colors[max(i for i in colors if i<=value)]) #returns threshold value as key to color dict, sends string of hex color code
if role == Qt.ItemDataRole.DisplayRole:
# See below for the nested-list data structure.
# .row() indexes into the outer list,
# .column() indexes into the sub-list
return str(self._data.iloc[index.row(),index.column()])
def rowCount(self, index):
# The length of the outer list.
return len(self._data)
def columnCount(self, index):
# The following takes the first sub-list, and returns
# the length (only works if all rows are an equal length
return len(self._data.columns)
def headerData(self, section, orientation, role):
# section is the index of the column/row.
if role == Qt.ItemDataRole.DisplayRole:
if orientation == Qt.Orientation.Horizontal:
return str(self._data.columns[section])
if orientation == Qt.Orientation.Vertical:
return str(self._data.index[section])
def flags(self, index):
return Qt.ItemFlag.ItemIsSelectable|Qt.ItemFlag.ItemIsEnabled|Qt.ItemFlag.ItemIsEditable
I've got a gui app in PyQt6 that just has a button and table view in it. Once pressed, the button connects TableModel
, which inherits from QtCore.QAbstractTableModel
, to the table view populates the data a does a bit of formatting. Great.
What I now want is to be able to access the header names (just the text thats in the headers), when a cell in the table is clicked. I've successfully connected a function that just prints out item.row()
and item.column()
and this is functioning correctly. I can't for the life of me get the header info though. Any help would be great
Code so far
I'm just posting the most relevant parts of the code, as it's long and spread over multiple files. Just lmk if there's more you need to see.
class PrimaryWindow(QtWidgets.QMainWindow):
"""docstring for PrimaryWindow"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.dbloc=r'test.sqlite'
self.conn=sqlite3.connect(self.dbloc)
#do all the connections
self.ui.DataButton.clicked.connect(self.get_data)
self.ui.DataTable.clicked.connect(self.Get_new_view)
def get_data(self):
#get some data from db
self.tmodel = TableModel(self.data)
self.ui.DataTable.setModel(self.tmodel)
def Get_new_view(self, item):
print('clicked',item.row(),item.column())
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data):
super(TableModel, self).__init__()
self._data = data
def data(self, index, role):
if role == Qt.ItemDataRole.BackgroundRole:
colors={
20:'#da172e', #red
15:'#ede60b', #yellow
10:'#0bed10', #green
99:'#0b6ded', #blue
5: '#FFFFFF' #white
}
value = self._data.iloc[index.row(),index.column()]
if value == 0:
return #don't color 0 values
else:
return QtGui.QColor(colors[max(i for i in colors if i<=value)]) #returns threshold value as key to color dict, sends string of hex color code
if role == Qt.ItemDataRole.DisplayRole:
# See below for the nested-list data structure.
# .row() indexes into the outer list,
# .column() indexes into the sub-list
return str(self._data.iloc[index.row(),index.column()])
def rowCount(self, index):
# The length of the outer list.
return len(self._data)
def columnCount(self, index):
# The following takes the first sub-list, and returns
# the length (only works if all rows are an equal length
return len(self._data.columns)
def headerData(self, section, orientation, role):
# section is the index of the column/row.
if role == Qt.ItemDataRole.DisplayRole:
if orientation == Qt.Orientation.Horizontal:
return str(self._data.columns[section])
if orientation == Qt.Orientation.Vertical:
return str(self._data.index[section])
def flags(self, index):
return Qt.ItemFlag.ItemIsSelectable|Qt.ItemFlag.ItemIsEnabled|Qt.ItemFlag.ItemIsEditable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论