Ir para o conteúdo

Python e Qt 6: Como utilizar um arquivo de interface XML com loadUiType()

Como utilizar um arquivo de interface XML com a linguagem de programação Python e loadUiType().

Código

Cuidado

Lembre-se de adequar a localização do arquivo de interface e ícones.

XML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>683</width>
    <height>384</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Python e Qt 6: Signals e Slots (XML loadUiType)</string>
  </property>
  <property name="windowIcon">
   <iconset>
    <normaloff>../../data/icons/icon.png</normaloff>../../data/icons/icon.png</iconset>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QLabel" name="label">
      <property name="text">
       <string>Digite algo no campo de texto e clique no botão</string>
      </property>
      <property name="alignment">
       <set>Qt::AlignCenter</set>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QLineEdit" name="line_edit">
      <property name="placeholderText">
       <string>Digite algo</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="button">
      <property name="text">
       <string>Clique Aqui</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menu_bar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>683</width>
     <height>21</height>
    </rect>
   </property>
   <widget class="QMenu" name="menu_file">
    <property name="title">
     <string>Arquivo</string>
    </property>
    <addaction name="action_close_app"/>
   </widget>
   <addaction name="menu_file"/>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
  <widget class="QToolBar" name="toolbar">
   <property name="windowTitle">
    <string>toolBar</string>
   </property>
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
   <addaction name="action_close_app"/>
  </widget>
  <action name="action_close_app">
   <property name="icon">
    <iconset>
     <normaloff>../../data/icons/exit-128px.png</normaloff>../../data/icons/exit-128px.png</iconset>
   </property>
   <property name="text">
    <string>Sair</string>
   </property>
   <property name="toolTip">
    <string>Sair do aplicativo</string>
   </property>
  </action>
 </widget>
 <resources/>
 <connections/>
</ui>

Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
"""Python e Qt 6: Como interagir com arquivo de interface XML utilizando loadUiType()"""

from PySide6.QtCore import Slot
from PySide6.QtUiTools import loadUiType
from PySide6.QtWidgets import QApplication

Ui_MainWindow, Ui_QMainWindow = loadUiType('MainWindow.ui')


class MainWindow(Ui_MainWindow, Ui_QMainWindow):
    def __init__(self, application):
        super().__init__()
        self.application = application
        self.setupUi(self)

        self.action_close_app.triggered.connect(self.actn_close_app)

        self.button.clicked.connect(self.on_button_clicked)

        self.show()

    @Slot()
    def on_button_clicked(self):
        text = self.line_edit.text()
        if text:
            self.label.setText(text)
        else:
            self.label.setText('Digite algo no campo de texto!')

    @Slot()
    def actn_close_app(self):
        self.close()

    def closeEvent(self, event):
        """Método é executado quando a janela é fechada."""
        print('[!] A janela foi fechada [!]')

    def showEvent(self, event):
        """Método é executado quando a janela ganha foco."""
        print('[!] Janela ganhou foco [!]')


if __name__ == "__main__":
    import sys

    # Para exibir o ícone na taskbar (barra de tarefas) do windows.
    if sys.platform == "win32" or sys.platform == 'win64':
        from ctypes import windll

        appid = 'br.natorsc.exemplo'
        windll.shell32.SetCurrentProcessExplicitAppUserModelID(appid)

    application = QApplication(sys.argv)
    window = MainWindow(application=application)
    sys.exit(application.exec_())