QT6 and matplotlib
-
I am now adding a matplotlib graph to my system.
From my understanding, I need to drag and drop on a
QWidget; which will contain my plot.I have then followed a series of instructions on producing matplotlib graphs; all of which have worked fine.
However, these all use the idea of building the form items (a
QWidget) and then setting this as the central widget (self.setCentralWidget(sc))Obviously, I have dropped a QWidget onto my form and want the plot to appear within that object.
Using the working examples I have, I get to the following point:
sc = MplCanvas(self, width=5, height=4, dpi=100) sc.axes.plot([0, 1, 2, 3, 4], [10, 1, 20, 3, 40]) widget = self.window.findChild(QWidget, 'plotWidget')The form runs without issue or warnings but, obviously, the
QWidget(namedplotWidgeton the form) does not appear and nothing appears inside: I haven't assignedscto it yet.How do I do this assignment?
Or am I on the wrong track? -
I am now adding a matplotlib graph to my system.
From my understanding, I need to drag and drop on a
QWidget; which will contain my plot.I have then followed a series of instructions on producing matplotlib graphs; all of which have worked fine.
However, these all use the idea of building the form items (a
QWidget) and then setting this as the central widget (self.setCentralWidget(sc))Obviously, I have dropped a QWidget onto my form and want the plot to appear within that object.
Using the working examples I have, I get to the following point:
sc = MplCanvas(self, width=5, height=4, dpi=100) sc.axes.plot([0, 1, 2, 3, 4], [10, 1, 20, 3, 40]) widget = self.window.findChild(QWidget, 'plotWidget')The form runs without issue or warnings but, obviously, the
QWidget(namedplotWidgeton the form) does not appear and nothing appears inside: I haven't assignedscto it yet.How do I do this assignment?
Or am I on the wrong track?@GaryN
I have never used Matplotlib. If I understand you right. You want to put anMplCanvas()-runtime-generated object, which is aQWidget, onto a form you have designed.So you can do any of:
-
Just
addWidget(sc)somewhere in the desired place. You don't need aQWidgetplaceholder on the form at design-time. -
Replace the design-time added
QWidgetwith yoursc(you may have toremoveWidget(yourWidget)). -
Retain your design-time added
QWidget. At runtime add e.g. aQHBoxLayoutonto that widget, and thenaddWidget(sc)onto that layout.
-
-
@GaryN
I have never used Matplotlib. If I understand you right. You want to put anMplCanvas()-runtime-generated object, which is aQWidget, onto a form you have designed.So you can do any of:
-
Just
addWidget(sc)somewhere in the desired place. You don't need aQWidgetplaceholder on the form at design-time. -
Replace the design-time added
QWidgetwith yoursc(you may have toremoveWidget(yourWidget)). -
Retain your design-time added
QWidget. At runtime add e.g. aQHBoxLayoutonto that widget, and thenaddWidget(sc)onto that layout.
I really appreciate your help on all of this. :)
From your three suggestions, I get the sense that building the form as I have been is possibly the wrong way?
Anyway, just sticking to this situation at present. I did have the
addWidget(sc)aspect, but how do I define it's(x, y)position on the existing form?I have a
QTabWidgetwith (currently) 2 tabs. How do I direct theaddWidgetto ensure it goes within the right tab? -
-
@GaryN
I have never used Matplotlib. If I understand you right. You want to put anMplCanvas()-runtime-generated object, which is aQWidget, onto a form you have designed.So you can do any of:
-
Just
addWidget(sc)somewhere in the desired place. You don't need aQWidgetplaceholder on the form at design-time. -
Replace the design-time added
QWidgetwith yoursc(you may have toremoveWidget(yourWidget)). -
Retain your design-time added
QWidget. At runtime add e.g. aQHBoxLayoutonto that widget, and thenaddWidget(sc)onto that layout.
Ah, I just re-read your third bulletpoint and initially got it wrong!
Yes, that might work; since all the examples I have make the sc widget be the central widget; and that QWidget would then sit exactly where I wanted it, with the matplotlib object residing inside exactly as my example copies have it in the single window!
Am I coorect in my interpretation there?
-
-
Ah, I just re-read your third bulletpoint and initially got it wrong!
Yes, that might work; since all the examples I have make the sc widget be the central widget; and that QWidget would then sit exactly where I wanted it, with the matplotlib object residing inside exactly as my example copies have it in the single window!
Am I coorect in my interpretation there?