简体   繁体   English

如何将小部件推广到具有不同选择的不同类?

[英]how to promote widget to different classes with different selection?

I am using pyqt5 to plot stock trend on a widget.我在小部件上使用 pyqt5 到 plot 股票趋势。 Sometime the widget only has one plot, sometimes I'd like to have 3 subplots.有时小部件只有一个 plot,有时我想要 3 个子图。 I designed two classes for same widget like below.我为同一个小部件设计了两个类,如下所示。 Firstly I promote the widget to the below first class with only one plot.首先,我将小部件提升到下面的第一个 class,只有一个 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. 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?是否可以为具有不同选择的相同小部件更改 class? 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 更改为:

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.如果通过“提升” OP 意味着在.ui 中使用自定义小部件,那么答案是否定的,你不能。

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?显然,OP 有一个 XY 问题,因为他想知道一个可能的解决方案:如何在 2 个类之间切换一个被提升的小部件,而不是背景问题:如何更改自定义小部件中的图?

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:然后只需使用每个 function 的点击信号来更改绘图:

     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.另一个解决方案不是推广而是使用 QStackedWidget。 The logic is to add each widget to the QStackedWidget and change the index with the buttons.逻辑是将每个小部件添加到 QStackedWidget 并使用按钮更改索引。

     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) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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