Hey community,
I try to create something like a window pool. You can use these windows everywhere in your program to display graphics and plot diagrams etc. The widgets working well but the main problem at the moment is the frustrated tries, to create the pool. One non QObject-Object should represent a QMainWindow to cut the bindings to Qt.
I cannot create a widget -> I tried invokeMethode, connect and QTimer but nothing works. Sometimes the methods don´t get called or I am not in the gui thread... Any idea?
header:
#pragma once
#include <QMainWindow>
{
Q_OBJECT
public:
MyWindow();
signals:
void start();
private:
QWindow *mWin;
};
{
Q_OBJECT
public:
QWindowPool();
public slots:
void createWindow();
};
{
Q_OBJECT
};
#pragma once
#include <QMainWindow>
class MyWindow : QObject
{
Q_OBJECT
public:
MyWindow();
signals:
void start();
private:
QWindow *mWin;
};
class QWindowPool : public QObject
{
Q_OBJECT
public:
QWindowPool();
public slots:
void createWindow();
};
class QWindow : public QMainWindow
{
Q_OBJECT
};
To copy to clipboard, switch view to plain text mode
#include <utils/MatWindow.h>
#include <QApplication>
#include <QTimer>
#include <thread>
QWindowPool pool;
QWindowPool::QWindowPool()
{
int c = 0;
}
void QWindowPool::createWindow()
{
printf("window created\n");
new QWindow();
}
MyWindow::MyWindow()
{
connect(this, SIGNAL(start()), &pool, SLOT(createWindow()));
QTimer::singleShot(0,
&pool,
SLOT(createWindow
()));
QMetaObject::invokeMethod(&pool,
"createWindow", Qt
::BlockingQueuedConnection);
emit start();
}
int main()
{
std::thread t1([](){
MyWindow mw;
qApp
->processEvents
(QEventLoop::AllEvents);
// <- not working });
// qApp->processEvents(QEventLoop::AllEvents); // work
t1.join();
return 0;
}
#include <utils/MatWindow.h>
#include <QApplication>
#include <QTimer>
#include <thread>
QWindowPool pool;
QWindowPool::QWindowPool()
{
int c = 0;
new QApplication(c, NULL);
}
void QWindowPool::createWindow()
{
printf("window created\n");
new QWindow();
}
MyWindow::MyWindow()
{
connect(this, SIGNAL(start()), &pool, SLOT(createWindow()));
QTimer::singleShot(0, &pool, SLOT(createWindow()));
QMetaObject::invokeMethod(&pool, "createWindow", Qt::BlockingQueuedConnection);
emit start();
}
int main()
{
std::thread t1([](){
MyWindow mw;
qApp->processEvents(QEventLoop::AllEvents); // <- not working
});
// qApp->processEvents(QEventLoop::AllEvents); // work
t1.join();
return 0;
}
To copy to clipboard, switch view to plain text mode
So, it does not work (createWindow does not get called) unless I uncomment the last processEvents in the main thread but that should not be the solution.
What I want is in the final application: The user should get the possibility to write anywhere in his code and in any thread:
MyWindow mw(dataToDisplay)
MyWindow mw(dataToDisplay)
To copy to clipboard, switch view to plain text mode
and the window should be created and showed to him. Currently it only works if he would call:
qApp->processEvents(QEventLoop::AllEvents);
To copy to clipboard, switch view to plain text mode
in the main thread. But I want do do this for him.... HOW?
Thank you.
BTW: I "cross posted" this question. First I asked at stackoverflow but didn´t get a right solution. http://stackoverflow.com/questions/2...fferent-thread
Bookmarks