in your application
void TapeMan::customEvent( QCustomEvent *event )
{
if( event
->type
() == QEvent::User + 3 ) // display message box {
MessageBoxEvent *me = ( MessageBoxEvent* )event;
ThreadMessageBox *tm = me->tm;
if( me->kind == TMSG_WARN )
tm
->messageResult
= QMessageBox::warning( this, me
->caption, me
->message,
me->button0, me->button1, me->button2 );
if( me->kind == TMSG_INFO )
tm
->messageResult
= QMessageBox::information( this, me
->caption,
me->message, me->button0, me->button1, me->button2 );
if( me->kind == TMSG_STOP )
tm
->messageResult
= QMessageBox::critical( this, me
->caption, me
->message,
me->button0, me->button1, me->button2 );
}
}
and then 2 classes
#ifndef THREADMESSAGEBOX_H
#define THREADMESSAGEBOX_H
#include <qstring.h>
#include <qmessagebox.h>
class ThreadMessageBox
{
public:
ThreadMessageBox
( THREAD_MESSAGE kind,
const QString &caption,
const QString
& message,
int button0,
int button1
=QMessageBox::NoButton,
~ThreadMessageBox();
int messageResult;
};
#endif
#include "threadmessagebox.h"
#include "messageboxevent.h"
#include <qapplication.h>
extern TapeMan *gApp;
ThreadMessageBox
::ThreadMessageBox( THREAD_MESSAGE kind,
const QString &caption,
const QString& message, int button0, int button1, int button2 )
{
MessageBoxEvent *mess = new MessageBoxEvent( this, kind, caption, message,
button0, button1, button2 );
messageResult = -99999;
while( messageResult == -99999 ) // wait
{
sleep( 1 );
}
}
ThreadMessageBox::~ThreadMessageBox()
{
}
#ifndef MESSAGE_BOX_EVENT_H
#define MESSAGE_BOX_EVENT_H
#include <qevent.h>
#include <qmessagebox.h>
#include <qdeepcopy.h>
class ThreadMessageBox;
class MessageBoxEvent : public QCustomEvent
{
public:
MessageBoxEvent
( ThreadMessageBox
*tm, THREAD_MESSAGE kind,
const QString &caption,
const QString
& message,
int button0,
int button1
=QMessageBox::NoButton,
~MessageBoxEvent();
THREAD_MESSAGE kind; // kind of message box
QDeepCopy<QString> caption; // caption for message box
QDeepCopy<QString> message; // text for message
int button0; // buttons to display
int button1;
int button2;
ThreadMessageBox *tm; // pointer to thread message box
};
#endif
#include "messageboxevent.h"
MessageBoxEvent::MessageBoxEvent( ThreadMessageBox *tm, THREAD_MESSAGE kind,
const QString &caption,
const QString
& message,
int button0,
int button1,
int button2
) : QCustomEvent
( QEvent::User+3 ) {
this->kind = kind;
this->caption = caption;
this->message = message;
this->button0 = button0;
this->button1 = button1;
this->button2 = button2;
this->tm = tm;
}
MessageBoxEvent::~MessageBoxEvent()
{
}
to get a message box from your thread
ThreadMessageBox mess = ThreadMessageBox( TMSG_INFO, "Tape Done", "Are there any more tapes",
// do action here!
in your application
void TapeMan::customEvent( QCustomEvent *event )
{
if( event->type() == QEvent::User + 3 ) // display message box
{
MessageBoxEvent *me = ( MessageBoxEvent* )event;
ThreadMessageBox *tm = me->tm;
if( me->kind == TMSG_WARN )
tm->messageResult = QMessageBox::warning( this, me->caption, me->message,
me->button0, me->button1, me->button2 );
if( me->kind == TMSG_INFO )
tm->messageResult = QMessageBox::information( this, me->caption,
me->message, me->button0, me->button1, me->button2 );
if( me->kind == TMSG_STOP )
tm->messageResult = QMessageBox::critical( this, me->caption, me->message,
me->button0, me->button1, me->button2 );
}
}
and then 2 classes
#ifndef THREADMESSAGEBOX_H
#define THREADMESSAGEBOX_H
#include <qstring.h>
#include <qmessagebox.h>
class ThreadMessageBox
{
public:
ThreadMessageBox( THREAD_MESSAGE kind, const QString &caption, const
QString& message, int button0, int button1=QMessageBox::NoButton,
int button2=QMessageBox::NoButton );
~ThreadMessageBox();
int messageResult;
};
#endif
#include "threadmessagebox.h"
#include "messageboxevent.h"
#include <qapplication.h>
extern TapeMan *gApp;
ThreadMessageBox::ThreadMessageBox( THREAD_MESSAGE kind, const QString &caption,
const QString& message, int button0, int button1, int button2 )
{
MessageBoxEvent *mess = new MessageBoxEvent( this, kind, caption, message,
button0, button1, button2 );
messageResult = -99999;
QApplication::postEvent( gApp, mess ); // post event
while( messageResult == -99999 ) // wait
{
sleep( 1 );
}
}
ThreadMessageBox::~ThreadMessageBox()
{
}
#ifndef MESSAGE_BOX_EVENT_H
#define MESSAGE_BOX_EVENT_H
#include <qevent.h>
#include <qmessagebox.h>
#include <qdeepcopy.h>
class ThreadMessageBox;
class MessageBoxEvent : public QCustomEvent
{
public:
MessageBoxEvent( ThreadMessageBox *tm, THREAD_MESSAGE kind, const QString &caption,
const QString& message, int button0, int button1=QMessageBox::NoButton,
int button2=QMessageBox::NoButton );
~MessageBoxEvent();
THREAD_MESSAGE kind; // kind of message box
QDeepCopy<QString> caption; // caption for message box
QDeepCopy<QString> message; // text for message
int button0; // buttons to display
int button1;
int button2;
ThreadMessageBox *tm; // pointer to thread message box
};
#endif
#include "messageboxevent.h"
MessageBoxEvent::MessageBoxEvent( ThreadMessageBox *tm, THREAD_MESSAGE kind,
const QString &caption, const QString& message,
int button0, int button1, int button2 ) : QCustomEvent( QEvent::User+3 )
{
this->kind = kind;
this->caption = caption;
this->message = message;
this->button0 = button0;
this->button1 = button1;
this->button2 = button2;
this->tm = tm;
}
MessageBoxEvent::~MessageBoxEvent()
{
}
to get a message box from your thread
ThreadMessageBox mess = ThreadMessageBox( TMSG_INFO, "Tape Done", "Are there any more tapes",
QMessageBox::Yes, QMessageBox::No ); // will block until message box is closed
if( mess.messageResult == QMessageBox::Yes )
// do action here!
To copy to clipboard, switch view to plain text mode
Bookmarks