Index: kdialog.cpp
===================================================================
--- kdialog.cpp	(revision 888795)
+++ kdialog.cpp	(working copy)
@@ -3,6 +3,7 @@
  *  Additions 1999-2000 by Espen Sand (espen@kde.org)
  *                      by Holger Freyther <freyther@kde.org>
  *            2005-2006 by Olivier Goffart (ogoffart at kde.org)
+ *            2008 by Sebastian Trueg (trueg at kde.org)
  *
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -35,6 +36,7 @@
 #include <QTimer>
 #include <QVBoxLayout>
 #include <QWhatsThis>
+#include <QtCore/QThread>
 
 #include <klocale.h>
 #include <kpushbutton.h>
@@ -1011,12 +1013,20 @@
     KDialogQueue *q;
     QList< QPointer<QDialog> > queue;
     bool busy;
-
 };
 
 KDialogQueue* KDialogQueue::self()
 {
   K_GLOBAL_STATIC(KDialogQueue, _self)
+
+  // we want KDialogQueue to be thread-safe, i.e. it should be possible
+  // to queue dialogs from any thread, not only the GUI thread. For that
+  // to work, the KDialogQueue has to live in the GUI thread. Only then
+  // the queued connections used below will end up there.
+  if(_self->thread() != QApplication::instance()->thread() &&
+     _self->thread() == QThread::currentThread())
+      _self->moveToThread(QApplication::instance()->thread());
+
   return _self;
 }
 
@@ -1037,7 +1047,14 @@
   KDialogQueue *_this = self();
   _this->d->queue.append( dialog );
 
-  QTimer::singleShot( 0, _this, SLOT( slotShowQueuedDialog() ) );
+  // move the dialog to the GUI thread
+  if(dialog->thread() != QApplication::instance()->thread() &&
+     dialog->thread() == QThread::currentThread())
+      dialog->moveToThread(QApplication::instance()->thread());
+
+  // We use a queued connection to be able to use queued dialogs
+  // from threads other than the GUI thread (example: KMessageBox)
+  QMetaObject::invokeMethod( _this, "slotShowQueuedDialog", Qt::QueuedConnection );
 }
 
 void KDialogQueue::Private::slotShowQueuedDialog()
@@ -1058,8 +1075,11 @@
   busy = false;
   delete dialog;
 
-  if ( !queue.isEmpty() )
-    QTimer::singleShot( 20, q, SLOT( slotShowQueuedDialog() ) );
+  if ( !queue.isEmpty() ) {
+      // We use a queued connection to be able to use queued dialogs
+      // from threads other than the GUI thread (example: KMessageBox)
+      QMetaObject::invokeMethod( q, "slotShowQueuedDialog", Qt::QueuedConnection );
+  }
 }
 
 #include "kdialog.moc"
