I am not too sure how to display a custom-made widget into the grid that I have set up using QtDesigner, and will appreciate if you could see what is wrong with setting up my codes in this manner, and also explain to me why I can't do that.
I have tried to add the widget from the function I have created to import my widget, but it does not work as well. Doing the following code below also did not work. I am not sure how to add widgets to my grid and set my grid as the parent grid, while setting the new widget as the child widget.
from PyQt5 import QtCore, QtGui, QtWidgets
from SelectDateTimeFinal import Ui_SelectDateTime
from CurrentDateAndDigitalClock import Ui_widget_currentDate_digitalClock
class Ui_MainWindow(object):
def openCurrentDateAndDigitalClock(self):
self.widget = QtWidgets.QWidget()
self.Ui = Ui_widget_currentDate_digitalClock()
self.Ui.setupUi(self.widget)
#NEED HEP HERE
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(950, 565)
MainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_Store3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Store3.setGeometry(QtCore.QRect(60, 400, 321, 29))
font = QtGui.QFont()
font.setFamily("Goudy Old Style")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.pushButton_Store3.setFont(font)
self.pushButton_Store3.setObjectName("pushButton_Store3")
self.pushButton_Store2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Store2.setGeometry(QtCore.QRect(580, 330, 331, 29))
font = QtGui.QFont()
font.setFamily("Goudy Old Style")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.pushButton_Store2.setFont(font)
self.pushButton_Store2.setObjectName("pushButton_Store2")
self.pushButton_Store1 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Store1.setGeometry(QtCore.QRect(60, 330, 321, 29))
font = QtGui.QFont()
font.setFamily("Goudy Old Style")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.pushButton_Store1.setFont(font)
self.pushButton_Store1.setObjectName("pushButton_Store1")
self.pushButton_Store4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Store4.setGeometry(QtCore.QRect(580, 400, 331, 29))
font = QtGui.QFont()
font.setFamily("Goudy Old Style")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.pushButton_Store4.setFont(font)
self.pushButton_Store4.setObjectName("pushButton_Store4")
self.pushButton_Reset = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_Reset.setGeometry(QtCore.QRect(10, 60, 271, 29))
font = QtGui.QFont()
font.setFamily("Goudy Old Style")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.pushButton_Reset.setFont(font)
self.pushButton_Reset.setObjectName("pushButton_Reset")
self.pushButton_SetDateTime = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_SetDateTime.setGeometry(QtCore.QRect(10, 20, 271, 29))
font = QtGui.QFont()
font.setFamily("Goudy Old Style")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.pushButton_SetDateTime.setFont(font)
self.pushButton_SetDateTime.setObjectName("pushButton_SetDateTime")
self.pushButton_SetDateTime.clicked.connect(self.openSelectDateTimeWindow)
self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(330, 20, 281, 101))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout_SelectedDateTime = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout_SelectedDateTime.setContentsMargins(0, 0, 0, 0)
self.gridLayout_SelectedDateTime.setObjectName("gridLayout_SelectedDateTime")
self.label_SelectedDateTime = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_SelectedDateTime.setText("")
self.label_SelectedDateTime.setAlignment(QtCore.Qt.AlignCenter)
self.label_SelectedDateTime.setObjectName("label_SelectedDateTime")
self.gridLayout_SelectedDateTime.addWidget(self.label_SelectedDateTime, 0, 0, 1, 1)
self.gridLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget_2.setGeometry(QtCore.QRect(640, 20, 291, 101))
self.gridLayoutWidget_2.setObjectName("gridLayoutWidget_2")
self.gridLayout_CurrentDateTime = QtWidgets.QGridLayout(self.gridLayoutWidget_2)
self.gridLayout_CurrentDateTime.setContentsMargins(0, 0, 0, 0)
self.gridLayout_CurrentDateTime.setObjectName("gridLayout_CurrentDateTime")
self.gridLayout_CurrentDateTime.addChildWidget(self.openCurrentDateAndDigitalClock)
# NEED HELP HERE
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Canteen System"))
self.pushButton_Store3.setText(_translate("MainWindow", "PushButton"))
self.pushButton_Store2.setText(_translate("MainWindow", "PushButton"))
self.pushButton_Store1.setText(_translate("MainWindow", "PushButton"))
self.pushButton_Store4.setText(_translate("MainWindow", "PushButton"))
self.pushButton_Reset.setText(_translate("MainWindow", "Reset to Current Date and Time"))
self.pushButton_SetDateTime.setText(_translate("MainWindow", "Set Date and Time"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
timer = QtCore.QTimer()
timer.timeout.connect(Ui_widget_currentDate_digitalClock.showDate)
timer.timeout.connect(Ui_widget_currentDate_digitalClock.showTime)
timer.start(100)
sys.exit(app.exec_())
I want my widget (which is a running date and time clock) to appear in the grid, as I will use that widget in other windows that I will create and would like to know how to apply it to other windows. I tried to run the following code but an error exit code -1073740791 (0xC0000409)
keeps appearing.
I can't be sure without seeing the code for Ui_widget_currentDat_digitalClock
, but I suspect that this
timer.timeout.connect(Ui_widget_currentDate_digitalClock.showDate)
timer.timeout.connect(Ui_widget_currentDate_digitalClock.showTime)
should be something like
timer.timeout.connect(ui.Ui.showDate)
timer.timeout.connect(ui.Ui.showTime)
And in Ui_MainWindow.setupUi
, this line
self.gridLayout_CurrentDateTime.addChildWidget(self.openCurrentDateAndDigitalClock)
should be something like
self.openCurrentDateAndDigitalClock() # this sets up self.widget
self.gridLayout_CurrentDateTime.addWidget(self.widget, 0, 0, 1, 1)
The output generated by pyuic should never be used as a base for your code. In fact, it should be just left alone, mostly because you might need to change your GUI at some point, making it very hard to integrate the new code with the existing one you edited until then.
As explained in the PyQt documentation, you should import that code instead and use it as a starting point for your application, from where you create the actual class of your windows and widgets and add the required connections and program logic.
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from ui_mainwindow import Ui_MainWindow
from CurrentDateAndDigitalClock import Ui_widget_currentDate_digitalClock
class DigitalClock(QWidget, Ui_widget_currentDate_digitalClock):
def __init__(self):
super(DigitalClock, self).__init__()
self.setupUi(self)
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.digitalClock = DigitalClock()
self.gridLayout_CurrentDateTime.addWidget(self.digitalClock)
self.pushButton_SetDateTime.clicked.connect(self.openSelectDateTimeWindow)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Also, you never should use addChildWidget
, which is an internal function used by QLayout subclasses. Use the specific layout's addWidget
method instead.
Please note that since you've not shared the code you used for your custom widget, I've not added the timer connection; I'm afraid that there could be some problem since you've connected it to the class methods showDate
and showTime
, while connection that require some level of GUI interaction (including automatically updating some text with a timer) should be done using the class instance methods.
User contributions licensed under CC BY-SA 3.0