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:
#ifndef MYQGRAPHICSVIEW_H
#define MYQGRAPHICSVIEW_H
#include <QWidget>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QMouseEvent>
#include <QFileDialog>
#include "log.h"
LOG_USE();
{
Q_OBJECT
public:
explicit MyQGraphicsView
(QWidget *parent
= nullptr
);
signals:
void viewClicked();
public slots:
void saveImage();
private:
// member variable to store click position
// member variable - flag of click beginning
bool m_mouseClick;
};
#endif // MYQGRAPHICSVIEW_H
#ifndef MYQGRAPHICSVIEW_H
#define MYQGRAPHICSVIEW_H
#include <QWidget>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QMouseEvent>
#include <QFileDialog>
#include "log.h"
LOG_USE();
class MyQGraphicsView : public QGraphicsView
{
Q_OBJECT
public:
explicit MyQGraphicsView(QWidget *parent = nullptr);
signals:
void viewClicked();
public slots:
void mouseReleaseEvent(QMouseEvent * e);
void saveImage();
private:
// member variable to store click position
QPoint m_lastPoint;
// member variable - flag of click beginning
bool m_mouseClick;
};
#endif // MYQGRAPHICSVIEW_H
To copy to clipboard, switch view to plain text mode
myqgraphicsview.cpp:
#include "myqgraphicsview.h"
MyQGraphicsView
::MyQGraphicsView(QWidget *parent
) :{
}
void MyQGraphicsView
::mouseReleaseEvent(QMouseEvent * e
) {
if (e->button() == Qt::RightButton) {
saveImage();
}
}
void MyQGraphicsView::saveImage()
{
ascene->clearSelection(); // Selections would also render to the file
ascene->setSceneRect(ascene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents
QImage image
(ascene
->sceneRect
().
size().
toSize(),
QImage::Format_ARGB32);
// Create the image with the exact size of the shrunk scene image.fill(Qt::transparent); // Start all pixels transparent
ascene->render(&painter);
QString fileName
= QFileDialog::getSaveFileName(this, tr
("Image File Name"),
".", tr
("Image Files (*.png)"));
if (fileName.compare("") != 0) {
image.save(fileName);
}
}
#include "myqgraphicsview.h"
MyQGraphicsView::MyQGraphicsView(QWidget *parent) :
QGraphicsView(parent)
{
}
void MyQGraphicsView::mouseReleaseEvent(QMouseEvent * e)
{
if (e->button() == Qt::RightButton) {
saveImage();
}
}
void MyQGraphicsView::saveImage()
{
QGraphicsScene *ascene = this->scene();
ascene->clearSelection(); // Selections would also render to the file
ascene->setSceneRect(ascene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents
QImage image(ascene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene
image.fill(Qt::transparent); // Start all pixels transparent
QPainter painter(&image);
ascene->render(&painter);
QString fileName = QFileDialog::getSaveFileName(this, tr("Image File Name"), ".", tr("Image Files (*.png)"));
if (fileName.compare("") != 0) {
image.save(fileName);
}
}
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).
Bookmarks