Pregunta

Este es el subobject de QTread ... y concreto en el hilo principal ...

El error de tiempo de ejecución de la siguiente manera:

Afirmación de falla en Qcoreapplication :: SendEvent: "No se pueden enviar eventos a Objetos propiedad de un hilo diferente.Hilo actual 176F0A8.Receptor '' (de tipo 'MainWindow') se creó en el tema 3976A0 ", archivo C: \ NDK_BUILDREPOS \ QT-Desktop \ SRC \ Corelib \ Kernel \ Qcoreapplication.cpp, Línea 405 Parámetro inválido Pasado a C Función de tiempo de ejecución.Inválido Parámetro pasado a la función de tiempo de ejecución C.

class PaintThread : public QThread {

private:
    QWidget* parent;

public:
    ~PaintThread() {}

    PaintThread(QWidget* parent = 0) {
        this->parent = parent;
    }

    void run() {
        while (1) {
            this->msleep(5000);
            parent->repaint();
        }
        this->exec();
    }
};

Este es el constructor de mainframe:

MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
tankPoint = new QRect(50, 50, 30, 30);

this->show();

PaintThread * pt = new PaintThread(this);
pt->start();
}

El siguiente es el anulación de PaintEvent para MainWindow

void paintEvent(QPaintEvent*) {
    QPainter  p(this);

    p.setPen(Qt::red);
    p.setBrush(Qt::red);
    p.drawEllipse(*tankPoint);

    tankPoint->setLeft(200);
}

¿Puede alguien decirme por qué?

¿Fue útil?

Solución

The parent (in this case your MainWindow) lives in a different thread. According to Qt documentation

You can manually post events to any object in any thread at any time using the thread-safe function QCoreApplication::postEvent(). The events will automatically be dispatched by the event loop of the thread where the object was created. Event filters are supported in all threads, with the restriction that the monitoring object must live in the same thread as the monitored object. Similarly, QCoreApplication::sendEvent() (unlike postEvent()) can only be used to dispatch events to objects living in the thread from which the function is called.

So as a solution I would propose the following:

  • Define a signal in your PaintThread class
  • connect this signal to the paint() slot in QWidget subclass
  • Emit it in the run() function
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top