Results 1 to 12 of 12

Thread: Q_DECLARE_METATYPE problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Q_DECLARE_METATYPE problem

    I'm now building with Qt5 code that compiled with no problems with Qt4, and getting an error message that is a bit obscure to me.

    F:\Qt\5.13.0\msvc2017\include\QtCore\qmetatype.h:1 798: error: C2338: Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system
    F:\Qt\5.13.0\msvc2017\include\QtCore\qmetatype.h:1 805: see reference to function template instantiation 'int qMetaTypeId<T>(void)' being compiled
    with
    [
    T=QMouseEvent *
    ]
    D:\testing\build-vspheroid-Qt3d\release\moc_myqgraphicsview.cpp:98: see reference to function template instantiation 'int qRegisterMetaType<QMouseEvent*>(void)' being compiled
    F:\Qt\5.13.0\msvc2017\include\QtCore\qatomic.h:56: see reference to class template instantiation 'QBasicAtomicInteger<int>' being compiled
    F:\Qt\5.13.0\msvc2017\include\QtCore\qatomic.h:154 : see reference to class template instantiation 'QAtomicInteger<int>' being compiled

    I have extended QGraphicsView to allow image save on a mouse click like this:

    myqgraphicsview.h:

    Qt Code:
    1. #ifndef MYQGRAPHICSVIEW_H
    2. #define MYQGRAPHICSVIEW_H
    3.  
    4. #include <QWidget>
    5. #include <QGraphicsView>
    6. #include <QGraphicsScene>
    7. #include <QMouseEvent>
    8. #include <QFileDialog>
    9.  
    10. #include "log.h"
    11. LOG_USE();
    12.  
    13. class MyQGraphicsView : public QGraphicsView
    14. {
    15. Q_OBJECT
    16. public:
    17. explicit MyQGraphicsView(QWidget *parent = nullptr);
    18.  
    19. signals:
    20. void viewClicked();
    21. public slots:
    22. void mouseReleaseEvent(QMouseEvent * e);
    23. void saveImage();
    24. private:
    25. // member variable to store click position
    26. QPoint m_lastPoint;
    27. // member variable - flag of click beginning
    28. bool m_mouseClick;
    29. };
    30.  
    31. #endif // MYQGRAPHICSVIEW_H
    To copy to clipboard, switch view to plain text mode 

    myqgraphicsview.cpp:

    Qt Code:
    1. #include "myqgraphicsview.h"
    2.  
    3. MyQGraphicsView::MyQGraphicsView(QWidget *parent) :
    4. QGraphicsView(parent)
    5. {
    6. }
    7.  
    8. void MyQGraphicsView::mouseReleaseEvent(QMouseEvent * e)
    9. {
    10. if (e->button() == Qt::RightButton) {
    11. saveImage();
    12. }
    13. }
    14.  
    15. void MyQGraphicsView::saveImage()
    16. {
    17. QGraphicsScene *ascene = this->scene();
    18. ascene->clearSelection(); // Selections would also render to the file
    19. ascene->setSceneRect(ascene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents
    20. QImage image(ascene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene
    21. image.fill(Qt::transparent); // Start all pixels transparent
    22. QPainter painter(&image);
    23. ascene->render(&painter);
    24. QString fileName = QFileDialog::getSaveFileName(this, tr("Image File Name"), ".", tr("Image Files (*.png)"));
    25. if (fileName.compare("") != 0) {
    26. image.save(fileName);
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    I don't know where and how I should insert the Q_DECLARE_METATYPE line. I don't know what metatype it is referring to (or even what a metatype is).

  2. #2
    Join Date
    Aug 2019
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Q_DECLARE_METATYPE problem

    I tried the code you posted and it compiles fine without any error (as expected). So to be honest I don't understand why you have this error message.

    In fact you don't have to declare Qt's types as metatypes as Qt already knows them.
    Normally you should not have to declare any metatype for overriding the mouseReleaseEvent().

    Perhaps you can try to rebuild everything from scratch, from a clean base (remove every build-generated files, check your .pro file, run qmake and then run the compilation).

    To answer your questioning about the metatypes, you need to use Q_DECLARE_METATYPE for user custom classes/structures if you want to use them in QVariant or in signals/slots connections. The related type must provide a public default constructor, a public copy constructor and a public destructor to be eligible to be declared as a metatype. Usually, we call the macro right below the class/struct declaration in the header file. If you use queued connections, you'll need to call qRegisterMetaType() too.
    For more accurate information, look at the documentation about it.

  3. #3
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Q_DECLARE_METATYPE problem

    Thanks for looking at this. The situation now is even more confusing. I have removed MyQGraphicsView from the program, which eliminated that error. But after adding back most of the rest of the code (I started with a very stripped-down version of the program I am converting over from Qt4 to Qt5) I am getting the same Q_DECLARE_METATYPE error, but now I can't see where it is coming from. These are the 5 lines of error messages:

    F:\Qt\5.13.0\msvc2017\include\QtCore\qmetatype.h:1 798: error: C2338: Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system
    F:\Qt\5.13.0\msvc2017\include\QtCore\qmetatype.h:1 805: see reference to function template instantiation 'int qMetaTypeId<T>(void)' being compiled
    with
    [
    T=QMouseEvent *
    ]
    D:\testing\build-vspheroid-Qt3d\release\moc_mainwindow.cpp:686: see reference to function template instantiation 'int qRegisterMetaType<QMouseEvent*>(void)' being compiled
    F:\Qt\5.13.0\msvc2017\include\QtCore\qatomic.h:56: see reference to class template instantiation 'QBasicAtomicInteger<int>' being compiled
    F:\Qt\5.13.0\msvc2017\include\QtCore\qatomic.h:154 : see reference to class template instantiation 'QAtomicInteger<int>' being compiled

    The relevant code in moc_mainwindow.cpp is:
    Qt Code:
    1. case 73:
    2. switch (*reinterpret_cast<int*>(_a[1])) {
    3. default: *reinterpret_cast<int*>(_a[0]) = -1; break;
    4. case 0:
    5. *reinterpret_cast<int*>(_a[0]) = qRegisterMetaType< QMouseEvent* >(); break;
    6. }
    7. break;
    To copy to clipboard, switch view to plain text mode 

    i.e. again reference to QMouseEvent. The file mainwindow.cpp is quite long, a couple of thousand lines, and I have no idea what part of the code is generating this error. There is no indication of what is causing the error. I have cleaned everything, run qmake and rebuilt (many times).

    Edit: I have discovered that the error is triggered by this function:

    Qt Code:
    1. void MainWindow::clickedGraph(QMouseEvent *event)
    2. {
    3. if (event->button() == Qt::RightButton) {
    4. LOG_QMSG("Right button click");
    5. QString fileName = QFileDialog::getSaveFileName(this, tr("Select image file"), ".",
    6. tr("Image files (*.png)"));
    7. if (fileName.compare("") == 0) {
    8. return;
    9. }
    10. colony_plot->savePng(fileName);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    specifically, it appears, by the function argument. There doesn't seem to be anything unusual about this.
    Last edited by gib; 19th August 2019 at 12:48.

  4. #4
    Join Date
    Aug 2019
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Q_DECLARE_METATYPE problem

    It seems strange indeed.

    When you "cleaned everything", did you also remove the moc files ? (in order to let qmake regenerate them properly).

    If this problem still appears, I think it comes either from the way you do the porting from Qt4 to Qt5 or from some code that becomes wrong from a version to another by generating side effects (which makes it harder to track).
    I do not pretend it is the case but if we could have a complete, minimal, reproducible (compilable and runnable) example that illustrates your problem, it would be much better to find the problem (and this way, we will avoid uncertain conjectures).

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Q_DECLARE_METATYPE problem

    Any errors from Qt's metatype systems are usually due to MOC (the "meta-object compiler"), which reads your header files and generates a "moc" file with metatype information for every class where the Q_OBJECT macro is declared.

    If, in doing your porting, you are simply copying over moc_*.cpp and ui_*.h files, then this is incorrect. These files are generated during the build process from the corresponding *.h and *.ui files by the MOC and UIC programs. You should remove them then rebuild the whole thing so they can be regenerated against the proper set of Qt 5 headers and libraries.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    121
    Thanks
    9
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Q_DECLARE_METATYPE problem

    No, I started with just the source files, certainly no moc_ files and no ui_*.h files. I'm on the trail of the bug again this morning.

    Edit: If I comment out clickedGraph I can build a working version of the program (not yet complete, but displaying the UI). If I comment out just the contents of the function, so I have just:

    void MainWindow::clickedGraph(QMouseEvent *event)
    {
    }

    I get the metatype error.

    There is something very odd about this line:

    void MainWindow::clickedGraph(QMouseEvent *event)

    In Qt Creator 'MainWindow' normally shows as pink, the rest of the line a dark blue. On this line 'inWindow::cl' was pink, the rest dark blue. I made a copy of the line (commenting out the original line), and this showed all as dark blue. After a couple of cycles of commenting and uncommenting, the anomalous pink disappeared, now what I see is either:

    Qt Code:
    1. //void MainWindow::clickedGraph(QMouseEvent *event)
    2. void MainWindow::clickedGraph(QMouseEvent *event)
    To copy to clipboard, switch view to plain text mode 

    or
    Qt Code:
    1. void MainWindow::clickedGraph(QMouseEvent *event)
    2. //void MainWindow::clickedGraph(QMouseEvent *event)
    To copy to clipboard, switch view to plain text mode 

    In the first case, the uncommented line is all black, while in the second case it is all dark blue (I don't know if these colours show up here.)

    Oh, now after some commenting and uncommenting, the uncommented line is all black in both cases. So I have a moving target, and I don't even know what it is.
    It is looking increasingly like bad behaviour on the part of Qt Creator.
    Last edited by gib; 19th August 2019 at 23:54.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Q_DECLARE_METATYPE problem

    Then are you running qmake after adding new files to the project? Qmake is necessary in order to ensure that the moc_*.cpp and ui*.h files are added to the project when you add new source files.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Q_DECLARE_METATYPE(T); error
    By SentaVera in forum Qt Programming
    Replies: 3
    Last Post: 2nd January 2014, 14:40
  2. Q_DECLARE_METATYPE() questions.
    By hickscorp in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2010, 16:04
  3. Problem to declare a stl map - Q_DECLARE_METATYPE
    By Althor in forum Qt Programming
    Replies: 5
    Last Post: 9th December 2008, 09:41
  4. How to use Q_DECLARE_METATYPE
    By lni in forum Qt Programming
    Replies: 6
    Last Post: 21st July 2008, 17:13
  5. Q_DECLARE_METATYPE and qVariantCanConvert
    By manojmka in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2008, 06:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.