python - findChild 使用自定义小部件返回 None

标签 python qt pyqt

我已经在 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:<imagewidget.QMapImageWidget object at 0x113586A8>

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

有什么想法吗?

最佳答案

我也遇到这个问题

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

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

关于python - findChild 使用自定义小部件返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16329622/

相关文章:

python - PyQt:槽执行多次

python - Pyparsing:如何将 token 存储到以下列表/组中?

python - python中如何转置单列数据的问题

c++ - 将参数正确传递给使用 Qt 的 QProcess 运行的 Linux 二进制文件

windows - 如何在 QtCreator 中设置编译器和调试器

python - 在 QLineEdit 中拦截 contextMenuEvent() 时出现 __init_subclass__ 错误

python - PIL替换颜色的最佳方法?

python - 亚马逊 Elastic Beanstalk : how to set the wsgi path?

c++ - QT - QFile 复制操作极其缓慢

python - SPYDER 中的 PyQT 退出错误