简体   繁体   中英

how to promote widget to different classes with different selection?

I am using pyqt5 to plot stock trend on a widget. Sometime the widget only has one plot, sometimes I'd like to have 3 subplots. I designed two classes for same widget like below. Firstly I promote the widget to the below first class with only one plot. Later when I select different button to plot different plots, I'd like to change the plot to have 3 subplots, which means I need to change class for the widget. Is it possible to change class for same widget with different selections? or Is there any alternative way?

class gui_stock_oneplot(QWidget):
    def __init__(self,parent=None):
        QWidget.__init__(self,parent)
        self.canvas=FigureCanvas(Figure())
        
        vertical_layout=QVBoxLayout()
        vertical_layout.addWidget(self.canvas)
        
        self.canvas.axis1=self.canvas.figure.add_subplot(111)
        self.canvas.axis2=self.canvas.axis1.twinx()
        self.canvas.axis3=self.canvas.axis1.twinx()
        self.canvas.figure.set_facecolor("white")  #lightblue
        self.setLayout(vertical_layout)

want to change widget class to:

class gui_stock_3plot(QWidget):
    def __init__(self,parent=None):
        QWidget.__init__(self,parent)
        self.canvas=FigureCanvas(Figure())
        
        vertical_layout=QVBoxLayout()
        vertical_layout.addWidget(self.canvas)
        
        self.canvas.axis1=self.canvas.figure.add_subplot(311)
        self.canvas.axis2=self.canvas.figure.add_subplot(312)
        self.canvas.axis3=self.canvas.figure.add_subplot(313)
        self.canvas.figure.set_facecolor("white")  #lightblue
        self.setLayout(vertical_layout)

If by "promote" the OP means using a custom widget in.ui then the answer is no, you can't.

Clearly the OP has an XY problem since he is wondering about a possible solution: how to switch between 2 classes a widget that was promoted, instead of the background problem: How to change the plots in a custom widget?

For the underlying problem there are at least 2 possible solutions:

  1. You can implement all the logic in the same widget first by cleaning the figure and then repainting:

     class Widget(QWidget): def __init__(self): super().__init__() layout = QVBoxLayout(self) self.canvas = FigureCanvas(Figure()) layout.addWidget(self.canvas) def one_plot(self): self.canvas.figure.clear() self.canvas.axis1 = self.canvas.figure.add_subplot(111) self.canvas.axis2 = self.canvas.axis1.twinx() self.canvas.axis3 = self.canvas.axis1.twinx() self.canvas.figure.set_facecolor("white") # lightblue self.canvas.draw() def three_plot(self): self.canvas.figure.clear() self.canvas.axis1 = self.canvas.figure.add_subplot(311) self.canvas.axis2 = self.canvas.figure.add_subplot(312) self.canvas.axis3 = self.canvas.figure.add_subplot(313) self.canvas.figure.set_facecolor("white") # lightblue self.canvas.draw()

    Then just use the clicked signal of each function to change plots:

     self.one_button.clicked.connect(self.foo_widget.one_plot) self.three_button.clicked.connect(self.foo_widget.three_plot)
  2. The other solution is not to promote but to use a QStackedWidget. The logic is to add each widget to the QStackedWidget and change the index with the buttons.

     self.one_widget = gui_stock_oneplot() one_index = self.stacked_widget.addWidget(self.one_widget) self.one_button.clicked.connect( lambda *args, index=one_index: self.stacked_widget.setCurrentIndex(index) ) self.three_widget = gui_stock_3plot() three_index = self.stacked_widget.addWidget(self.three_widget) self.three_button.clicked.connect( lambda *args, index=three_index: self.stacked_widget.setCurrentIndex(index) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM