pyqt -qtableWidget拖动/移动列后获取标头标签

发布于 2025-01-23 11:53:36 字数 696 浏览 2 评论 0原文

我有一个QTableWidget,我填充了来自SQL的数据 - 我使视图中的列重新排序以使

self.horizontalHeader().setSectionsMovable(True)

用户完美的体验,我创建了很多代码,用户可以决定他想通过两个列表框的他想看到的列,一个显示桌子上的标头已经在SQL表中可见的所有标题。用户可以切换向右的列,并使它们可见。用户还可以将元素上下放置在列中,以从列重新排序标题。对于可见的列列表,我必须获得一个列表,其中包含所有可见标头 - 但我只有一个问题: 当用户将“ ID”之类的标头拖动到第一个位置

示例

,我想获得全部标题作为for循环中的列表,

header = self.horizontalHeaderItem(column).text()

将“用户名”作为第一列(位置0)和“ ID”为最后一列(位置3)。也

header = self.horizontalHeader().model().headerData(0,Qt.Orientation.Horizontal)

没有任何区别..

有人有什么想法吗?

I have a QTableWidget which I fill with data from SQL - I enabled to reorder the columns in the View by

self.horizontalHeader().setSectionsMovable(True)

To give the user perfect experience, I created much of code, that the user can decide which column he want to see by two list boxed, one showing the headers already in the table visible, in the other, all headers possible by the SQL Table. The user can switch the columns left an right and make them visible or not. The user can also put the elements in the list up and down to reorder the headers from the column. For the visible column list I have to get a list with all visible headers in the TableWidget - But I only have one problem:
When the user drags a Header like "ID" to first position

example

and I want to get all the headers as a list in a for loop,

header = self.horizontalHeaderItem(column).text()

gives "USERNAME" as first column (position 0) and "ID" as last column (position 3). Also

header = self.horizontalHeader().model().headerData(0,Qt.Orientation.Horizontal)

makes no difference..

Has anybody an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

止于盛夏 2025-01-30 11:53:36

您必须使用逻辑索引迭代

header = self.horizontalHeader()
    for col in range(self.columnCount()):
        print(self.horizontalHeaderItem(header.logicalIndex(col)).text())

you must iterate with logical index

header = self.horizontalHeader()
    for col in range(self.columnCount()):
        print(self.horizontalHeaderItem(header.logicalIndex(col)).text())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文