QTableView automatically scrolls to the bottom to achieve a scrolling effect similar to QTextBrowser

Original article

QTableView automatically scrolls to the bottom to achieve a scrolling effect similar to QTextBrowser


Preface

I have been studying QT for a while. I have learned a lot in CSDN and I should make some contributions.


提示:原创文章,转载请注明本地址

1. Realization effect

Qt control, how to automatically scroll to the bottom after adding items with ScrollView. It is similar to the effect that QTextBrowser automatically scrolls to the bottom after adding a new item ( it automatically scrolls when it is at the bottom, and stops scrolling when it is turned to the top ). I searched online for a long time but could not find the specific implementation code. I tried many methods but the results were not good. Suddenly I came up with the implementation code. It is simple and efficient. I posted it here for you to use :)

2. Directly upload the code

1.Code

Examples are as follows (original, please indicate if you want to reprint):

# 初始化TableView
	def initTableView():
		self.Table = QTableView()
		# 禁用编辑
        self.Table.setEditTriggers(QAbstractItemView.NoEditTriggers)
        # 禁用Tab键导航
        self.Table.setTabKeyNavigation(False)
        # 去除选中虚线框
        self.Table.setFocusPolicy(Qt.NoFocus)
        # 设置表格参考线是否可见
        self.Table.setShowGrid(False)
        # 设置是否启用滚动条
        self.Table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)  # 禁用水平滚动条
        self.Table.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)  # 启用垂直滚动条
        
        # 前面一通设置无关紧要,后面这里是关键,定义垂直滚动条用来设置自动滚动
        self.TableScrollBar = self.Table.verticalScrollBar()   

# 当增加新条目后
	def appendNewData():
		#.......
			#append
		#.......
		# 这里设置自动滚动,每增加一个新条目就自动滚动到最后。就这么简单 :)
        if self.TableScrollBar.value() >= self.TableScrollBar.maximum():
            self.Table.scrollToBottom()

Summarize

There is nothing to summarize, just read more and practice more. Some effects are not readily available on the Internet. You can achieve them by yourself if you think more.

Guess you like

Origin blog.csdn.net/flash_temp/article/details/125285712