QT multi-hilo de comunicación EMIT

cabecera:

#ifndef HEAD_H

#define HEAD_H
# include <QWidget>
#include <QThread>
#include <QMessageBox>
#include <QApplication>
#include <QDebug>

clase MyThread: QThread pública
{
    Q_OBJECT
pública:
    MyThread ();
    void run ();
señales:
    Enviar vacío (QString s);
};

clase MyWidget: QWidget pública {
    Q_OBJECT
pública:
    MyWidget (QWidget * parent = 0);
    ~ MyWidget ();
ranuras públicas:
    vacío receiveslot (QString s);
};
#endif // HEAD_H



CPP文件: # include "head.h"

MyThread :: MyThread ()
{
}
vacío MyThread :: run ()
{
    while (true)
    {
    sueño (5);
    Emit enviar ( "Este es el hilo hijo");
    qDebug () << "El hilo está funcionando!";
    }
// exec ();
    QThread :: run ();
}


MyWidget :: MyWidget (* QWidget padre): QWidget (padre)
{

}
MyWidget :: ~ MyWidget ()
{
}
vacío MyWidget :: receiveslot (QString s)
{
QMessageBox :: información (0, "Información", s);
}


Int main (int argc, char * argv [])
{
    QApplication un (argc, argv);
    MyWidget w;
    w.show ();
    MyThread * MTH = new MyThread;
    QObject :: connect (MTH, SEÑAL (send (QString)), y w, SLOT (receiveslot (QString)));
    mth-> start ();
    volver a.exec ();
}

PRO archivo:

QT + = núcleo GUI

greaterThan (QT_MAJOR_VERSION, 4): QT + = widgets de
CONFIG + = c ++ 11

TARGET = untitled35


TEMPLATE = app

SOURCES + = main.cpp

HEADERS + = \
    head.h




Publicado 12 artículos originales · ganado elogios 6 · Vistas a 20000 +

Supongo que te gusta

Origin blog.csdn.net/huaweizte123/article/details/52744507
Recomendado
Clasificación