Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom widget staticMetaObject
Forum Updated to NodeBB v4.3 + New Features

Custom widget staticMetaObject

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 3.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bronstein87
    wrote on last edited by bronstein87
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bronstein87
        wrote on last edited by
        #3

        i don't understand, there are only mainwindow and custom QChartView on it, nothing more. so QCursor::pos() should return QChartView...

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          QChartView is a class derived from QGraphicsView.

          What do you get if you add that qDebug()<<qApp->widgetAt(QCursor::pos()); to your keyPressEvent ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bronstein87
            wrote on last edited by bronstein87
            #5

            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?

            kshegunovK 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Are you sure it is initialised properly ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • B bronstein87

                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?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                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?

                Read and abide by the Qt Code of Conduct

                B 1 Reply Last reply
                0
                • kshegunovK kshegunov

                  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?

                  B Offline
                  B Offline
                  bronstein87
                  wrote on last edited by
                  #8

                  @kshegunov
                  output is QWidget

                  kshegunovK 1 Reply Last reply
                  0
                  • B bronstein87

                    @kshegunov
                    output is QWidget

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #9

                    Then you either have a QWidget under the mouse position, or you have a custom widget that's missing the Q_OBJECT macro.

                    Read and abide by the Qt Code of Conduct

                    B 1 Reply Last reply
                    0
                    • kshegunovK kshegunov

                      Then you either have a QWidget under the mouse position, or you have a custom widget that's missing the Q_OBJECT macro.

                      B Offline
                      B Offline
                      bronstein87
                      wrote on last edited by bronstein87
                      #10

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

                      kshegunovK 1 Reply Last reply
                      0
                      • B bronstein87

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

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by
                        #11

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

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved