PyQt4 中 QListView 的 clicked() 信号

发布于 2024-09-03 04:41:15 字数 62 浏览 3 评论 0原文

我有一个有效的 QListView,但从文档中,我无法弄清楚如何使用新选定项目的索引来触发信号。有什么想法吗?

I have a working QListView, but from the documentation, I can't figure out how to get a signal to fire with the index of the newly selected item. Any ideas?

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

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

发布评论

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

评论(2

病毒体 2024-09-10 04:41:15

恕我直言,实现此目的的一种更简单的方法是使用 QListWidget 而不是 QListView,这样您可以使用 itemClicked 信号,该信号将所选项目发送到回调函数。

Imho, an easier way to achieve this would be to use a QListWidget instead of a QListView, this way you could use the itemClicked signal, which sends the selected item to the callback function.

素衣风尘叹 2024-09-10 04:41:15

这些是我如何实现它的代码片段:

class VenueList(QListView):
    def __init__(self, parent, venues):
        super(VenueList, self).__init__(parent)
        self.clicked.connect(self.venue_selected)
        [...]

    def venue_selected(self, index):
        venue = self.model().data(index, VenueListModel.VenueRole)
        doStuff()

您可以浏览我如何使用这个的完整代码 此处(第 69 行)。不过,我确实警告您,这段代码非常糟糕,需要进行一些认真的重构。

These is a snipplet of code of how I achieved it:

class VenueList(QListView):
    def __init__(self, parent, venues):
        super(VenueList, self).__init__(parent)
        self.clicked.connect(self.venue_selected)
        [...]

    def venue_selected(self, index):
        venue = self.model().data(index, VenueListModel.VenueRole)
        doStuff()

You can browse the full code of how I used this here (line 69). I do warn you, however, that this code is pretty bad, and needs some serious refactoring.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文