qobject_cast doesn't work with QWT type
-
Hi,
I use QWT library to plot data.
After I plotted N-curves I'm trying to get one of them with name "MyCurve"QwtPlotCurve curve = plotForm->canvas()->findChild<QwtPlotCurve>(QString("MyCurve"), Qt::FindDirectChildrenOnly);But the program can't compile this line of code with the errors:
- C:\Qt\5.14.1\msvc2017_64\include\QtCore\qobject.h:176: error: C2039: 'staticMetaObject': is not a member of 'QwtPlotCurve'
- C:\Qt\5.14.1\msvc2017_64\include\QtCore\qobject.h:176: ошибка: C2440: 'static_cast': cannot convert from 'QObject *' to 'T' with [T=QwtPlotCurve]
But if I write:
QObject curve = plotForm->canvas()->findChild<QObject>(QString("MyCurve"), Qt::FindDirectChildrenOnly);it works. But then the qobject_cast<QwtPlotCurve>() throws the error :)
I googled this errors but didn't understand how to solve the problem -
Hi,
I use QWT library to plot data.
After I plotted N-curves I'm trying to get one of them with name "MyCurve"QwtPlotCurve curve = plotForm->canvas()->findChild<QwtPlotCurve>(QString("MyCurve"), Qt::FindDirectChildrenOnly);But the program can't compile this line of code with the errors:
- C:\Qt\5.14.1\msvc2017_64\include\QtCore\qobject.h:176: error: C2039: 'staticMetaObject': is not a member of 'QwtPlotCurve'
- C:\Qt\5.14.1\msvc2017_64\include\QtCore\qobject.h:176: ошибка: C2440: 'static_cast': cannot convert from 'QObject *' to 'T' with [T=QwtPlotCurve]
But if I write:
QObject curve = plotForm->canvas()->findChild<QObject>(QString("MyCurve"), Qt::FindDirectChildrenOnly);it works. But then the qobject_cast<QwtPlotCurve>() throws the error :)
I googled this errors but didn't understand how to solve the problem@Please_Help_me_D As far as I can see,
QwtPlotCurveis not aQObject

Soqobject_cast<QwtPlotCurve>()can not work -
Hi,
I use QWT library to plot data.
After I plotted N-curves I'm trying to get one of them with name "MyCurve"QwtPlotCurve curve = plotForm->canvas()->findChild<QwtPlotCurve>(QString("MyCurve"), Qt::FindDirectChildrenOnly);But the program can't compile this line of code with the errors:
- C:\Qt\5.14.1\msvc2017_64\include\QtCore\qobject.h:176: error: C2039: 'staticMetaObject': is not a member of 'QwtPlotCurve'
- C:\Qt\5.14.1\msvc2017_64\include\QtCore\qobject.h:176: ошибка: C2440: 'static_cast': cannot convert from 'QObject *' to 'T' with [T=QwtPlotCurve]
But if I write:
QObject curve = plotForm->canvas()->findChild<QObject>(QString("MyCurve"), Qt::FindDirectChildrenOnly);it works. But then the qobject_cast<QwtPlotCurve>() throws the error :)
I googled this errors but didn't understand how to solve the problem@Please_Help_me_D
QwtPlotCurve is not a QObject based class. -> qobject_cast won't work.use static_cast<QwtPlotCurve>();
that said, find child shouldn't work either. Object is probably null or invalid -> object_cast fails
-
@Please_Help_me_D As far as I can see,
QwtPlotCurveis not aQObject

Soqobject_cast<QwtPlotCurve>()can not work@KroMignon @J-Hilk thank you
I understand that this is more QWT question but still, don't you know how to get plotted item?
In debug modeQObject curve = plotForm->canvas()->findChild<QObject>(QString("MyCurve"), Qt::FindDirectChildrenOnly);shows me:

-
@KroMignon @J-Hilk thank you
I understand that this is more QWT question but still, don't you know how to get plotted item?
In debug modeQObject curve = plotForm->canvas()->findChild<QObject>(QString("MyCurve"), Qt::FindDirectChildrenOnly);shows me:

@Please_Help_me_D said in qobject_cast doesn't work with QWT type:
I understand that this is more QWT question but still, don't you know how to get plotted item?
Sorry I think I can not help you, I never used QWT and I don't understand what you want to extract from
QwtPlot.
Perhaps reading documentation could help you => https://qwt.sourceforge.io/class_qwt_plot.html -
@KroMignon @J-Hilk thank you
I understand that this is more QWT question but still, don't you know how to get plotted item?
In debug modeQObject curve = plotForm->canvas()->findChild<QObject>(QString("MyCurve"), Qt::FindDirectChildrenOnly);shows me:

@Please_Help_me_D said in qobject_cast doesn't work with QWT type:
how to get plotted item?
You already have the
itemList. You can get a list withQwtPlotCurve-items only by passingRtti_PlotCurve(plotForm->itemList( Rtti_PlotCurve );). Then you can iterate through the list and get yourQwtPlotCurve(s).https://qwt.sourceforge.io/class_qwt_plot_dict.html#a49d60b01e53a33423987aa80559449f9
There is also
infoToItem()
https://qwt.sourceforge.io/class_qwt_plot.html#aa951d433d8eba6a47bfd8f0537090c82 -
QwtPlotinherits fromQwtPlotDict, that has anitemList()method. Have you tried to iterate over it, comparing the itemtitle()and maybe thertti()value? Just a guess from five minutes into Qwt documentation, never used.
Furthermore, I think findChild should be used with a pointer type, even if here it is useless... -
@Please_Help_me_D said in qobject_cast doesn't work with QWT type:
how to get plotted item?
You already have the
itemList. You can get a list withQwtPlotCurve-items only by passingRtti_PlotCurve(plotForm->itemList( Rtti_PlotCurve );). Then you can iterate through the list and get yourQwtPlotCurve(s).https://qwt.sourceforge.io/class_qwt_plot_dict.html#a49d60b01e53a33423987aa80559449f9
There is also
infoToItem()
https://qwt.sourceforge.io/class_qwt_plot.html#aa951d433d8eba6a47bfd8f0537090c82@Pl45m4 @Pl45m4 thank you very much!
I also read the documentation but sometimes it is difficult to understand the idea of some function. Only now I understood how I could useinfoToItem().
The last thing now. I find all curvesQwtPlotItemList itemList = plotForm->itemList( QwtPlotItem::Rtti_PlotCurve );but they are stored inQwtPlotItemListtype. How to convertQwtPlotItemList[1]toQwtPlotCurve? -
@Pl45m4 @Pl45m4 thank you very much!
I also read the documentation but sometimes it is difficult to understand the idea of some function. Only now I understood how I could useinfoToItem().
The last thing now. I find all curvesQwtPlotItemList itemList = plotForm->itemList( QwtPlotItem::Rtti_PlotCurve );but they are stored inQwtPlotItemListtype. How to convertQwtPlotItemList[1]toQwtPlotCurve?@Please_Help_me_D said in qobject_cast doesn't work with QWT type:
How to convert QwtPlotItemList[1] to QwtPlotCurve?
QwtPlotItemIterator it; for ( it = itemList.begin(); it != itemList.end(); ++it ) { QwtPlotCurve* curve = static_cast<QwtPlotCurve *>( *it ); }Or if you have just one item and you know that there aren't more, you can do something like this:
if(itemList.size() > 0 && itemList[0] != nullptr) QwtPlotCurve* curve = static_cast<QwtPlotCurve *>( itemList[0] ); -
@Please_Help_me_D said in qobject_cast doesn't work with QWT type:
How to convert QwtPlotItemList[1] to QwtPlotCurve?
QwtPlotItemIterator it; for ( it = itemList.begin(); it != itemList.end(); ++it ) { QwtPlotCurve* curve = static_cast<QwtPlotCurve *>( *it ); }Or if you have just one item and you know that there aren't more, you can do something like this:
if(itemList.size() > 0 && itemList[0] != nullptr) QwtPlotCurve* curve = static_cast<QwtPlotCurve *>( itemList[0] );@Pl45m4 thank you a lot!