How do I detect if my application has been suspended (when someone changes applications) and react by deactivating my timer and then re-activating it when my application becomes un-suspended (when someone re-opens my half running app). Beneath Is how far I have coded so far on this part my app but it gives an error: "'QApplication::QApplication(const QApplication&)' is private" and it says within the context of myapp.cpp line 4. Please if anyone can tell me what I'm doing wrong it would be greatly appreciated.
Here is my code for the main.cpp:
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QObject>
#include <QGraphicsObject>
#include <QTimer>
#include <QVariant>
#include "timecontrol.h"
#include "scorecontrol.h"
#include "Retry.h"
#include <QEvent>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape);
viewer.setMainQmlFile(QLatin1String("qml/Raker/main.qml"));
viewer.showExpanded();
QObject *rootObject = viewer.rootObject();
QTimer *timmer = new QTimer;
timmer->setInterval(1000);
TimeControl *timcon = new TimeControl;
scorecontrol *scorer = new scorecontrol;
Retry *probeer = new Retry;
QObject::connect(timmer, SIGNAL(timeout()), timcon, SLOT(updateTime()));
QObject::connect(timcon, SIGNAL(setTime(QVariant)), rootObject, SLOT(setTime(QVariant)));
QObject::connect(rootObject, SIGNAL(blockClicked(int, int)), scorer, SLOT(checkRight(int, int)));
QObject::connect(scorer, SIGNAL(setScore(QVariant)), rootObject, SLOT(setScore(QVariant)));
QObject::connect(scorer, SIGNAL(setState(QVariant)), rootObject, SLOT(setState(QVariant)));
QObject::connect(rootObject, SIGNAL(start()), probeer, SLOT(Reetry()));
QObject::connect(probeer, SIGNAL(start()), timmer, SLOT(start()));
QObject::connect(probeer, SIGNAL(start(int)), scorer, SLOT(randomNum(int)));
QObject::connect(probeer, SIGNAL(sReset()), timcon, SLOT(reset()));
QObject::connect(probeer, SIGNAL(tReset()), scorer, SLOT(reset()));
QObject::connect(timcon, SIGNAL(timeOut()), scorer, SLOT(reset()));
QObject::connect(timcon, SIGNAL(setState(QVariant)), rootObject, SLOT(setState(QVariant)));
QObject::connect(timcon, SIGNAL(changeFinal()), scorer, SLOT(changeFinal()));
QObject::connect(scorer, SIGNAL(setFinal(QVariant)), rootObject, SLOT(setFinal(QVariant)));
return app.exec();
}
myApp.h:
#ifndef MYAPP_H
#define MYAPP_H
#include <QApplication>
#include <QObject>
class MyApp : public QApplication
{
public:
MyApp(QApplication &app);
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
#endif // MYAPP_H
myapp.cpp:
#include "myapp.h"
#include <QEvent>
MyApp::MyApp(QApplication &app) : QApplication(app)
{
installEventFilter(this);
}
bool MyApp::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::ApplicationDeactivate)
{
}
if (event->type() == QEvent::ApplicationDeactivate)
{
}
else
{
return false;
}
}
Add an event filter to check for the activate and deactivate events. From the QEvent documentation:
QEvent::ApplicationActivate 121 The application has been made available to the user.
QEvent::ApplicationDeactivate 122 The application has been suspended, and is unavailable to the user.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->comboBox->installEventFilter(this);
.
.
.
}
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::ApplicationDeactivate)
{
// Deactivate timer
}
if (event->type() == QEvent::ApplicationActivate)
{
// Turn timer back on
}
return false;
}
QEvent::AppliciationActivate and QEvent::ApplicationDeactivate have been deprecated (see here). Instead, use QEvent::ApplicationStateChange. There is no need to use an event filter to utilise this; just connect to QGuiApplication::applicationStateChanged(Qt::ApplicationState) and handle it as appropriate.
Related
I'm trying to use GLEW with QT in a QOpenGLWidget in Visual Studio.
Some details:
Visual Studio 2013
Glew 2.0.0 x64
QT 5.6.2 x64
I keep getting "Missing GL version" error when calling glewInit(). I've searched online a lot, and this problem seems to sometimes be caused by how the format is set (QSurfaceFormat), or how the functions create()/ makeCurrent() / doneCurrent() are used. But I can't seem to find a working solution. I'm still a bit confused about the whole QOpenGLContext thing also.
I manage to get the QOpenGLWidget work without GLEW, and using "old" gl functions (glBegin(), glEnd(), etc...). And I also get GLEW to work with GLFW3.
Is there something I seem to misunderstand in the code below?
My subclass of QOpenGLWidget
MyGLWidget.h
#pragma once
#include <QWidget>
#include "GL\glew.h"
#include <QOpenGLWidget>
#include <gl/GLU.h>
#include <gl/GL.h>
#include <iostream>
#include "qopenglcontext.h"
#include "loadShader.h"
class MyGLWidget : public QOpenGLWidget
{
public:
MyGLWidget(QWidget *parent = 0);
~MyGLWidget();
public:
void initializeGL() override;
void resizeGL(int w, int h) override;
void paintGL() override;
};
MyGLWidget.cpp
#include "MyGLWidget.h"
MyGLWidget::MyGLWidget(QWidget *parent)
: QOpenGLWidget(parent)
{
QSurfaceFormat glformat;
glformat.setVersion(3, 2);
glformat.setOption(QSurfaceFormat::DeprecatedFunctions);
glformat.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
glformat.setProfile(QSurfaceFormat::CompatibilityProfile);
QSurfaceFormat::setDefaultFormat(glformat);
this->setFormat(glformat);
create();
makeCurrent();
}
MyGLWidget::~MyGLWidget(){}
void MyGLWidget::initializeGL()
{
glewExperimental = TRUE;
GLenum err = glewInit();
if (GLEW_OK != err){
std::cout << "[Error] GLEW failed to initialize. " << (const char*)glewGetErrorString(err);
}
doneCurrent();
GLuint TextShader_ID = LoadShaders("Shaders/TextVertShader.vert", "Shaders/TextFragShader.frag");
}
void MyGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void MyGLWidget::resizeGL(int w, int h)
{
//...
}
main.cpp
#include "OGLQT_test2.h"
#include <QtWidgets/QApplication>
#include "GL\glew.h"
#include <QOpenGLFunctions>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
OGLQT_test2 w;
w.show();
return a.exec();
}
OGLQT_test2.h
#pragma once
#include "GL\glew.h"
#include <QtWidgets/QMainWindow>
#include "ui_OGLQT_test2.h"
#include "qopenglcontext.h"
class OGLQT_test2 : public QMainWindow
{
Q_OBJECT
public:
OGLQT_test2(QWidget *parent = Q_NULLPTR);
private:
Ui::OGLQT_test2Class ui;
};
OGLQT_test2.cpp
#include "OGLQT_test2.h"
#include "MyGLWidget.h"
OGLQT_test2::OGLQT_test2(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
MyGLWidget* glwidget = new MyGLWidget(this);
glwidget->setFixedHeight(400);
glwidget->setFixedWidth(500);
glwidget->move(50, 50);
glwidget->initializeGL();
glwidget->resizeGL(400,500);
glwidget->paintGL();
}
It seems that you cannot do this:
glformat.setVersion(x, y);
with GLEW. It works with QOpenGLFunctions, but apparently, it doesn't work with GLEW. You may not be able to do this, either:
glformat.setOption(QSurfaceFormat::DeprecatedFunctions);
glformat.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
glformat.setProfile(QSurfaceFormat::CompatibilityProfile);
Try removing the format.
Also, I think you're doing a little bit of a mess here:
glwidget->initializeGL();
glwidget->resizeGL(400,500);
glwidget->paintGL();
There is a simple way to do this, just do:
glwidget->resize(400,500);
glwidget->show();
resizeGL is an event called by resize. That goes the same for initializeGL() and paintGL() being called by show.
Another one is:
GLenum err = glewInit();
if (GLEW_OK != err){
std::cout << "[Error] GLEW failed to initialize. " << (const char*)glewGetErrorString(err);
}
doneCurrent();
Now why would you call doneCurrent here?
I want to display an error message whenever my independent thread encounters the word "alert1" in a specific .txt file. But I get the above error inside the monitorForAlerts() inside mythread.cpp file. The line expectedly executes if I were to place it inside dialog.cpp. So I guess this is due to non-inheritance of this object. Can you please advise me how to solve this error for the given code?
Here is the code:
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QtCore>
#include "mythread.h"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
public slots:
private:
Ui::Dialog *ui;
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
};
#endif // DIALOG_H
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QtCore>
#include <QDebug>
#include <QFile>
#include <Windows.h>
#include <QMessageBox>
#include <QTimer>
#define ALERTS_MESSAGE_STORAGE_PATH "E:\\QT1\\simpleGUIThread2\\simpleGUIThread2\\usbAlert.txt"
#define TIMER_VALUE 500
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0);
void run();
QString name;
void monitorForAlerts();
int exec();
public slots:
signals:
void testSignal(QString message);
public slots:
};
#endif // MYTHREAD_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked()
{
ui->label->show();
}
void Dialog::on_pushButton_2_clicked()
{
ui->label->hide();
}
mythread.cpp
#include "mythread.h"
#include "dialog.h"
MyThread::MyThread(QObject *parent) :
QThread(parent)
{
}
void MyThread::run()
{
exec();
}
int MyThread::exec()
{
while(1)
{
monitorForAlerts();
emit(testSignal("hello world!!"));
sleep(1);
}
}
void MyThread::monitorForAlerts()
{
QString response = ALERTS_MESSAGE_STORAGE_PATH;
QFile resp(response);
resp.open(QIODevice::WriteOnly);
resp.close();
QFile resp1(response);
char buf[121];
char buf1[] = "alert1";
char buf2[] = "alert2";
resp1.open(QIODevice::ReadOnly);
while(resp1.size() == 0)
{
Sleep(3000);
}
qint64 lineLength = resp1.readLine(buf, sizeof(buf));
resp1.close();
if(strcmp(buf,buf1) == 0)
{
QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
qDebug()<<"warning 1!!";
QMessageBox::critical(this,tr("ERROR"),tr("Large change in illumination.\nPlease re-capture reference image.\n"));
}
if(strcmp(buf,buf2) == 0)
{
QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
qDebug()<<"warning 2!!";
QMessageBox::critical(this,tr("ERROR"),tr("The camera position has been moved or an object is obscuring its view.\nPlease check the device.\n"));
}
}
main.cpp
#include "dialog.h"
#include <QApplication>
#include "mythread.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyThread mThread1;
mThread1.name = "mThread1";
mThread1.start();
Dialog w;
w.show();
return a.exec();
}
LATEST UPDATE*********************************************************************
Hi Zlatomir,
I choose to take your 1st advice. I have created a signal that the thread will emit and connect it to a slot for QDialog. Please let me know if my understanding is correct, because I do not know where to implement the connect(), since the signal is declared in mythread.h and the slot in dialog.h. The connection type argument for connect is Qt::QueuedConnection, so that gui elements from another thread different than main-thread.
are NOT created. Is this statement correct? and where do I place this?
connect( mThread, SIGNAL(alertSignal(QString)), this, SLOT(alertSlot(QString)), Qt::QueuedConnection);
mythread.h
//....
signals:
void alertSignal(QString message);
//....
dialog.h
//....
public slots:
void alertSlot(QString message);
//....
mythread.cpp
//....
if(strcmp(buf,buf1) == 0)
{
QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
qDebug()<<"warning 1!!";
emit(alertSignal("alert1"));
}
else if(strcmp(buf,buf2) == 0)
{
QFile::remove(ALERTS_MESSAGE_STORAGE_PATH);
qDebug()<<"warning 2!!";
emit(alertSignal("alert2"));
}
dialog.cpp
void Dialog::alertSlot(QString message)
{
if(strcmp(message, "alert1"))
QMessageBox::critical(this,tr("ERROR"),tr("Large change in illumination.\nPlease re-capture reference image.\n"));
else if(strcmp(message, "alert2"))
QMessageBox::critical(this,tr("ERROR"),tr("The camera position has been moved or an object is obscuring its view.\nPlease check the device.\n"));
}
Now if this were correct, how do i implement the connect() and in which file?
The first argument is the problem, in your case this is not a good argument, because there this is a pointer to a MyThread instance, and MyThread is not a QWidget (is not derived from QWidget).
To solve this you can show the QMessageBox::critical from a slot in mainwindow (the Dialog class in your code, there you pass the instance of main-window that is a QWidget) and connect that slot with a signal that you emit from your thread, make sure that the connection type argument for connect is Qt::QueuedConnection, so that you don't try to create gui elements from another thread different than main-thread.
Another option would be to validate the data before you start the second thread and to tell
the user that he needs to provide the right files.
LE: Also check the QThread's documentation for the recommended way to use the class, now it's recommended not to derive from QThread.
LE2 - answer to the update
That connect can be made where ever you can have the two instances that you want to connect, in your case main.cpp is a good place to connect those (don't forget to fully qualify the name for connect: QObject::connect):
//...
MyThread mThread1;
mThread1.name = "mThread1";
mThread1.start();
Dialog w;
QObject::connect( &mThread1, SIGNAL(alertSignal(QString)), &w, SLOT(alertSlot(QString)), Qt::QueuedConnection);
w.show();
//...
i compiled the following code for arm9 board. whenever i press any key the event should be detected.
keypress.ccp:
#include "keypress.h"
#include <QApplication>
#include <QKeyEvent>
KeyPress::KeyPress(QWidget *parent) :
QWidget(parent)
{
myLabel = new QLabel("LABEL");
mainLayout = new QVBoxLayout;
mainLayout->addWidget(myLabel);
setLayout(mainLayout);
}
void KeyPress::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_A)
{
myLabel->setText("You pressed A");
}
}
void KeyPress::keyReleaseEvent(QKeyEvent *event)
{
if(event->key())
{
qDebug()<<"key released";
}
if(event->key() == Qt::Key_A)
{
myLabel->setText("You released A");
}
}
keypress.h:
#ifndef KEYPRESS_H
#define KEYPRESS_H
#include <QWidget>
#include <QtGui>
class KeyPress : public QWidget
{
Q_OBJECT
public:
KeyPress(QWidget *parent = 0);
protected:
void keyPressEvent(QKeyEvent *);
void keyReleaseEvent(QKeyEvent *);
private:
QLabel *myLabel;
QVBoxLayout *mainLayout;
};
#endif // KEYPRESS_H
main.cpp
#include <QtGui>
#include "keypress.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug()<<"test";
KeyPress *keyPress = new KeyPress();
keyPress->show();
return a.exec();
}
using external key pad.
qt configuration:
/configure -embedded arm -xplatform qws/linux-arm-gnueabi-g++ -little-endian -qt-gfx-linuxfb -qt-gfx-qvfb -qt-kbd-tty -qt-kbd-qvfb -qt-kbd-linuxinput -qt-mouse-linuxinput -qt-mouse-qvfb -qt-mouse-pc -declarative -confirm-license
on board: export QWS_KEYBOARD="LinuxInput:/dev/input/event0"
when i run cat /dev/input/event0 | hexdump i am able to recieve keycodes for the key pressed. but when i run the the executable keypad is not responding. am i missing something? if so what else i should do to make the keypad work??
I derived a class from QTextEdit and use it as a "logbook". I equipped it with a slot to receive log-messages.
class CLogbook : public QTextEdit
{
Q_OBJECT;
public:
void log(QString msg) {append(msg)};
public slots:
void recvLogSignal(const QString message)
{
append("hallo");
std::cout << "signal received.\n";
log(message);
}
};
another class then emits a signal like this:
// in the header
signals:
void logMessage(const QString);
// in the implementation
emit logMessage("qt is cute");
std::cout << "if you can read this the logMessage was emitted\n";
and also i connect the signal to the slot
connect(tableeditor, SIGNAL(logMessage(const QString)), logbook, SLOT(recvLogSignal(const QString)));
However the message is never shown in the "logbook". What am i missing here?
SOLVED: The connect method was called after emitting the signal :-(
It is hard to see exactly what is wrong with your implementation without a full example. Sometimes signals or slots will fail if an object goes out of scope if it isn't initialized on the heap.
Another way that it could fail is if your QApplication hasn't reached the exec() call.
I haven't experimented with using const in signal and slot calls, and I haven't seen it in any examples before, so that could be causing the problem; but it seems to work fine in the example below.
Working Example With a Derived Class of QTextEdit
Here is a simple example I put together that does some basic logging with a push button and a line edit.
Here is the header:
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
//#include <QTextEdit>
#include "clogbook.h"
#include <QLineEdit>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget() {}
public slots:
void on_pushButton();
void on_lineEditReturn();
private:
CLogBook * text_edit_log;
QLineEdit * line_edit;
};
#endif // WIDGET_H
Here is the source:
#include "widget.h"
#include <QBoxLayout>
#include <QPushButton>
#include <QTimer>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QBoxLayout * box_layout = new QBoxLayout(QBoxLayout::TopToBottom);
text_edit_log = new CLogBook;
line_edit = new QLineEdit("Type Here and press Enter.");
QPushButton * push_button = new QPushButton("Click to Add To Log");
text_edit_log->setText("My Log Book");
box_layout->addWidget(text_edit_log);
box_layout->addWidget(line_edit);
box_layout->addWidget(push_button);
this->setLayout(box_layout);
QObject::connect(push_button, SIGNAL(clicked()), this, SLOT(on_pushButton()));
QObject::connect(line_edit, SIGNAL(returnPressed()), this, SLOT(on_lineEditReturn()));
}
void Widget::on_pushButton()
{
// text_edit_log->append("Push Button Logging Test");
text_edit_log->recvLogSignal("Push button test.");
}
void Widget::on_lineEditReturn()
{
// text_edit_log->append(line_edit->text());
text_edit_log->recvLogSignal(QString("LineEdit: ") + line_edit->text() );
line_edit->clear();
}
And here is the main:
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
And here is the CLogBook class:
#ifndef CLOGBOOK_H
#define CLOGBOOK_H
#include <QTextEdit>
#include <iostream>
class CLogBook : public QTextEdit
{
Q_OBJECT
public:
explicit CLogBook(QWidget *parent = 0) : QTextEdit(parent) { }
void log (QString msg) { append(msg); }
public slots:
void recvLogSignal(const QString message)
{
append("hallo");
std::cout << "signal received.\n" << std::endl;
log(message);
}
};
#endif // CLOGBOOK_H
Can someone tell me why this qt code will not call the Callback when ASYNC_TIMERS is defined (ie m_timer.start is called from the pthread but the slot never runs). Obviously it is to do with being called from a pthread as it works when ASYNC_TIMERS is not defined but I want to know how to fix it from the pthread. I have tryed many things found on the net including moveToThread(), calling the threads run which calls exec() but I have had no luck on this problem?
Cheers
multitimer.h:
#pragma once
#ifndef MULTI_TIMER_H
#define MULTI_TIMER_H
#include <QThread>
#include <QTimer>
#include <QMutex>
#include <QMap>
#include <QMetaType>
#include <cassert>
class MultiTimer : public QThread
{
Q_OBJECT
public:
typedef void (*multiTimerCallback)(quint32 p_id);
private:
QTimer m_timer;
QMutex m_mutex;
quint32 m_id;
multiTimerCallback m_callback;
void KillTimer(void);
public:
// only TimerFactory is allowed to instantiate MultiTimer
MultiTimer(quint32 p_id, multiTimerCallback p_callback);
~MultiTimer();
enum TTimerType
{
TT_SingleShot, ///< Timer fires only once
TT_Repetitive ///< Timer keeps firing repeatedly until stopped with KillTimer()
};
void SetTimer(quint32 p_delayInMilliseconds, MultiTimer::TTimerType timerType);
private slots:
void Update(void);
};
#endif
timer.cpp:
#include <QtCore/QCoreApplication>
#include "multitimer.h"
#include <stdio.h>
//--------------------------------------------------------------------------------------------------
void MultiTimer::SetTimer(quint32 p_delayInMilliseconds, MultiTimer::TTimerType timerType)
{
QMutexLocker locker(&m_mutex);
m_timer.setSingleShot(TT_SingleShot == timerType ? true : false);
m_timer.start(p_delayInMilliseconds);
//QTimer::singleShot(p_delayInMilliseconds, this,SLOT(Update()));
}
void MultiTimer::KillTimer(void)
{
QMutexLocker locker(&m_mutex);
m_timer.stop();
}
void MultiTimer::Update(void)
{
QMutexLocker locker(&m_mutex);
if (NULL != m_callback)
m_callback(m_id);
}
MultiTimer::MultiTimer(quint32 p_id, multiTimerCallback p_callback)
: m_id(p_id)
, m_callback(p_callback)
{
bool isConnected = true;
isConnected &= this->connect(&this->m_timer, SIGNAL(timeout()), this, SLOT(Update()), Qt::QueuedConnection);
assert(isConnected);
//this->start();
}
MultiTimer::~MultiTimer()
{
KillTimer();
wait();
}
//--------------------------------------------------------------------------------------------------
#define ASYNC_TIMERS
#define GLOBAL_TIMERS
void Callback(quint32 p_id)
{
printf("Got timered by timer %d.\n", p_id);
}
MultiTimer *mt;
void StartTimers(void)
{
#ifndef GLOBAL_TIMERS
mt = new MultiTimer(1, Callback);
#endif
mt->SetTimer(1000, MultiTimer::TT_SingleShot);
}
#ifdef ASYNC_TIMERS
pthread_t AsyncTaskThread;
void *ProcessAsyncTasks(void */*ptr*/)
{
StartTimers();
return NULL;
}
#endif
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
#ifdef GLOBAL_TIMERS
mt = new MultiTimer(1, Callback);
#endif
#ifdef ASYNC_TIMERS
pthread_create(&AsyncTaskThread, NULL, &ProcessAsyncTasks, NULL);
#else
StartTimers();
#endif
return a.exec();
}
I think Threads and QObjects has the answer: you can't use event-driven objects in a different thread than where you created it.
In your code, if GLOBAL_TIMERS is enabled, you'll be creating the MultiTimer in your main
thread, but calling m_timer.start() in a different one.
Quote the docs:
Event driven objects may only be used in a single thread. Specifically, this applies to the timer mechanism and the network module. For example, you cannot start a timer or connect a socket in a thread that is not the object's thread.
So don't do that. (And use QThread, while you're at it.)
You need a QEventLoop to process signal/slot stuff in the new thread.
QTimer needs those to work.
void *ProcessAsyncTasks(void */*ptr*/)
{
QEventLoop loop;
StartTimers();
loop.exec();
return NULL;
}
Why not use QThread?
OK for anyone also trying to solve this, I think I figured this out, if I move the m_timer and multitimer objects back to the main qt thread and emit the timer start signal "properly" it all seems to work (see below).
Possibly I could still make a exec call in the qthread to take over from the pthread and move the multitimer and m_timer to that, but this way works for now and I get my "Got timered by timer %d.\n" output on time even after the pthread is dead as desired.:
Thanks all for the input, if there is a better way to do this or a massive mistake I have overlooked it would be good to know? Cheers
multitimer.h:
/**
#file multitimer.h
#brief Partial implementation of Windows-like SetTimer()/KillTimer() for Qt.
*/
#pragma once
#ifndef MULTI_TIMER_H
#define MULTI_TIMER_H
#include <QThread>
#include <QTimer>
#include <QMutex>
#include <QMap>
#include <QMetaType>
#include <cassert>
class MultiTimer : public QThread
{
Q_OBJECT
public:
typedef void (*multiTimerCallback)(quint32 p_id);
private:
QTimer m_timer;
QMutex m_mutex;
quint32 m_id;
multiTimerCallback m_callback;
void KillTimer(void);
public:
// only TimerFactory is allowed to instantiate MultiTimer
MultiTimer(quint32 p_id, multiTimerCallback p_callback);
~MultiTimer();
enum TTimerType
{
TT_SingleShot, ///< Timer fires only once
TT_Repetitive ///< Timer keeps firing repeatedly until stopped with KillTimer()
};
void SetTimer(quint32 p_delayInMilliseconds, MultiTimer::TTimerType timerType);
private slots:
void Update(void);
signals:
void start_sig(int);
};
#endif
timer.cpp:
#include <QtCore/QCoreApplication>
#include "multitimer.h"
#include <stdio.h>
//--------------------------------------------------------------------------------------------------
void MultiTimer::SetTimer(quint32 p_delayInMilliseconds, MultiTimer::TTimerType timerType)
{
QMutexLocker locker(&m_mutex);
m_timer.setSingleShot(TT_SingleShot == timerType ? true : false);
connect(this, SIGNAL(start_sig(int)), &m_timer, SLOT(start(int)), Qt::QueuedConnection);
//m_timer.start(p_delayInMilliseconds);
emit start_sig(p_delayInMilliseconds);
disconnect(this, SIGNAL(start_sig(int)), &m_timer, SLOT(start(int)));
}
void MultiTimer::KillTimer(void)
{
QMutexLocker locker(&m_mutex);
m_timer.stop();
}
void MultiTimer::Update(void)
{
QMutexLocker locker(&m_mutex);
if (NULL != m_callback)
m_callback(m_id);
}
MultiTimer::MultiTimer(quint32 p_id, multiTimerCallback p_callback)
: m_id(p_id)
, m_callback(p_callback)
{
bool isConnected = true;
isConnected &= this->connect(&this->m_timer, SIGNAL(timeout()), this, SLOT(Update()), Qt::QueuedConnection);
assert(isConnected);
//this->start();
moveToThread(qApp->thread());
m_timer.moveToThread(qApp->thread());
}
MultiTimer::~MultiTimer()
{
KillTimer();
wait();
}
//--------------------------------------------------------------------------------------------------
#define ASYNC_TIMERS
#define xGLOBAL_TIMERS
void Callback(quint32 p_id)
{
printf("Got timered by timer %d.\n", p_id);
}
MultiTimer *mt;
void StartTimers(void)
{
#ifndef GLOBAL_TIMERS
mt = new MultiTimer(1, Callback);
#endif
mt->SetTimer(2000, MultiTimer::TT_SingleShot);
}
#ifdef ASYNC_TIMERS
pthread_t AsyncTaskThread;
void *ProcessAsyncTasks(void */*ptr*/)
{
StartTimers();
return NULL;
}
#endif
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
#ifdef GLOBAL_TIMERS
mt = new MultiTimer(1, Callback);
#endif
#ifdef ASYNC_TIMERS
pthread_create(&AsyncTaskThread, NULL, &ProcessAsyncTasks, NULL);
#else
StartTimers();
#endif
return a.exec();
}