如何在不使用布局的情况下使用自定义窗口小部件

时间:2019-04-11 22:02:11

标签: python pyqt pyqt5

我有一个带有QWidget的QDialog,我想要的是用自定义小部件替换该小部件。

我在接口上使用QTDesigner

我已经看到了与布局相似的方法,但是即使我确实需要在应用程序的其他几个部分中重用此自定义小部件,也不要使事情变得过于复杂。而且我还需要在运行时用其他自定义小部件替换小部件。 我想做这样的事情

from PyQt5.QtWidgets import QDialog, QApplication, QWidget
from MainWindow import *

class MainWindowController(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

    //side_bar is the custom widget
    def add_side_bar(self, side_bar):
        self.ui.widget = side_bar
from PyQt5.QtWidgets import QWidget
from SideBar import *

class SideBarController(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from MainWindowController import *
from SideBarController import *
class Main(QApplication):
    def __init__(self, sys_argv):
        super(Main, self).__init__(sys_argv)
        self.main_window = MainWindowController()
        self.side_bar = SideBarController()
        self.main_window.add_side_bar(self.side_bar)
        self.main_window.show()


if __name__ == "__main__":
    app = Main(sys.argv)
    sys.exit(app.exec_())

0 个答案:

没有答案
相关问题