I want to use std::variant<Foo, Bar> as internal value of QVariant. How to do this?
#include <QCoreApplication>
#include <QMetaType>
#include <variant>
enum Foo{A, B};
enum class Bar{C, D};
Q_DECLARE_METATYPE(std::variant<Foo, Bar>);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
return a.exec();
}
I am getting this error:
error: macro "Q_DECLARE_METATYPE" passed 2 arguments, but takes just 1
Q_DECLARE_METATYPE(std::variant<Foo, Bar>);
^
The problem is Q_DECLARE_METATYPE is a macro, and macros don't like ,s.
You can either pass a type alias
using T = std::variant<Foo, Bar>;
Q_DECLARE_METATYPE(T)
or wrap the use in extra ()
Q_DECLARE_METATYPE((std::variant<Foo, Bar>));
Related
I Want to use MediaInfo.dll downloaded from here [DLL v0.7.94][1]
[1]: https://mediaarea.net/bg/MediaInfo/Download/Windows. My question is how to call some function in this .dll using Qt framework
#include <QCoreApplication>
#include <QLibrary>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if (QLibrary::isLibrary("MediaInfo.dll")) { // C:/MediaInfo.dll
QLibrary lib("MediaInfo.dll");
lib.load();
if (!lib.isLoaded()) {
qDebug() << lib.errorString();
}
if (lib.isLoaded()) {
qDebug() << "success";
}
}
return a.exec();
}
You have a good example in QLibrary documentation. Basically you have to know the function name (the symbol) and it's prototype.
#include <QCoreApplication>
#include <QLibrary>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if (QLibrary::isLibrary("MediaInfo.dll")) { // C:/MediaInfo.dll
QLibrary lib("MediaInfo.dll");
lib.load();
if (!lib.isLoaded()) {
qDebug() << lib.errorString();
}
if (lib.isLoaded()) {
qDebug() << "success";
// Resolves symbol to
// void the_function_name()
typedef void (*FunctionPrototype)();
auto function1 = (FunctionPrototype)lib.resolve("the_function_name");
// Resolves symbol to
// int another_function_name(int, const char*)
typedef int (*AnotherPrototypeExample)(int, const char*);
auto function2 = (AnotherPrototypeExample)lib.resolve("another_function_name");
// if null means the symbol was not loaded
if (function1) function1();
if (function2) int result = function2(0, "hello world!");
}
}
return a.exec();
}
You need to declare a function prototype and get a pointer to a function in DLL.
QLibrary myLib("mylib");
typedef void (*MyPrototype)();
MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol");
if (myFunction)
myFunction();
See more on QLibrary.
Why do you want to use QLibrary when there is a C/C++ binding?
Include file with functions prototypes.
Example with dynamic call of the DLL.
A bit hidden, but all is included in the the DLL zip package in the link you provided in your question.
Jérôme, developer of MediaInfo
I am starting to learn threads in the C++11 standard in Qt.I can't include library ,no such of directory For example, I have the following simple code:
#include <QCoreApplication>
#include <thread>
#include <iostream>
using namespace std;
void func();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThread th1;
th1.run();
return a.exec();
}
void func()
{
cout << "This string from thread!"<<endl;
}
On the second string of the code, I have an error. The compiler doesn't "see" , I know that i must "include" 11 standard, so i go to .pro and paste CONFIG += c++11, but it isn't helping me :C
Please, I need your help!
You try to use QThread subclass, but you said that you want to use C++11 thread so use this:
#include <thread>
#include <QDebug>
#include <QApplication>
#include <iostream>
void foo()
{
std::cout << "This string from thread!"<<endl;
//while(true)
//{
// qDebug() <<"works";
// Sleep(500);
//}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
std::thread t(foo);
t.join();
return a.exec();
}
Also next is wrong:
MyThread th1;
th1.run();
Should be:
MyThread th1;
th1.start();
Details about threading with Qt classes:
https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong
http://qt-project.org/doc/qt-5/thread-basics.html
http://qt-project.org/doc/qt-5/qtconcurrent-index.html
Given below is the code for embedding an application using QX11EmbedContainer.
#include "mainwindow.h"
#include <QApplication>
#include <QX11EmbedContainer>
#include <QProcess>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QX11EmbedContainer container;
container.show();
QProcess process(&container);
process.start("C:/Users/Administrator/Desktop.../.exe");
int status=a.exec();
process.close();
return status;
}
On running the application the error that I am getting is:
C:\Qt4.8.5\src\gui\kernel\qx11embed_x11.h:77: error: C2061: syntax error : identifier 'XEvent'
Where am I going wrong?
QX11EmbedContainer is X11-specific, it will not work under other systems (like Windows).
I'm implementing gtest now, and it gives me an error : main previously defined here.
Here's utest.cpp
// Bring in my package's API, which is what I'm testing
#include "../src/test.cpp"
// Bring in gtest
#include <gtest/gtest.h>
// Declare a test
TEST(TestSuite, testCase1)
{
EXPECT_EQ(5,getX(5));
}
// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
And here's the code that i'm testing
test.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <Project2Sample/R_ID.h>
#include <geometry_msgs/Twist.h>
#include <nav_msgs/Odometry.h>
#include <sensor_msgs/LaserScan.h>
#include <sstream>
#include "math.h"
int getX(int x)
{
return x;
}
int main(int argc, char **argv)
{
return 0;
}
There's nothing in test.cpp main but actual code will have some codes in main.
I dont have header files for utest and test cpp files
I tried
#ifndef UTEST_H
#define UTEST_H
and didn't solve the error.
The error message states what the problem is, you have two main() functions. I believe you want to remove the duplicate main() from test.cpp.
So now I have a
int main (int argc, char *argv[]){}
how to make it string based? will int main (int argc, std::string *argv[]) be enough?
You can't change main's signature, so this is your best bet:
#include <string>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<std::string> params(argv, argv+argc);
// ...
return 0;
}
If you want to create a string out of the input parameters passed, you can also
add character pointers to create a string yourself
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string passedValue;
for(int i = 1; i < argc; i++)
passedValue += argv[i];
// ...
return 0;
}
You can't do it that way, as the main function is declared explicitly as it is as an entry point. Note that the CRT knows nothing about STL so would barf anyway. Try:
#include <string>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<std::string> args;
for(int i(0); i < argc; ++i)
args.push_back(argv[i]);
// ...
return(0);
}; // eo main
That would be non-standard because the Standard in 3.6.1 says
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
No. That is not allowed. If present, it is mandated to be char *argv[].
BTW, in C++ main should always be declared to return 'int'
main receives char*. you will have to put the argv array into std::strings yourself.