我一直试图获取使用QtDesigner创建的小部件以显示在主窗口中(我也使用QtDesigner创建的小窗口)显示出来,但无法这样做。我一直在研究如何执行此操作,但尚未找到适合我的代码的解决方案。我创建了一个类,以继承QtDesigner的Ui生成的代码,如下所示。
我尝试创建对象QtWidget.QGridLayout
,然后将小部件添加到布局中,但是当我打开主窗口时,它仍然不显示小部件。这是因为我在小部件代码下创建了一个类吗?该类应该显示当前日期和时间,并显示运行时间,因此我使用QTimer函数创建了一个类。我有什么方法可以使用QtDesigner生成的代码来工作,而又不创建新类?推荐吗?我之所以问是因为我不熟悉编程,因此对类的工作方式并不熟悉。
在小部件中,
class Ui_widget_currentDate_digitalClock(object):
def setupUi(self, widget_currentDate_digitalClock):
widget_currentDate_digitalClock.setObjectName("widget_currentDate_digitalClock")
widget_currentDate_digitalClock.resize(631, 170)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(widget_currentDate_digitalClock.sizePolicy().hasHeightForWidth())
widget_currentDate_digitalClock.setSizePolicy(sizePolicy)
self.verticalLayout = QtWidgets.QVBoxLayout(widget_currentDate_digitalClock)
self.verticalLayout.setObjectName("verticalLayout")
self.label_CurrentDate = QtWidgets.QLabel(widget_currentDate_digitalClock)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_CurrentDate.sizePolicy().hasHeightForWidth())
self.label_CurrentDate.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Palatino Linotype")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_CurrentDate.setFont(font)
self.label_CurrentDate.setAlignment(QtCore.Qt.AlignCenter)
self.label_CurrentDate.setObjectName("label_CurrentDate")
self.verticalLayout.addWidget(self.label_CurrentDate)
self.label_DigitalClock = QtWidgets.QLabel(widget_currentDate_digitalClock)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_DigitalClock.sizePolicy().hasHeightForWidth())
self.label_DigitalClock.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Palatino Linotype")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_DigitalClock.setFont(font)
self.label_DigitalClock.setAlignment(QtCore.Qt.AlignCenter)
self.label_DigitalClock.setObjectName("label_DigitalClock")
self.verticalLayout.addWidget(self.label_DigitalClock)
self.retranslateUi(widget_currentDate_digitalClock)
QtCore.QMetaObject.connectSlotsByName(widget_currentDate_digitalClock)
def retranslateUi(self, widget_currentDate_digitalClock):
_translate = QtCore.QCoreApplication.translate
widget_currentDate_digitalClock.setWindowTitle(_translate("widget_currentDate_digitalClock", "Canteen System"))
self.label_CurrentDate.setText(_translate("widget_currentDate_digitalClock", "Current Date"))
self.label_DigitalClock.setText(_translate("widget_currentDate_digitalClock", "Current Time"))
class OpenDTWidget(QtWidgets.QWidget, Ui_widget_currentDate_digitalClock):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showDate)
timer.timeout.connect(self.showTime)
timer.start(100)
def showDate(self):
date = QtCore.QDate.currentDate()
textDate = date.toString("dddd, dd MMMM, yyyy")
self.label_CurrentDate.setText(textDate)
def showTime(self):
time = QtCore.QTime.currentTime()
textTime = time.toString('hh:mm:ss')
self.label_DigitalClock.setText(textTime)
在主窗口中
from DTWidget import OpenDTWidget
class OpenMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.transformedUi(self)
def transformedUi(self, MainWindow):
super(OpenMainWindow, self).setupUi(MainWindow)
self.OpenDtWidget = OpenDTWidget()
self.OpenDtWidget.setGeometry(QtCore.QRect(200, 200, 100, 100))
self.OpenDtWidget.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = OpenMainWindow()
MainWindow.show()
sys.exit(app.exec_())
调用我在小部件代码中创建的对象后,如何使它在主窗口中使用,而不是在执行上述操作时让新的小部件窗口自己打开?在哪里添加代码以实现我想要的?非常感谢您提供的任何帮助。非常感谢。