How can I use a custom widget from Qt Designer with PySide?

时间:2016-04-04 16:31:22

标签: python pyside qt-designer

I have created the following custom widget with Qt Designer:

from PySide import QtCore, QtGui

class Ui_Custom_Date_Range_Widget(object):
    def setupUi(self, Custom_Date_Range_Widget):
        Custom_Date_Range_Widget.setObjectName("Custom_Date_Range_Widget")
        Custom_Date_Range_Widget.resize(341, 101)
        Custom_Date_Range_Widget.setMinimumSize(QtCore.QSize(341, 0))
        Custom_Date_Range_Widget.setMaximumSize(QtCore.QSize(341, 110))
        Custom_Date_Range_Widget.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.widget = QtGui.QWidget(Custom_Date_Range_Widget)
        self.widget.setGeometry(QtCore.QRect(6, 1, 330, 99))
        self.widget.setObjectName("widget")
        self.custom_date_range_verticalLayout = QtGui.QVBoxLayout(self.widget)
        self.custom_date_range_verticalLayout.setContentsMargins(5, 5, 5, 5)
        self.custom_date_range_verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.custom_date_range_verticalLayout.setObjectName("custom_date_range_verticalLayout")
        self.custom_date_range_label = QtGui.QLabel(self.widget)
        font = QtGui.QFont()
        font.setFamily("Segoe UI Symbol")
        font.setPointSize(12)
        font.setWeight(50)
        font.setBold(False)
        self.custom_date_range_label.setFont(font)
        self.custom_date_range_label.setObjectName("custom_date_range_label")
        self.custom_date_range_verticalLayout.addWidget(self.custom_date_range_label)
        self.custom_date_range_date_edits_horizontalLayout = QtGui.QHBoxLayout()
        self.custom_date_range_date_edits_horizontalLayout.setObjectName("custom_date_range_date_edits_horizontalLayout")
        self.custom_date_range_from_label = QtGui.QLabel(self.widget)
        font = QtGui.QFont()
        font.setFamily("Segoe UI Light")
        font.setPointSize(12)
        self.custom_date_range_from_label.setFont(font)
        self.custom_date_range_from_label.setObjectName("custom_date_range_from_label")
        self.custom_date_range_date_edits_horizontalLayout.addWidget(self.custom_date_range_from_label)
        self.custom_date_range_from_dateEdit = QtGui.QDateEdit(self.widget)
        font = QtGui.QFont()
        font.setFamily("Segoe UI Light")
        font.setPointSize(12)
        self.custom_date_range_from_dateEdit.setFont(font)
        self.custom_date_range_from_dateEdit.setCalendarPopup(True)
        self.custom_date_range_from_dateEdit.setObjectName("custom_date_range_from_dateEdit")
        self.custom_date_range_date_edits_horizontalLayout.addWidget(self.custom_date_range_from_dateEdit)
        self.custom_date_range_to_label = QtGui.QLabel(self.widget)
        font = QtGui.QFont()
        font.setFamily("Segoe UI Light")
        font.setPointSize(12)
        self.custom_date_range_to_label.setFont(font)
        self.custom_date_range_to_label.setObjectName("custom_date_range_to_label")
        self.custom_date_range_date_edits_horizontalLayout.addWidget(self.custom_date_range_to_label)
        self.custom_date_range_to_dateEdit = QtGui.QDateEdit(self.widget)
        font = QtGui.QFont()
        font.setFamily("Segoe UI Light")
        font.setPointSize(12)
        self.custom_date_range_to_dateEdit.setFont(font)
        self.custom_date_range_to_dateEdit.setCalendarPopup(True)
        self.custom_date_range_to_dateEdit.setObjectName("custom_date_range_to_dateEdit")
        self.custom_date_range_date_edits_horizontalLayout.addWidget(self.custom_date_range_to_dateEdit)
        self.custom_date_range_verticalLayout.addLayout(self.custom_date_range_date_edits_horizontalLayout)
        self.custom_date_range_buttons_horizontalLayout = QtGui.QHBoxLayout()
        self.custom_date_range_buttons_horizontalLayout.setObjectName("custom_date_range_buttons_horizontalLayout")
        self.custom_date_range_cancel_pushButton = QtGui.QPushButton(self.widget)
        font = QtGui.QFont()
        font.setFamily("Segoe UI Light")
        font.setPointSize(12)
        self.custom_date_range_cancel_pushButton.setFont(font)
        self.custom_date_range_cancel_pushButton.setStyleSheet("QPushButton {border: 2px solid rgb(255, 255, 255);\n"
" color: rgb(255, 255, 255);\n"
"border-radius: 10px;\n"
"pading: 0.8px;\n"
"background-color: rgb(170, 0, 0);}\n"
"\n"
"QPushButton:checked {background-color: rgb(0, 170, 0);}\n"
"\n"
"QPushButton:hover {background-color: rgb(0, 170, 255); }")
        self.custom_date_range_cancel_pushButton.setObjectName("custom_date_range_cancel_pushButton")
        self.custom_date_range_buttons_horizontalLayout.addWidget(self.custom_date_range_cancel_pushButton)
        self.custom_date_range_apply_pushButton = QtGui.QPushButton(self.widget)
        font = QtGui.QFont()
        font.setFamily("Segoe UI Light")
        font.setPointSize(12)
        self.custom_date_range_apply_pushButton.setFont(font)
        self.custom_date_range_apply_pushButton.setStyleSheet("QPushButton {border: 2px solid rgb(255, 255, 255);\n"
" color: rgb(255, 255, 255);\n"
"border-radius: 10px;\n"
"pading: 0.8px;\n"
"background-color: rgba(0, 105, 234, 255);}\n"
"\n"
"QPushButton:checked {background-color: rgb(0, 170, 0);}\n"
"\n"
"QPushButton:hover {background-color: rgb(0, 170, 255); }")
        self.custom_date_range_apply_pushButton.setObjectName("custom_date_range_apply_pushButton")
        self.custom_date_range_buttons_horizontalLayout.addWidget(self.custom_date_range_apply_pushButton)
        self.custom_date_range_verticalLayout.addLayout(self.custom_date_range_buttons_horizontalLayout)

        self.retranslateUi(Custom_Date_Range_Widget)
        QtCore.QMetaObject.connectSlotsByName(Custom_Date_Range_Widget)

    def retranslateUi(self, Custom_Date_Range_Widget):
        Custom_Date_Range_Widget.setWindowTitle(QtGui.QApplication.translate("Custom_Date_Range_Widget", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.custom_date_range_label.setText(QtGui.QApplication.translate("Custom_Date_Range_Widget", "Custom Date Range", None, QtGui.QApplication.UnicodeUTF8))
        self.custom_date_range_from_label.setText(QtGui.QApplication.translate("Custom_Date_Range_Widget", "FROM", None, QtGui.QApplication.UnicodeUTF8))
        self.custom_date_range_from_dateEdit.setDisplayFormat(QtGui.QApplication.translate("Custom_Date_Range_Widget", "dd-MMM-yyyy", None, QtGui.QApplication.UnicodeUTF8))
        self.custom_date_range_to_label.setText(QtGui.QApplication.translate("Custom_Date_Range_Widget", "TO", None, QtGui.QApplication.UnicodeUTF8))
        self.custom_date_range_to_dateEdit.setDisplayFormat(QtGui.QApplication.translate("Custom_Date_Range_Widget", "dd-MMM-yyyy", None, QtGui.QApplication.UnicodeUTF8))
        self.custom_date_range_cancel_pushButton.setText(QtGui.QApplication.translate("Custom_Date_Range_Widget", "Cancel", None, QtGui.QApplication.UnicodeUTF8))
        self.custom_date_range_apply_pushButton.setText(QtGui.QApplication.translate("Custom_Date_Range_Widget", "Apply", None, QtGui.QApplication.UnicodeUTF8))

I want to use the above widget in a menu, and I thought to do this:

from custom_date_range_widget import Ui_Custom_Date_Range_Widget

self.custom_date_range_menu = QtGui.QMenu(self)
self.custom_date_range_menu.setFixedHeight(120)
self.custom_date_range_menu.setFixedWidth(350)

self.custom_date_range_widget = Ui_Custom_Date_Range_Widget(
self.custom_date_range_menu)

self.custom_date_range_pushButton.setMenu(self.custom_date_range_menu)

I get the following error:

self.custom_date_range_widget = Ui_Custom_Date_Range_Widget(self.custom_date_range_menu)
TypeError: object.__new__() takes no parameters

1 个答案:

答案 0 :(得分:0)

you want the setupUI to run when you create a new instance correct? When you create a new instance of a class the special __init__ method is called so you would want to override the object.__init__ which is complaining about receiving input and add this:

class Ui_Custom_Date_Range_Widget(object):
    def __init__(self, Custom_Date_Range_Widget):
        self.setupUI(Custom_Date_Range_Widget)

    def setupUI ...

or possibly just rename the setupUI to __init__