Qt loads dll objects into separate thread, why? - c++

I am having some issues with starting a timer-based object from a dll with the event loop. Therefore I made a minimal example and this is my output:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is MainWindow(0x28fe08), parent's thread is QThread(0x1222aab0), current thread is QThread(0x121421c8)
QObject::startTimer: Timers can only be used with threads started with QThread
MainWindow thread: 0x1222aab0
DllThreadTest thread: 0x121421c8
I am using Windows 8 and Qt5.3.2.
So my questions are:
Why is the object that I create from the dll inside a different thread than the MainWindow? As you can see in the code, I am not using threads.
I can not pass this as a parameter when creating the object from my library as you can see in the first error message. See my example below.
I guess that when I solve the thread-issue, I also solve the timer-problem.
Example:
dllthreadtest.h
#ifndef DLLTHREADTEST_H
#define DLLTHREADTEST_H
#include <QObject>
#include <QTimer>
#if defined TEST_BUILD_DLLTHREADTEST
#define TEST_COMMON_DLLSPEC Q_DECL_EXPORT
#else
#define TEST_COMMON_DLLSPEC Q_DECL_IMPORT
#endif
class TEST_COMMON_DLLSPEC DllThreadTest : public QObject
{
Q_OBJECT
public:
explicit DllThreadTest(QObject *parent = 0);
signals:
public slots:
void onTimeOut();
private:
QTimer timer;
};
#endif // DLLTHREADTEST_H
dllthreadtest.cpp
#include "dllthreadtest.h"
#include <QDebug>
DllThreadTest::DllThreadTest(QObject *parent) :
QObject(parent)
{
timer.start(1000);
}
void DllThreadTest::onTimeOut()
{
qDebug()<<"time out";
}
DllThreadTest.pro
QT += core
QT -= gui
TARGET = DllThreadTest
CONFIG += console
CONFIG -= app_bundle
CONFIG += debug_and_release build_all
CONFIG(debug, debug|release) {
win32: TARGET = $$join(TARGET,,,d)
}
TEMPLATE = lib
DEFINES += TEST_BUILD_DLLTHREADTEST
SOURCES += main.cpp \
dllthreadtest.cpp
HEADERS += \
dllthreadtest.h
I am building it and then including the library in a newly created default project that just creates a QMainWindow. There I instantiate a DllThreadTest-object.
DllImportTest.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = DllImportTest
TEMPLATE = app
INCLUDEPATH += \
$$PWD/libs/
win32{
LIBS += -L$$PWD/libs/ -ldllthreadtest
}
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
maindwindow.cpp (only class of DllImportTest.pro)
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dllthreadtest.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),
this,SLOT(onButtonClick()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onButtonClick()
{
dllThreadTest_=new DllThreadTest;
qDebug()<<"MainWindow thread:"<<this->thread();
qDebug()<<"DllThreadTest thread:"<<dllThreadTest_->thread();
}
main.cpp (entry point of DllImportTest)
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

Related

Qudpsocket readyread() don't work in try-catch block

I wanna get message by UDP protocol.
If I create object to work with QUdpsocket in try-catch block, signal readyread() don't work.
But if I created UDPworker's object out of try-catch block - all OK.
What I did uncorrect in exceptions and Qt combo?
Is it about implementation QUdpsocket?
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//MyUDP m_UDP; // its work
try
{
MyUDP m_UDP; // its not work! WHY?!
}
catch(...)
{
qDebug()<< "Unknown exception cautch!!!" << endl;
}
return a.exec();
}
pr.pro
QT += core gui network
CONFIG += c++14 console
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
MyUDP::MyUDP(QObject *parent) :
QObject(parent)
{
socket = new QUdpSocket(this);
socket->bind(QHostAddress::LocalHost, 6650);
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
//Base class QObject
class MyUDP : public QObject
{
Q_OBJECT
public:
explicit MyUDP(QObject *parent = 0);
void SayHello();
public slots:
void readData(){
QMessageBox::information(0, "Внимание","Это очень важный текст", 0,0,0);
}
private:
QUdpSocket *socket;
};
#endif // MAINWINDOW_H
You are creating MyUDP inside a try block, not catching any error an getting out of the try/catch scope, so MyUDP is begin destroyed:
try {
MyUDP m_UDP; // Created in the stack, valid only inside the try block
}
catch (...)
{
}
//m_UDP is already destroyed and undefined here
I should add that you are using QObjects, and signaling/slots mechanism not in intended/orthodox way, but that is beyond your question. You should read the documentation about the QObject and signal/slot mechanism, Qt has plenty of information both on-line and in the QtAssistant application.

Trouble on Qt Creator on macOS - ld: library not found for -lQt5Widgets_debug

I installed Qt Creator 4.5.0 based on Qt5.10.0 and I made a simple project.
#include "widget.h"
#include <QApplication>
#include <QTextEdit>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextEdit textEdit;
textEdit.show();
return a.exec();
}
I built this project and got this message.
ld: library not found for -lQt5Widgets_debug
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I use macOS10.13.2, Xcode9.2. What should I do for this?
I must install some libraries for this?
.pro is below.
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Test1
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
widget.h is below.
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp is below.
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
Thank you for watching this.
Its likely that the files widget.h and widget.cpp do NOT exist in your project, which might cause this issue. and anyway your project is not using them.
In order to run the piece of code you written in main.cpp your project .pro need not be more than this:
#-------------------------------------------------
#
# Project created by QtCreator 2018-01-15T00:00:06
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Test1
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
HEADERS += \
FORMS += \
And remove #include widget.h from main.cpp because you are not using widget.cpp and widget.h for anything.

QWebEnginePage abnormal CPU usage

On windows 7 pro x64 with Qt 5.6.1 this line alone is bringing my CPU up to 10-15%:
QWebEnginePage *page = new QWebEnginePage(this);
Deleting the page later (even immediately) does not help in reducing the usage back to 0-1% as usual for my apps. Any ideas what to do?
Full code causing the problem:
problem.pro
QT += core gui webenginewidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = problem
TEMPLATE = app
SOURCES += main.cpp\
MainWindow.cpp
HEADERS += MainWindow.h
FORMS += MainWindow.ui
problem.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWebEnginePage>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
problem.cpp
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Comment the next line to reduce CPU usage back to normal
QWebEnginePage *page = new QWebEnginePage(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
With QNetworkAccessManager I was able to achieve the same result as with the QWebEnginePage, but without the unnecesarry raised CPU usage.

Error while promoting Widget container in Qt

I've created my own class in QT 5.4 with header and source file, then I've implemented there a video player. The problem is, that it's working fine when I'm placing it in the center of the main window or when I'm showing it in a separate window, but when I'm trying to create a Widget container in the main window and promote widget to it, it gives me some errors:
'videowindow' does not name a type
'widget' was not declared in this scope
expected type-specifier before 'videowindow'
So, here is the code:
videowindow.h - header of my implemented class
#ifndef VIDEOWINDOW_H
#define VIDEOWINDOW_H
#include "mainwindow.h"
class videoWindow
{
private:
QMediaPlayer *player;
QMediaPlaylist *playlist;
public:
videoWindow();
void video_enable();
void widget();
};
#endif // VIDEOWINDOW_H
videowindow.cpp - source
#include "videowindow.h"
#include "mainwindow.h"
videoWindow::videoWindow()
{
}
void videoWindow::video_enable()
{
player = new QMediaPlayer;
playlist = new QMediaPlaylist;
playlist->addMedia(QUrl::fromLocalFile("D:\\фото\\video\\P1150277.MOV"));
QVideoWidget *videoWid = new QVideoWidget;
player->setVideoOutput(videoWid);
videoWid->show();
player->setPlaylist(playlist);
playlist->setCurrentIndex(1); // start from a first video in the playlist
player->play();
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtMultimedia>
#include <QtMultimediaWidgets>
#include <QtDebug>
#include <string>
#include <QGraphicsVideoItem>
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 "videowindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include "videowindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
InstantPlayer.pro - .pro file of the project
#-------------------------------------------------
#
# Project created by QtCreator 2016-06-24T11:37:26
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = InstantPlayers
TEMPLATE = app
#
QT += multimedia
QT += multimediawidgets
#
SOURCES += main.cpp\
mainwindow.cpp \
videowindow.cpp \
playerbase.cpp
HEADERS += mainwindow.h \
videowindow.h \
playerbase.h
FORMS += mainwindow.ui \
playerbase.ui

QtCreator: Loading External Library

So I've been developing a small ACARS (plane tracking system) for the VATSIM network and apart of this entails using the FSUIPC library to interact with Flight Sim X. I have the program working beautifully in VS2010 but when I attempt to use any functions from FSUIPC I get the error:
"mainwindow.obj:-1: error: LNK2019: unresolved external symbol FSUIPC_Open referenced in **function "public: void __cdecl MainWindow::connectFSUIPC(void)" (?connectFSUIPC#MainWindow##QEAAXXZ)"**
From some research I've learnt that this is a library load error and for the life of me I can't figure out how to get it working. Below is the code for all files I am using.
AcarsTest.pro
#-------------------------------------------------
#
# Project created by QtCreator 2013-12-16T23:56:06
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = AcarsTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += "D:/Windows/QT/Tools/QtCreator/bin/AcarsTest/"
LIBS += -LD:/Windows/QT/Tools/QtCreator/bin/AcarsTest/ -lFSUIPC_User
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <Windows.h>
#include <FSUIPC_User.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void connectFSUIPC();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp
#include <Windows.h>
#include "mainwindow.h"
#include <QApplication>
#include "FSUIPC_User.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connectFSUIPC()
{
DWORD dwResult;
if(FSUIPC_Open(SIM_ANY, &dwResult))
{
}
}
try change code like this
your code:
LIBS += -LD:/Windows/QT/Tools/QtCreator/bin/AcarsTest/ -lFSUIPC_User
replace by
win32:LIBS += "D:/Windows/QT/Tools/QtCreator/bin/AcarsTest/FSUIPC_User.lib"