【问题标题】:findChild returns None with custom widgetfindChild 使用自定义小部件返回 None
【发布时间】:2013-05-02 02:56:39
【问题描述】:

我在 Python 中创建了一个自定义 Qt 小部件,并设法让它在运行时加载到表单中,但是当我尝试使用 findChild 将它从表单实例中抓取回来时,我得到了 None

小部件已加载,如果我打印出表单上的名称和对象,我可以看到它:

DEBUG:root:<PyQt4.QtGui.QCheckBox object at 0x11358030>
DEBUG:root:Near_Other_Infrastructure
DEBUG:root:<PyQt4.QtGui.QCheckBox object at 0x113582B8>
DEBUG:root:photo
DEBUG:root:<imagewidget.QMapImageWidget object at 0x113586A8>

这是代码:

images = self.forminstance.findChild(QMapImageWidget)

更新:

似乎这样做有效:

images = self.forminstance.findChild(QWidget, "photo")

它返回DEBUG:root:&lt;imagewidget.QMapImageWidget object at 0x113586A8&gt;

虽然我真的更喜欢通过类型获取控件而不使用名称。

有什么想法吗?

【问题讨论】:

  • 我们可以看到您用于将小部件加载到表单中的行吗?
  • 我正在使用 QUiLoader() 和 load 方法加载它。
  • 我也遇到过这种情况。可能是 Qt 或 PyQt 中的错误?你试过 self.forminstance.findChild(QMapImageWidget, "photo")。也就是说,对您有用的是一种解决方法,也许只是运气。您的原始代码也应该可以工作。

标签: python qt pyqt


【解决方案1】:

我也有这个问题。

一个简单的解决方案是找到一个非自定义基类并进行强制转换。

Custom* ptr = dynamic_cast<QWidget*>(root->findChild<QWidget*>("MyWidgetName"));
if (ptr)
{
    //...whatever
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2021-03-08
    • 2014-04-09
    • 1970-01-01
    • 2014-05-06
    • 2022-01-18
    • 2014-05-12
    相关资源
    最近更新 更多