PDA

View Full Version : Place two pie charts side by side on one gridLayout



dennisvz
11th January 2020, 17:27
Using QChart and QChartView I show a single pie chart on a Ui with a gridLayout built in Qt Creator/Designer. I would now like to show two piecharts on the same grid layout -- side by side - with each chart taking up half the grid (vertically speaking) -- essentially making two big, equal-sized columns.
I use this code to show the first pie:


series = QPieSeries()
chart = QChart()
# the pie chart is made here
self.chartView = QChartView(chart)
self.ui.gridLayout.addWidget(self.chartView)

and have duplicate code (with different pie data) below it for the second pie.
The resulting charts show one on top of the other stretching across the UI, and the top pie takes up 3/4 of vertical space and the other gets 1/4 of the vertical space.

I have played around with various combinations of positioning, like:


self.ui.gridLayout.addWidget(self.chartView, 0, 1)


but nothing works. Is there a way to get what I want without redesigning the UI with two grid layouts?

d_stranz
11th January 2020, 22:26
According to the QGridLayout docs, there is no addWidget() method that takes only a single argument. So, PyQt must be defaulting these arguments to something, but I don't know what. You should at least provide 0, 0 as the second two arguments.

You can't add the same chartView instance to a layout more than once. So the instances in cell 0, 0 and 0, 1 must be different.

Unless you also plan to put other things in this layout, why don't you just use a QHBoxLayout instead? Then you simply call addWidget() without the need for row or column numbers.

dennisvz
12th January 2020, 00:20
I'll swap the gridLayout for a QHBoxLayout and give it a try. Thanks

Upon second thought, I had thought a QHboxLayout was a drag and drop layout I could add via the Qt Designer, but its not. How do I add it to my UI?

d_stranz
12th January 2020, 01:35
Upon second thought, I had thought a QHboxLayout was a drag and drop layout I could add via the Qt Designer

Yes, of course it is, second item under Layouts:

13320

You can also select everything in your widget, right-click in your top level widget and choose "Lay out horizontally" to wrap the contents in a horizontal layout.

dennisvz
12th January 2020, 02:47
Sorry, I was looking specifically for a QHBoxLayout; I did not realize it was the same as just plain Horizontal Layout.

d_stranz
12th January 2020, 16:55
Qt Designer is intended for use by GUI designers, who may not be programmers. So if you look at all of the names for UI items in Qt Designer, I don't think any of them have the same names as the Qt C++ classes that implement them. Of course, as programmers who also design UIs, we have to learn both sets of names. :-(