Qt: Compare QVariable with Enum of DLL - qt

I try to compare a QVariable of a QSignalSpy with a Enum in a test function. I found Q_ENUM_NS, but I got a linker error. I have rebuilt it and run qmake.
Error Message: Undefined reference to CCS::staticMetaObject
testopcua.cpp (most parts)
Q_DECLARE_METATYPE(CCS::DataManagerState)
using namespace ::testing;
class TestOpcUa : public Test
{
public:
TestOpcUa()
{
//.....
}
~TestOpcUa()
{
delete _selectedConfValid;
delete _opcUaObjectValid;
}
protected:
MockOpcConf* _selectedConfValid = nullptr;
OpcUa* _opcUaObjectValid = nullptr;
};
TEST_F(TestOpcUa, checkHandshake)
{
qRegisterMetaType<CCS::DataManagerState>("state");
QSignalSpy readySpy(_opcUaObjectValid, SIGNAL(newManagerState(CCS::DataManagerState)));
_opcUaObjectValid->connect();
readySpy.wait();
ASSERT_EQ(readySpy.count(), 1);
ASSERT_EQ(readySpy.at(0).at(0).value<CCS::DataManagerState>(), CCS::DataManagerState::Ready); //Linker error
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
testing::InitGoogleTest(&argc, argv);
testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
enums.h of DLL
#include <QObject>
namespace CCS
{
Q_NAMESPACE
enum class DataManagerState
{None, Configure, Ready, Active, Waiting, Stop};
Q_ENUM_NS(DataManagerState)
}

I think you need to add an explicit export/import directive to the QT_NAMESPACE macro.
namespace CCS
{
Q_DECL_EXPORT Q_NAMESPACE
...
}
But you'll most likely need to abstract the Q_DECL_EXPORT part so it is a Q_DECL_IMPORT in right circumstances, as described in Creating Shared Libraries (or in the Qt Wiki How to create a library with Qt...)
global.h of DLL
#include <QtCore/QtGlobal>
#if defined(MYSHAREDLIB_LIBRARY)
# define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
#else
# define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
#endif
enums.h of DLL
#include "global.h"
#include <QObject>
namespace CCS
{
MYSHAREDLIB_EXPORT Q_NAMESPACE
...
}
Note that if you plan to also use the "DLL" code as a statically linked lib (or directly included in other code, w/out any lib), you'll need a 3rd condition in global.h which defines MYSHAREDLIB_EXPORT as nothing.
global.h of DLL
#include <QtCore/QtGlobal>
#if defined(MYSHAREDLIB_LIBRARY)
# define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
#elif defined(MYSHAREDLIB_STATIC)
# define MYSHAREDLIB_EXPORT
#else
# define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
#endif
Q_DECLARE_METATYPE(CCS::DataManagerState)
Shouldn't need this.

Since Qt 5.14 you can use Q_NAMESPACE_EXPORT macro.
https://doc.qt.io/qt-5/qobject.html#Q_NAMESPACE_EXPORT
namespace CSS
{
Q_NAMESPACE_EXPORT(MYSHAREDLIB_LIBRARY)
...
}

Related

Qt - Compile-time check if qRegisterMetaType<T>() was called

‎
Question: Is there a way to check at compile-time if qRegisterMetaType<T>() was called for a custom type T?
The custom type T needs to be registered in Qt meta-type system in order to be used in e.g. queued connections.
If such a connection is made, and a signal triggered, the runtime warning will be shown:
QObject::connect: Cannot queue arguments of type 'T'
(Make sure 'T' is registered using qRegisterMetaType().)
This is hard to track, so I would prefer to check this at compile-time. Is that in any way possible?
(I understand that if it was possible, it would probably already be a part of Qt Framework itself, but maybe...?)
Note: I know I can check if a type was declared as metatype (Check if type is declared as a meta type system (for SFINAE)), but this doesn't solve my problem.
The code example would be:
#include <QCoreApplication>
#include <QDebug>
#include <QMetaMethod>
#include <QObject>
#include <QThread>
#include <QTimer>
struct Payload {
Payload() = default;
};
// Type is declared as metatype
Q_DECLARE_METATYPE(Payload)
class ObjectOne : public QObject {
Q_OBJECT
public:
using QObject::QObject;
void emitPayloadChanged() { Payload p; emit payloadChanged(p); }
signals:
void payloadChanged(const Payload& p);
};
class ObjectTwo : public QObject {
Q_OBJECT
public:
using QObject::QObject;
void handlePayload(const Payload& p) { qDebug() << "handling payload"; }
};
int main(int argc, char* argv[]) {
QCoreApplication app(argc, argv);
// Uncommenting the following line fixes the runtime warning
// qRegisterMetaType<Payload>();
QThread t1, t2;
ObjectOne o1;
o1.moveToThread(&t1);
ObjectTwo o2;
o2.moveToThread(&t2);
t1.start();
t2.start();
QObject::connect(&o1, &ObjectOne::payloadChanged, &o2, &ObjectTwo::handlePayload);
QTimer::singleShot(0, &o1, [&] { QMetaObject::invokeMethod(&o1, &ObjectOne::emitPayloadChanged); });
return app.exec();
}
#include "main.moc"

Image not shown using Qpixmap with QT5.4 and mvs2013 x64

I am using QT (version 5.4 msvc2013_64) with of-course vs2013. The compilation is successful but when running not every thing functions. Before with vs2010 and QT (i think version 5.1 msvc_2010_opengl do not remember exactly) every thing was working but now things are wried which maybe typical with the start of newer versions but need to be resolved. I also got other issues with this but lets get this resolved first as maybe others will be resolved too else I will post another question for the other problems. Of-course If i do not see a solution to this problem with the current QT and vs I will switch to vs2012 (requires downloading and installing); Maybe then everything will work as before. Thanks in advance.
ui_ta7feezquran.h
/********************************************************************************
** Form generated from reading UI file 'ta7feezquran.ui'
**
** Created by: Qt User Interface Compiler version 5.4.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_TA7FEEZQURAN_H
#define UI_TA7FEEZQURAN_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Ta7feezQuranClass
{
public:
QWidget *centralWidget;
QLabel *a;
QMenuBar *menuBar;
QToolBar *mainToolBar;
void setupUi(QMainWindow *Ta7feezQuranClass)
{
if (Ta7feezQuranClass->objectName().isEmpty())
Ta7feezQuranClass->setObjectName(QStringLiteral("Ta7feezQuranClass"));
Ta7feezQuranClass->resize(572, 485);
centralWidget = new QWidget(Ta7feezQuranClass);
centralWidget->setObjectName(QStringLiteral("centralWidget"));
a = new QLabel(centralWidget);
a->setObjectName(QStringLiteral("a"));
a->setGeometry(QRect(-30, -20, 601, 471));
a->setPixmap(QPixmap(QString::fromUtf8(":/Ta7feezQuran/Resources/Amasjed Alharam.jpg")));
a->setScaledContents(true);
Ta7feezQuranClass->setCentralWidget(centralWidget);
menuBar = new QMenuBar(Ta7feezQuranClass);
menuBar->setObjectName(QStringLiteral("menuBar"));
menuBar->setGeometry(QRect(0, 0, 572, 21));
menuBar->setLayoutDirection(Qt::RightToLeft);
Ta7feezQuranClass->setMenuBar(menuBar);
mainToolBar = new QToolBar(Ta7feezQuranClass);
mainToolBar->setObjectName(QStringLiteral("mainToolBar"));
Ta7feezQuranClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
retranslateUi(Ta7feezQuranClass);
QMetaObject::connectSlotsByName(Ta7feezQuranClass);
} // setupUi
void retranslateUi(QMainWindow *Ta7feezQuranClass)
{
Ta7feezQuranClass->setWindowTitle(QApplication::translate("Ta7feezQuranClass", "Ta7feezQuran", 0));
a->setText(QString());
} // retranslateUi
};
namespace Ui {
class Ta7feezQuranClass: public Ui_Ta7feezQuranClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_TA7FEEZQURAN_H
ta7feezquran.h
#ifndef TA7FEEZQURAN_H
#define TA7FEEZQURAN_H
#include <QtWidgets/QMainWindow>
#include "ui_ta7feezquran.h"
class Ta7feezQuran : public QMainWindow
{
Q_OBJECT
public:
Ta7feezQuran(QWidget *parent = 0);
~Ta7feezQuran();
private:
Ui::Ta7feezQuranClass ui;
};
#endif // TA7FEEZQURAN_H
ta7feezquran.cpp
#include "ta7feezquran.h"
Ta7feezQuran::Ta7feezQuran(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
Ta7feezQuran::~Ta7feezQuran()
{
}
main.cpp
#include "ta7feezquran.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Ta7feezQuran w;
w.show();
return a.exec();
}
ta7feezquran.qrc
<RCC>
<qresource prefix="/Ta7feezQuran">
<file>Resources/Amasjed Alharam.jpg</file>
</qresource>
</RCC>
Some Application features used, for example images, multimedia, print support, etc., require the use of qt plugins.
The required plugins should be copied to the executable folder (folder where .exe file is included) by copying the plugin folders (with the dlls included) and pasting them inside the executable folder. Every thing else should work as expected.
In this case imageformats folder (with qjpeg.dll included as the file used is jpg) is needed to copied.

templated class in Qt

Is it possible to use a templated class (not based on QObject and doesn't have Q_OBJECT macro) in Qt? I keep getting a linker error when trying to use a templated class. however, when I remove the template from the class, it compiles and links fine. I'm just trying to declare a local variable of type Filter, which uses a template, and I get this linker error:
error: undefined reference to `NumericFilter<int>::NumericFilter(int, int)'
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "filter.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
NumericFilter<int> filter(0, 1);
}
MainWindow::~MainWindow()
{
delete ui;
}
filter.h
template <class T>
class NumericFilter {
public:
NumericFilter (int itemType, int val);
protected:
T m_val;
};
filter.cpp
#include "filter.h"
template <class T>
NumericFilter<T>::NumericFilter (int, int)
{
}
Note that if you remove the template in the declaration and source files and comment out the 'T' member, then it compiles fine.
http://www.parashift.com/c++-faq-lite/separate-template-fn-defn-from-decl.html
If you compile and (try to) link these two .cpp files, most compilers will generate linker errors. There are two solutions for this. The first solution is to physically move the definition of the template function into the .h file, even if it is not an inline function.
This solution may (or may not!) cause significant code bloat, meaning your executable size may increase dramatically (or, if your compiler is smart enough, may not; try it and see).
The other solution is to leave the definition of the template function in the .cpp file and simply add the line template void foo(); to that file:
// File "foo.cpp"
#include <iostream>
#include "foo.h"
template<typename T> void foo()
{
std::cout << "Here I am!\n";
}
template void foo<int>();
So for your case, you would have in your .cpp file:
#include "filter.h"
template <class T>
NumericFilter<T>::NumericFilter(int, int)
{
}
template NumericFilter<int>::NumericFilter<int>(int, int); // added line!!!
Tada! No compile errors!
BTW, parashift's explanations on templates in C++ are the best IMHO.
Hope that helps.

Export QObject-based class to DLL

I'm composing a class which derives from QObject, and I want to export this class into a DLL file so other applications can use it. But I got some mysterious problem here:
The code is shown below:
mydll.h:
#ifndef MYDLL_H
#define MYDLL_H
#include "mydll_global.h"
#include <QObject>
#include <QDebug>
class MYDLLSHARED_EXPORT MyDll : public QObject
{
Q_OBJECT
public:
explicit MyDll(QObject * parent = 0);
void test() const;
};
#endif // MYDLL_H
mydll_global.h:
#ifndef MYDLL_GLOBAL_H
#define MYDLL_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(MYDLL_LIBRARY)
# define MYDLLSHARED_EXPORT Q_DECL_EXPORT
#else
# define MYDLLSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // MYDLL_GLOBAL_H
mydll.cpp:
#include "mydll.h"
MyDll::MyDll(QObject * parent) :
QObject(parent)
{
}
void MyDll::test() const {
qDebug() << "Hello from dll!";
}
and the dll is used in another application. The dll is compiled successfully. I've add LIBS += "myDll.dll" in the .pro file of the application using this dll, and I've copied myDll.dll to the working directory of the application.
The compiler reports:
C4273: "MyDll::qt_static_metacall" : inconsistent dll linkage.
C2491: "MyDll::staticMetaObject": definition of dllimport static data member not allowed
What's the problem here?
Your code for mydll_global.h checks whether MYDLL_LIBRARY is defined, but none of the code you have posted defines MYDLL_LIBRARY. Is this declared in a file that you have not shared on the question? If not, you need to add a #define MYDLL_LIBRARY in your build project, or your PCH.

File not found during compilation

What is wrong with the code bellow? When I compile it I get a warning that file not found. Something is invalid. I'm probably making a few mistakes here. I think the problem is perhaps with the way I inherit from QWidget.
#include <QtGui/QApplication>
#include "filedialogs.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FileDialogs w;
w.openFile();
return 0;
}
#ifndef FILEDIALOGS_H
#define FILEDIALOGS_H
#include <QWidget>
class QFileDialog;
class FileDialogs : public QWidget
{
public:
FileDialogs(QWidget *parent = 0);
~FileDialogs();
void openFile();
};
#endif // FILEDIALOGS_H
#include <QFileDialog>
#include "filedialogs.h"
FileDialogs::FileDialogs(QWidget *parent)
: QWidget(parent)
{
}
FileDialogs::~FileDialogs()
{
}
void FileDialogs::openFile()
{
QString filename = QFileDialog::getOpenFileName(
this,
tr("Open Document"),
QDir::currentPath(),
tr("Document files (*.doc *.rtf);;All files (*.*)") );
if( !filename.isNull() )
{
qDebug( filename.toAscii() );
}
}
#-------------------------------------------------
#
# Project created by QtCreator 2011-07-29T19:06:33
#
#-------------------------------------------------
QT += core gui
TARGET = exX
TEMPLATE = app
SOURCES += main.cpp\
filedialogs.cpp
HEADERS += filedialogs.h
This error message is emitted by the MOC compiler. You are missing the Q_OBJECT macro. Put it in your class declaration like this:
class FileDialogs : public QWidget
{
Q_OBJECT
public:
....
I know this Question is very old. But in my case it was another problem.
I had to include the path of the headers manually in the .pro file.
INCLUDEPATH += src/subdir

Resources