Python QT findChildren 未从 UI 文件中找到任何 children
Python QT findChildren does not find any children from UI file
我有一个 UI 这样的文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>RoverPlanner</class>
<widget class="QWidget" name="RoverPlanner">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>752</width>
<height>555</height>
</rect>
</property>
<property name="windowTitle">
<string>RoverPlanner</string>
</property>
<widget class="QWidget" name="horizontalLayoutWidget_6">
<property name="geometry">
<rect>
<x>0</x>
<y>10</y>
<width>641</width>
<height>501</height>
</rect>
</property>
<layout class="QHBoxLayout" name="hl_rover_planner">
<item>
<layout class="QVBoxLayout" name="vl_path_config">
<item>
<layout class="QHBoxLayout" name="hl_rover_select">
<item>
<widget class="QLabel" name="lbl_rover">
<property name="text">
<string>Rover : </string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cb_rover">
<item>
<property name="text">
<string>Husky</string>
</property>
</item>
<item>
<property name="text">
<string>ArgoJ5</string>
</property>
</item>
<item>
<property name="text">
<string>Husky (Simulated)</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="hl_path_creator">
<item>
<widget class="QLabel" name="lbl_path_creator">
<property name="text">
<string>Path Creator</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_add_to_path">
<property name="text">
<string>Add to path</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="hl_segment_options">
<item>
<layout class="QVBoxLayout" name="vl_segment_type">
<item>
<widget class="QRadioButton" name="radio_circle">
<property name="text">
<string>Circle</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radio_line">
<property name="text">
<string>Line</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="vl_segment_extra_options">
<item>
<widget class="QCheckBox" name="chk_backwards">
<property name="text">
<string>Backwards</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="chk_clockwise">
<property name="text">
<string>Clockwise</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="hl_radius">
<item>
<widget class="QLabel" name="lbl_radius">
<property name="text">
<string>Circle Radius (m)</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="spin_radius">
<property name="singleStep">
<double>0.010000000000000</double>
</property>
<property name="value">
<double>1.000000000000000</double>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="hl_arc_angle">
<item>
<widget class="QLabel" name="lbl_arc_angle">
<property name="text">
<string>Arc Angle (rad)</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="spin_arc_angle">
<property name="decimals">
<number>5</number>
</property>
<property name="singleStep">
<double>0.010000000000000</double>
</property>
<property name="value">
<double>0.785400000000000</double>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="vl_segments">
<item>
<widget class="QLabel" name="lbl_path">
<property name="text">
<string>Paths</string>
</property>
</widget>
</item>
<item>
<widget class="QTreeView" name="tree_paths"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="btn_save_paths">
<property name="text">
<string>Save Paths</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_load_paths">
<property name="text">
<string>Load Paths</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
下面还有一些 Python 代码,用于 RQT ROS 插件。
import os
import rospy
import rospkg
from PySide2.QtWidgets import QTreeView, QPushButton, QFileDialog
from python_qt_binding import loadUi
from qt_gui.plugin import Plugin
from python_qt_binding.QtWidgets import QWidget
from argparse import ArgumentParser
def get_file():
print("Hello World!")
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.AnyFile)
dlg.setFilter("XML files (*.xml)")
if dlg.exec_():
filenames = dlg.selectedFiles()
f = open(filenames[0], 'r')
with f:
data = f.read()
# self.contents.setText(data)
class RoverPlanner(Plugin):
def __init__(self, context):
super(RoverPlanner, self).__init__(context)
self.setObjectName('RoverPlanner')
parser = ArgumentParser()
parser.add_argument("-q", "--quiet", action="store_true",
dest="quiet",
help="Put plugin in silent mode")
args, unknowns = parser.parse_known_args(context.argv())
if not args.quiet:
print('arguments: ', args)
print('unknowns: ', unknowns)
self.widget = QWidget()
ui_file = os.path.join(rospkg.RosPack().get_path('cuarl-rover-planner'), 'resource', 'rover_planner_widget.ui')
loadUi(ui_file, self.widget)
self.widget.setObjectName('RoverPlannerUI')
# Show widget.windowTitle on left-top of each plugin (when
# it's set in widget). This is useful when you open multiple
# plugins at once. Also if you open multiple instances of your
# plugin at once, these lines add number to make it easy to
# tell from pane to pane.
if context.serial_number() > 1:
self.widget.setWindowTitle(self.widget.windowTitle() + (' (%d)' % context.serial_number()))
# Add widget to the user interface
context.add_widget(self.widget)
self.tree_paths = self.widget.findChild(QTreeView)
self.btn_load_paths = self.widget.findChildren(QPushButton)
print(self.tree_paths)
print(self.btn_load_paths)
def shutdown_plugin(self):
# TODO unregister all publishers here
pass
def save_settings(self, plugin_settings, instance_settings):
# TODO save intrinsic configuration, usually using:
# instance_settings.set_value(k, v)
pass
def restore_settings(self, plugin_settings, instance_settings):
# TODO restore intrinsic configuration, usually using:
# v = instance_settings.value(k)
pass
根据文档,我的代码似乎应该找到这些 children。但是这段代码的输出是
('arguments: ', Namespace(quiet=False))
('unknowns: ', [])
None
[]
我不知道是我没有正确使用 findChildren/findChild 函数还是缺少某些东西
我曾经遇到过类似的问题,原因是库使用了一些像 python_qt_binding 这样的包装器,但代码的另一部分使用了 PyQt5 或 PySide2。为什么会这样?因为包装器创建新的 类 使用相同的代码库但最终它们是不同的 类.
我的建议是,如果您要使用 python_qt_binding,则不再直接使用 pyqt5 或 pyside2,而是使用包装器,在您的情况下,您应该使用以下内容:
import os
from argparse import ArgumentParser
import rospy
import rospkg
from qt_gui.plugin import Plugin
from python_qt_binding.QtWidgets import QWidget, <b>QTreeView, QPushButton, QFileDialog</b>
from python_qt_binding import loadUi
# ....
我有一个 UI 这样的文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>RoverPlanner</class>
<widget class="QWidget" name="RoverPlanner">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>752</width>
<height>555</height>
</rect>
</property>
<property name="windowTitle">
<string>RoverPlanner</string>
</property>
<widget class="QWidget" name="horizontalLayoutWidget_6">
<property name="geometry">
<rect>
<x>0</x>
<y>10</y>
<width>641</width>
<height>501</height>
</rect>
</property>
<layout class="QHBoxLayout" name="hl_rover_planner">
<item>
<layout class="QVBoxLayout" name="vl_path_config">
<item>
<layout class="QHBoxLayout" name="hl_rover_select">
<item>
<widget class="QLabel" name="lbl_rover">
<property name="text">
<string>Rover : </string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cb_rover">
<item>
<property name="text">
<string>Husky</string>
</property>
</item>
<item>
<property name="text">
<string>ArgoJ5</string>
</property>
</item>
<item>
<property name="text">
<string>Husky (Simulated)</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="hl_path_creator">
<item>
<widget class="QLabel" name="lbl_path_creator">
<property name="text">
<string>Path Creator</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_add_to_path">
<property name="text">
<string>Add to path</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="hl_segment_options">
<item>
<layout class="QVBoxLayout" name="vl_segment_type">
<item>
<widget class="QRadioButton" name="radio_circle">
<property name="text">
<string>Circle</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radio_line">
<property name="text">
<string>Line</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="vl_segment_extra_options">
<item>
<widget class="QCheckBox" name="chk_backwards">
<property name="text">
<string>Backwards</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="chk_clockwise">
<property name="text">
<string>Clockwise</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="hl_radius">
<item>
<widget class="QLabel" name="lbl_radius">
<property name="text">
<string>Circle Radius (m)</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="spin_radius">
<property name="singleStep">
<double>0.010000000000000</double>
</property>
<property name="value">
<double>1.000000000000000</double>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="hl_arc_angle">
<item>
<widget class="QLabel" name="lbl_arc_angle">
<property name="text">
<string>Arc Angle (rad)</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="spin_arc_angle">
<property name="decimals">
<number>5</number>
</property>
<property name="singleStep">
<double>0.010000000000000</double>
</property>
<property name="value">
<double>0.785400000000000</double>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="vl_segments">
<item>
<widget class="QLabel" name="lbl_path">
<property name="text">
<string>Paths</string>
</property>
</widget>
</item>
<item>
<widget class="QTreeView" name="tree_paths"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="btn_save_paths">
<property name="text">
<string>Save Paths</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_load_paths">
<property name="text">
<string>Load Paths</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
下面还有一些 Python 代码,用于 RQT ROS 插件。
import os
import rospy
import rospkg
from PySide2.QtWidgets import QTreeView, QPushButton, QFileDialog
from python_qt_binding import loadUi
from qt_gui.plugin import Plugin
from python_qt_binding.QtWidgets import QWidget
from argparse import ArgumentParser
def get_file():
print("Hello World!")
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.AnyFile)
dlg.setFilter("XML files (*.xml)")
if dlg.exec_():
filenames = dlg.selectedFiles()
f = open(filenames[0], 'r')
with f:
data = f.read()
# self.contents.setText(data)
class RoverPlanner(Plugin):
def __init__(self, context):
super(RoverPlanner, self).__init__(context)
self.setObjectName('RoverPlanner')
parser = ArgumentParser()
parser.add_argument("-q", "--quiet", action="store_true",
dest="quiet",
help="Put plugin in silent mode")
args, unknowns = parser.parse_known_args(context.argv())
if not args.quiet:
print('arguments: ', args)
print('unknowns: ', unknowns)
self.widget = QWidget()
ui_file = os.path.join(rospkg.RosPack().get_path('cuarl-rover-planner'), 'resource', 'rover_planner_widget.ui')
loadUi(ui_file, self.widget)
self.widget.setObjectName('RoverPlannerUI')
# Show widget.windowTitle on left-top of each plugin (when
# it's set in widget). This is useful when you open multiple
# plugins at once. Also if you open multiple instances of your
# plugin at once, these lines add number to make it easy to
# tell from pane to pane.
if context.serial_number() > 1:
self.widget.setWindowTitle(self.widget.windowTitle() + (' (%d)' % context.serial_number()))
# Add widget to the user interface
context.add_widget(self.widget)
self.tree_paths = self.widget.findChild(QTreeView)
self.btn_load_paths = self.widget.findChildren(QPushButton)
print(self.tree_paths)
print(self.btn_load_paths)
def shutdown_plugin(self):
# TODO unregister all publishers here
pass
def save_settings(self, plugin_settings, instance_settings):
# TODO save intrinsic configuration, usually using:
# instance_settings.set_value(k, v)
pass
def restore_settings(self, plugin_settings, instance_settings):
# TODO restore intrinsic configuration, usually using:
# v = instance_settings.value(k)
pass
根据文档,我的代码似乎应该找到这些 children。但是这段代码的输出是
('arguments: ', Namespace(quiet=False))
('unknowns: ', [])
None
[]
我不知道是我没有正确使用 findChildren/findChild 函数还是缺少某些东西
我曾经遇到过类似的问题,原因是库使用了一些像 python_qt_binding 这样的包装器,但代码的另一部分使用了 PyQt5 或 PySide2。为什么会这样?因为包装器创建新的 类 使用相同的代码库但最终它们是不同的 类.
我的建议是,如果您要使用 python_qt_binding,则不再直接使用 pyqt5 或 pyside2,而是使用包装器,在您的情况下,您应该使用以下内容:
import os
from argparse import ArgumentParser
import rospy
import rospkg
from qt_gui.plugin import Plugin
from python_qt_binding.QtWidgets import QWidget, <b>QTreeView, QPushButton, QFileDialog</b>
from python_qt_binding import loadUi
# ....