stribika got it almost right:
QMetaObject::invokeMethod( textEdit, "append", Qt::QueuedConnection,
Q_ARG( QString, myString ) );
cjhuitt's right, though: You usually want to declare a signal on the thread and connect it to the append()
slot, to get object lifetime management for free (well, for the price of a minor interface change). On a sidenote, the additional argument:
Qt::QueuedConnection ); // <-- This option is important!
from cjhuitt's answer isn't necessary anymore (it was, in Qt <= 4.1), since connect()
defaults to Qt::AutoConnection
which now (Qt >= 4.2) does the right thing and switches between queued and direct connection mode based on QThread::currentThread()
and the thread affinity of the receiver QObject
at emit time (instead of sender and receiver affinity at connect time).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…