[PyQt] Cannot connect to the clicked signal of a QPieSeries

Maurizio Berti maurizio.berti at gmail.com
Wed Mar 21 13:42:14 GMT 2018


I cannot test here because I have not QChart installed, so I can't answer
on the series.clicked problem.
About the slice.clicked connect, the reason you are receiving always the
same number is because you are using lambda within a for cycle, which
simply uses the latest value for "i" as an argument for the function:
lambda will act as any other function, if it cannot find "i" in its scope,
will look for it in locals() or eventually globals().

To get that working you have to explicitly declare the argument:
slice.clicked.connect(lambda i=i: slice_clicked(i))

Be careful when using lambdas with signals, since they can have optional
arguments (such as QAbstractButton's clicked) and you have to take them
into account when adding lambda arguments.


2018-03-21 12:45 GMT+01:00 Joseba Echevarria García <josebagar at gmail.com>:

> Hi all,
>
> I'm trying to create a dynamic QChart plot based on QPieSeries. In this
> context I want to be able to detect which pie slice was clicked.and to do
> so I have set up a basic example (shown below).
>
> My problem is that I am unable to detect which slice was clicked; when
> connecting the `clicked` signal of a Series I get a TypeError exception
> stating:
> TypeError: C++ type 'QPieSlice*' is not supported as a signal argument type
> I'm guessing the feature is not yet implemented? I have unsuccessfully
> tried to connect each slice's `clicked` signal to the slot, but I'm
> always getting the latest value in the series ( slice_clicked  always
> sees i=1 always in the example below).
>
> I'm getting the same exception when trying to connect the hovered signal.
> I have also tried passing the type name as a string by using the @pyqtSlot
> decorator, to no success.
>
> Is there anything I can do to be able to connect the `clicked` signal for
> the QPieSeries?
>
> I'm using Python 3.6.3 as bundled with Anaconda, Qt5.10.0, PyQt 5.10 and
> PyQtChart 5.10 as downloaded from pip on Windows 7 64 bits.
>
> Sample code:
> import sys
> from PyQt5 import QtWidgets, QtChart, QtGui
>
> def slice_clicked(*args, **kwargs):
>     print('Slice clicked!')
>     print(args)
>     print(kwargs)
>
> # Create the PyQt app and main window
> app = QtWidgets.QApplication(sys.argv)
> window = QtWidgets.QMainWindow()
> # Create the QChart object and set a few options
> chart = QtChart.QChart()
> chart.legend().hide()
> chart.setAnimationOptions(QtChart.QChart.SeriesAnimations)
> chart.setTitle('My precious')
> # Create the series with the plot data with some silly data in it.
> series = QtChart.QPieSeries()
> series.setUseOpenGL(enable=True)
> series.append('Gandalf', 3)
> series.append('Frodo', 1)
> # Show the labels in each of the slices
> for i, slice in enumerate(series.slices()):
>     slice.setLabelVisible()
>     # The following does not work either (i is always `1`)
>     slice.clicked.connect(lambda: slice_clicked(i))
> # Connect the slice clicked signal to the slot
> # The following crashes with the following message:
> #     Traceback (most recent call last):
> #       File "C:/Users/jechevarria/.PyCharm2017.3/config/scratches/scratch_5.py",
> line 24, in <module>
> #         series.clicked.connect(slice_clicked)
> #     *TypeError: C++ type 'QPieSlice*' is not supported as a signal
> argument type*
> # series.clicked.connect(slice_clicked)
> # Add the series to the chart, create the view and add some more options
> chart.addSeries(series)
> view = QtChart.QChartView(chart)
> view.setRenderHint(QtGui.QPainter.Antialiasing)
>
> window.resize(640, 480)
> window.setCentralWidget(view)
> window.show()
>
> sys.exit(app.exec_())
>
> Thanks in advance,
> Joseba
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>



-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180321/194cb48f/attachment.html>


More information about the PyQt mailing list