Skip to content

Instantly share code, notes, and snippets.

@fovtran
Created September 12, 2018 07:32
Show Gist options
  • Save fovtran/bdc38b5c00fcf817e30d9bfd2ff968fc to your computer and use it in GitHub Desktop.
Save fovtran/bdc38b5c00fcf817e30d9bfd2ff968fc to your computer and use it in GitHub Desktop.
QT Ui file Widgets bindings generator
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""qt_list_ui_widgets.py: Provides a list of QT5 widgets contained in a XML UI file made with QTCreator."""
__author__ = "dmc"
__license__ = "GPL"
__version__ = "1.0.1"
__email__ = "dcadogan@live.com.ar"
__status__ = "Testing"
import sys,os
import xml.etree.ElementTree
from collections import defaultdict
class qt_ui_parser():
def __init__(self):
self.conns = []
self.widgets = []
def describe_widgets(self, file):
"""List class of UI widgets for a QT5 definitions made with QTCreator"""
_xml = xml.etree.ElementTree.parse(file)
root = _xml.getroot()
root_tag = root.tag
root_attr = root.attrib
root_iter = _xml.iter() # iterate the ui root element
for elem in root_iter:
for subelem in elem:
if subelem.tag == "widget":
self.widgets.append(subelem)
print("Widget: \t" + subelem.attrib['name'] + " : " + subelem.attrib['class'])
for elem in root_iter:
for subelem in elem:
if subelem.tag == "connections":
print("Connections: ")
for conn in subelem:
self.conns.append(conn)
print(conn.tag)
def get_conns(self):
return self.conns
def get_widgets(self):
return self.widgets
def list_widget_targets(self):
print("\n")
c = 0
for widget in self.widgets:
c += 1
w_name = widget.attrib['name']
w_class = widget.attrib['class']
w_target = "self." + w_name + "_" + str(c)
w_event = "stateChanged"
print( "\tself.%s.%s.connect(%s)" %(w_name , w_event, w_target) )
print("\n")
c = 0
for widget in self.widgets:
c += 1
w_name = widget.attrib['name']
w_class = widget.attrib['class']
w_target = "self." + w_name + "_" + str(c)
print( "\tdef %s(self, s):" %(w_name) )
print( "\t\t'''Classtype: %s'''" %(w_class) )
print( "\t\tself.%s = self.%s.isChecked()"% (w_name, w_name) )
def list_conn_targets(self):
for conn in self.conns:
print( "self.%s.%s.connect(%s)" %("checkBox", "stateChanged", "self.extra_filtering") )
if __name__ == "__main__":
print("Listando clases Widget en UI de Qt5\n")
c = qt_ui_parser()
c.describe_widgets('simple.ui')
conns = c.get_conns()
c.list_widget_targets()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment