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.


  • Lifetime Qt Champion

    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...


  • Lifetime Qt Champion

    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?


  • Lifetime Qt Champion

    Are you sure it is initialised properly ?


  • Qt Champions 2016

    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


  • Qt Champions 2016

    Then you either have a QWidget under the mouse position, or you have a custom widget that's missing the Q_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.
    alt text
    But there is no any other widgets besides QChartView...


  • Qt Champions 2016

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.