ich habe ein Problem bezüglich Threads in DLLs und Threads in einer QtApplication.
Ich besitze zwei verschiedene Kameras von zwei verschiedenen Herstellern deren Schnittstellen unterschiedlich sind. Aus diesem Grund habe ich zwei DLLs erstellt, welche nach außen identische Schnittstellen besitzen.
In meiner Hauptapplikation benutze ich QLibrary um die DLL einzubindne und .resolve() für das Casten der Schnittstellen.
Innerhalb der DLL benötige ich einen Thread, um dauerhaft Bilder aufzunehmen (live) und diese bereitzustellen. Meinem Wissenstand nach kann ich keine SIGNAL/SLOT-Beziehung zwischen den aufgelösten Schnittstellen und meinem Hauptprogramm herstellen, weil SIGNALE aus der DLL nicht aufgelöst werden können. Deshalb würde ich gerne einen weiteren Thread im Hauptprogramm erstellen, der die DLL dauerhaft nach neuen Daten fragt.
Das Problem an der Sache ist, dass der DLL-Thread die gesamte GUI des Hauptprogramms blockiert - woran liegt das?
Nachfolgend ein verkürztes Beispiel, wie meine Implementierung aussieht, um mir notfalls die Fehler direkt um die Ohren zu hauen oder ihr habt eine ganz andere Herrangehensweise für mich.
---DLL-Projekt-(Start)---
camera.h
Code: Alles auswählen
class Camera: public QObject
{
Q_OBJECT
public:
explicit Camera(QObject *parent = 0);
public slots:
void isImageAvailable( );
//much more functions
private:
bool imageFinished;
};
Code: Alles auswählen
Camera::Camera( QObject *parent ) : public QObject( parent ){
imageFinished = false;
}
bool Camera::isImageAvailable( ){
return imageFinished;
}
//much more functions
Code: Alles auswählen
#if defined(DLL_LIBRARY)
# define DLLSHARED_EXPORT Q_DECL_EXPORT
#else
# define DLLSHARED_EXPORT Q_DECL_IMPORT
#endif
QThread *thread;
Camera *camera;
extern "C" DLLSHARED_EXPORT bool initObjects( );
extern "C" DLLSHARED_EXPORT bool isImageAvailable( );
//many other functions
Code: Alles auswählen
void initObjects( ){
thread = new QThread( 0 );
camera = new Camera( 0 );
camera ->moveToThread( thread );
thread->start( );
}
bool isImageAvailable( ){
return camera->isImageAvailable( );
}
//much more functions
---Hauptprogramm-(Start)---
dllinc.h
Code: Alles auswählen
class DllInc : public QObject
{
Q_OBJECT
public:
explicit DllInc(QObject *parent = 0);
public slots:
void startIt( );
private:
typedef void (*initObjectsPrototype)(bool);
initObjectsPrototype initObjectsFnc;
typedef void (*isImageAvailablePrototype)(bool);
isImageAvailablePrototype isImageAvailableFnc;
QLibrary library;
};Code: Alles auswählen
DllInc::DllInc(QObject *parent) : QObject(parent){
library.setFileName( "TestDll" );
library.load( );
if( library.isLoaded( ) ){
initObjectsFnc = (initObjectsPrototype) library.resolve("initObjects");
isImageAvailableFnc = (isImageAvailablePrototype) library.resolve("isImageAvailable");
}
}
void DllInc::startIt( ){
this->initObjectsFnc( );
while( this->isImageAvailableFnc( ) == false ){
this->thread()->wait( 200 );
}
//get image now
}
Code: Alles auswählen
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
testBtn1 = new QPushButton("GetImage",this);
dllInc = new DllInc( 0 );
connect(testBtn1,SIGNAL(clicked()),dllInc ,SLOT(startIt()));
thread = new QThread( this );
dllInc->moveToThread( thread );
thread->start();
}
Grüße,
Pixtar