C++ thread connect in Qt - c++

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

Related

How To Debug The Threw Exceptions From QThread?

Consider the following code(The system specification is mentioned down below):
#include <stdexcept>
#include <thread>
void f()
{
throw std::runtime_error("Something");
}
int main()
{
std::thread thr(f);
thr.join();
}
I easily could run it with a debugger and have the stack trace from where the exception threw. Like this:
But the same code fails if I use a QThread instead of std::thread:
#include <QCoreApplication>
#include <QThread>
void f()
{
throw std::runtime_error("Something");
}
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
auto const thr = QThread::create(f);
thr->start();
return QCoreApplication::exec();
}
Like this:
It's possible to debug it like std::thread? and have the stack frames from where the exception threw?
Environment:
OS: Fedora 35
Kernel: Linux 5.15.10
Compiler: GCC 11.2.1
Qt: 5.15.2
QtCreator: 6.0.1

Calling functions in a DLL from Qt (c++)

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

QX11EmbedContainer code error

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

count the number of film instances

Here is my implementation
Film.h //header
#ifndef FILM_H
#define FILM_H
#include <QString>
class Film {
protected:
QString title;
double dailyRate;
public:
Film(QString ti,double dr);
virtual double calculateRental(int num)const;
};
#endif // FILM_H
Film.cpp
#include "film.h"
#include <QString>
Film::Film(QString ti,double dr){
title=ti;
dailyRate=dr;
}
double Film::calculateRental(int num)const {
return dailyRate*num;
}
main.cpp
#include <QtCore/QCoreApplication>
#include <QtCore/QTextStream>
#include "film.h"
using namespace std;
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QTextStream cout(stdout, QIODevice::WriteOnly);
Film f("Top Gun", 10.00); //create an instance of a film
cout <<f.calculateRental(2);
return a.exec();
}
how do I count the number of Film instances created? I know is something like that:
static int numOfFilms;
numOfFilms++;
how do I use the code?
It depends. If you replace Class with class, and QString has a conversion constructor from const char*, then yes.

class extending GtkWindow

i'm trying to learn c++, but i can not find if it's possible to extend a class in this way:
main.cc
#include "mWindow.h"
using namespace std;
int main( int argc, char* argv[] ) {
gtk_init( &argc, &argv );
mWindow win = mWindow();
gtk_main();
return 0;
}
mWindow.cc
#include "mWindow.h"
mWindow::mWindow() {
gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (this, "my window");
gtk_widget_show_all (GTK_WIDGET(this));
}
mWindow.h
#ifndef MWINDOW_H_INCLUDED
#define MWINDOW_H_INCLUDED
#include <gtk/gtk.h>
using namespace std;
class mWindow : public GtkWindow {
public:
mWindow();
};
#endif
I suggest you take a look at gtkmm (http://www.gtkmm.org/) if you want to use GTK+ in conjunction with C++, i.e. there is no need to try to reinvent the wheel and write your own C++ interface for GTK+ (which is a C library).
thanks,
I was trying to use C libraries as if they were C++.
This is how I solved with gtkmm:
main.cc
#include <gtkmm/main.h>
#include "examplewindow.h"
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
ExampleWindow window;
Gtk::Main::run(window);
return 0;
}
examplewindow.h
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm-2.4/gtkmm.h>
class ExampleWindow : public Gtk::Window {
public:
ExampleWindow();
};
#endif //GTKMM_EXAMPLEWINDOW_H
examplewindow.cc
#include "examplewindow.h"
ExampleWindow::ExampleWindow() {
set_title("Gtk::TextView example");
set_border_width(5);
set_default_size(400, 200);
show_all_children();
}
also add the command to complete successfully, at least on Arch Linux:
g++ $(pkg-config --cflags --libs gtkmm-2.4) main.cc examplewindow.cc examplewindow.h -o executable
another small indication, what i shouldl use as dynamic arrays or vectors and for hashmap?