1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include <QtGui>
class myMsgHandler: public QObject
{
Q_OBJECT
myMsgHandler()
{
// important
qRegisterMetaType<QtMsgType>("QtMsgType");
}
public :
//accès au singleton pour la connection
static myMsgHandler & instance()
{
static myMsgHandler obj;
return obj;
}
static void MsgHandler(QtMsgType type, const char *msg)
{
QString s = msg;
emit instance().newMsg(type, s);
}
signals :
void newMsg(QtMsgType type, const QString &msg);
};
class maThread : public QThread
{
void run()
{
qDebug() << "Thread " <<QThread::currentThreadId();
}
};
class MaWidget : public QWidget
{
Q_OBJECT
QTextBrowser * txt;
maThread t;
public:
MaWidget()
{
QVBoxLayout * l = new QVBoxLayout(this);
txt = new QTextBrowser;
l->addWidget(txt);
QPushButton * but = new QPushButton("lancer thread");
l->addWidget(but);
connect(but,SIGNAL(clicked()),this,SLOT(runThread()));
connect(&myMsgHandler::instance(),SIGNAL(newMsg(QtMsgType , const QString &)),this,SLOT(receiveMsg(QtMsgType , const QString &)));
}
public slots:
void runThread()
{
t.start();
}
void receiveMsg(QtMsgType type, const QString &msg)
{
txt->append(msg);
}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qInstallMsgHandler(myMsgHandler::MsgHandler);
MaWidget w;
w.show();
return a.exec();
} |
Partager