如何在不创建派生类的情况下模拟 QTreeWidget itemClicked 信号?

发布于 2024-12-20 10:10:57 字数 159 浏览 3 评论 0原文

我无法找到 QTreeWidget 的 ItemClicked() SIGNAL 的正确模拟。

有没有办法模拟它以便生成 ItemClicked 信号?

例如:我们可以在 QTreeWidget 的派生类中发出 ItemClicked,但不能(作为 QT 规则)在它之外。

I am unable to find a proper simulation for ItemClicked() SIGNAL for QTreeWidget.

Is there a way to simulate it so that ItemClicked Signal is generated ?

e.g: we can emit ItemClicked in a derived class of QTreeWidget but cannot (as a QT rule) outside of it.

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

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

发布评论

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

评论(1

残月升风 2024-12-27 10:10:57

您不能使用 A 类的 emit 调用来发出 B 类的信号。但请注意信号和槽的文档 说:

“您可以将任意数量的信号连接到单个插槽,并且一个信号可以连接到您需要的任意数量的插槽。甚至可以将一个信号直接连接到另一个信号。(这将发出每当第一个信号发出时,立即发出第二个信号。)”

因此,您可以通过在 A 类中声明一个与您希望 B 类发出的信号具有相同签名的信号来解决此问题,并将这些信号连接在一起:

connect(
    myclass, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
    treewidget, SIGNAL(itemClicked(QTreeWidgetItem*, int))
); 

然后发出itemClicked 来自 myclass。如果我没有记错的话,它适用于这种情况......并为您触发树形小部件的 itemClicked 信号。

You can't use the emit call for class A to emit class B's signals. But note that the documentation for signals and slots says:

"You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)"

So you can work around this by declaring a signal in class A of the same signature as the one you want class B to emit, and connecting the signals together:

connect(
    myclass, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
    treewidget, SIGNAL(itemClicked(QTreeWidgetItem*, int))
); 

Then emit itemClicked from myclass. If I'm not mistaken, it will work for this case...and fire the treewidget's itemClicked signal for you.

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