Unsolved Custom widget staticMetaObject
-
I added QChartVIew like custom widget in Qt Designer. When i do something like that:
qDebug()<<ui->ChartViewAlpha->objectName(); qDebug()<<ui->ChartViewAlpha->staticMetaObject.className();
it gives me correct information about widget. but, when i'm catching some event, for example:
void MainWindow::keyPressEvent(QKeyEvent* pe) { qDebug()<<qApp->widgetAt(QCursor::pos())->objectName(); qDebug()<<qApp->widgetAt(QCursor::pos())->staticMetaObject.className(); }
it returns empty strings, but cursor under cursor was QChartView. How can i resolve this problem? Also trying to convert via dynamic_cast or qobject_cast to QChartView * returns a null pointer.
-
Hi,
Because your pointer is "inside" a widget, doesn't mean that what is under it is that widget hence the null pointer returned by qobject_cast.
For example, if you use a QMainWnidow and add a QWidget as a central widget with a QPushButton in it, if you move your mouse over the button and call widgetAt you'll get the QPushButton but neither the central widget QWidget nor the QMainWindow even though your cursor is technically inside these two widgets..
-
i don't understand, there are only mainwindow and custom QChartView on it, nothing more. so QCursor::pos() should return QChartView...
-
QChartView is a class derived from QGraphicsView.
What do you get if you add that
qDebug()<<qApp->widgetAt(QCursor::pos());
to your keyPressEvent ? -
It returns QWidget(0x0) , but maybe problem is i added qchartview like it was written here? [https://forum.qt.io/topic/75148/add-qchartview-to-qt-desiner-like-custom-widget/2](link url) and i need to make Base class name: QGraphicsView not QWidget?
-
Are you sure it is initialised properly ?
-
How's the compiler supposed to deduce the meta-object at compile time? (that's what you're asking it to do by using
staticMetaObject
).
There's a polymorphic function just for this:QObject::metaObject
, so what does:qDebug() << qApp->widgetAt(QCursor::pos())->metaObject().className();
output?
-
@kshegunov
output is QWidget -
Then you either have a
QWidget
under the mouse position, or you have a custom widget that's missing theQ_OBJECT
macro. -
@kshegunov
so, i took "Zoom Line Example" from Qt Creator and added in Chart View's method void ChartView::mousePressEvent(QMouseEvent *event)
this: QMessageBox::information(NULL,tr("Error"),qApp->widgetAt(QCursor::pos())->metaObject()->className());
It returned QWidget.
But there is no any other widgets besides QChartView... -
@bronstein87 said in Custom widget staticMetaObject:
But there is no any other widgets besides QChartView...
That you know of.
QChartView
can have multiple child widgets inside of itself to draw/organize its layout/display.