Issue
I have problem to access my button and label from my dialog.ui. I am using Python 3.x and QT Designer 5.x.
from PyQt5 import uic, QtWidgets
from PyQt5.QtWidgets import QApplication
Form, Window = uic.loadUiType("dialog.ui") #load ui (GUI) file
app = QApplication([]) #create a QApplication
window = Window()
form = Form()
form.setupUi(window)
def on_click():
# self.qlFreeText.text("hello")
alert = QMessageBox()
alert.setText("You clicked the button!")
alert.exec_()
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi('basic.ui',self)
# self.ButtonSearch = self.findChild(QtWidgets.QPushButton, 'qpbSearch')
self.ButtonSearch = self.findChild(QtWidgets.QObject, 'qpbSearch')
self.ButtonSearch.button.clicked.connect(self.printButtonPressed)
self.qlFreeText = self.findChild(QWidgets.QLabel, 'qlFreeText')
# self.show()
def printButtonPressed(self):
on_click()
window.show() #show window
app.exec_() #run application until user closes it
dialog.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QDateEdit" name="dateStart">
<property name="geometry">
<rect>
<x>50</x>
<y>50</y>
<width>110</width>
<height>22</height>
</rect>
</property>
<property name="displayFormat">
<string>yyyy-MM-dd</string>
</property>
</widget>
<widget class="QDateEdit" name="dateEnd">
<property name="geometry">
<rect>
<x>220</x>
<y>50</y>
<width>110</width>
<height>22</height>
</rect>
</property>
<property name="displayFormat">
<string>yyyy-MM-dd</string>
</property>
</widget>
<widget class="QLabel" name="qlFreeText">
<property name="geometry">
<rect>
<x>120</x>
<y>140</y>
<width>55</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QPushButton" name="qpbSearch">
<property name="geometry">
<rect>
<x>190</x>
<y>220</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
When I click on the button, nothing happens. What I would like to try is, when I click on the button, than it changes the label text. But currently I can even not use the click on the button.
Solution
You are confusing the following concepts:
You are creating the UI class where you create the connection but never use it. How do you think something works?
You don't need to use findChild() since if you use loadUi or loadUiType it will map the objects using the objectName.
Considering the above, the solutions are as follows:
loadUi():
from PyQt5 import uic, QtWidgets
class Ui(QtWidgets.QDialog):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi("dialog.ui", self)
self.qpbSearch.clicked.connect(self.printButtonPressed)
def printButtonPressed(self):
self.qlFreeText.setText("hello")
alert = QtWidgets.QMessageBox()
alert.setText("You clicked the button!")
alert.exec_()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Ui()
w.show() # show window
sys.exit(app.exec_())
loadUiType():
from PyQt5 import uic, QtWidgets
Form, _ = uic.loadUiType("dialog.ui")
class Ui(QtWidgets.QDialog, Form):
def __init__(self):
super(Ui, self).__init__()
self.setupUi(self)
self.qpbSearch.clicked.connect(self.printButtonPressed)
def printButtonPressed(self):
self.qlFreeText.setText("hello")
alert = QtWidgets.QMessageBox()
alert.setText("You clicked the button!")
alert.exec_()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Ui()
w.show() # show window
sys.exit(app.exec_())
I recommend you check Using Qt Designer.
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.