随函附上:
import sys, logging, datetime
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.Qt import QVBoxLayout
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.__class__.instance = self
self.resize(1200, 1600) # w, h
main_splitter = QtWidgets.QSplitter(self)
main_splitter.setOrientation(QtCore.Qt.Vertical)
self.setCentralWidget(main_splitter)
self.top_frame = QtWidgets.QFrame()
main_splitter.addWidget(self.top_frame)
self.bottom_frame = BottomFrame()
self.bottom_frame.setMaximumHeight(350)
self.bottom_frame.setMinimumHeight(100)
main_splitter.addWidget(self.bottom_frame)
main_splitter.setCollapsible(1, False)
self.bottom_frame.construct()
class BottomFrame(QtWidgets.QFrame):
def construct(self):
layout = QtWidgets.QVBoxLayout(self)
# without this you get the default 10 px border all round the table: too much
layout.setContentsMargins(1, 1, 1, 1)
self.setLayout(layout)
self.messages_table = LogTableView()
layout.addWidget(self.messages_table)
self.messages_table.visual_log( hello world )
self.messages_table.visual_log( message 2 qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd )
self.messages_table.visual_log( message 3 )
self.messages_table.visual_log( message 4 , logging.ERROR)
self.messages_table.visual_log( message 5 )
self.messages_table.visual_log( message 6 qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd qunaomdd )
self.messages_table.visual_log( message 7 )
class LogTableView(QtWidgets.QTableView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setModel(QtGui.QStandardItemModel())
self.horizontalHeader().setStretchLastSection(True)
self.horizontalHeader().hide()
self.setVerticalHeader(VerticalHeader(self))
self.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
self.verticalHeader().hide()
self.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.setAlternatingRowColors(True)
# this doesn t seem to have any effect
# self.verticalHeader().setMinimumSectionSize(1)
def sizeHintForRow(self, row ):
hint = super().sizeHintForRow(row)
# print(f size hint for row {row}: {hint} )
# this doesn t seem to have any effect!
if hint < 25 and hint > 10:
hint = 10
return hint
def visual_log(self, msg: str, log_level: int=logging.INFO):
model = self.model()
i_new_row = model.rowCount()
model.insertRow(i_new_row)
datetime_stamp_str = datetime.datetime.now().strftime( %a %H:%M:%S.%f )[:-3]
model.setItem(i_new_row, 0, QtGui.QStandardItem(datetime_stamp_str))
model.setItem(i_new_row, 1, QtGui.QStandardItem(str(log_level)))
self.setColumnWidth(0, 160)
self.setColumnWidth(1, 100)
model.setItem(i_new_row, 2, QtGui.QStandardItem(msg))
QtCore.QTimer.singleShot(0, self.resizeRowsToContents)
QtCore.QTimer.singleShot(10, self.scrollToBottom)
class VerticalHeader(QtWidgets.QHeaderView):
def __init__(self, parent):
super().__init__(QtCore.Qt.Vertical, parent)
def sectionSizeHint(self, logical_index):
hint = super().sectionSizeHint(logical_index)
print(f vh index {logical_index} hint {hint} )
return hint
def main():
app_instance = QtWidgets.QApplication(sys.argv)
MainWindow().show()
sys.exit(app_instance.exec())
if __name__ == __main__ :
main()
浏览高度为
BUT... 如果你有一部案文,需要一行以上,使增长高度正确,则单线浏览量总是稍微高。sizeHintForRow(
),QTableView
方法似乎总是将24(pixels)从超级阶级退回......但即使我干预这一和残酷地说“no,使它成为一个较小的猎物”,但似乎后来一些东西方都推翻了。
<代码>垂直头盔的最低条码代码>似乎也没有效果。
我还认为,表格模型的<代码>数据()方法可能是专有的,但作用13“SizeHintRole”似乎从未发射过。
Source code
I have tried looking at the source code. The method of interest here seems to be on l. 3523, one of several versions of QHeaderView
s resizeSections
method. The code is naturally quite daunting, but I did spot, for example, invalidateCachedSizeHint()
on l. 3261, which might explain why the size hints are being ignored... Anyone with particularly intricate knowledge of QHeaderView
s functionality? Update later After Musicamante suggested use of setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContent)
(good idea), I don t now know what part of the source code is involved.
......单行浏览量略高。 如果你们看到一些不同的东西,就像完全紧缩的单行流一样,请让我知道。 页: 1 一位不同的顾问可能会产生不同的结果,谁知道?