PDA

View Full Version : GUi or Not GUI - that is the question



bruccutler
23rd February 2007, 18:14
Hello,
I need to know what call I can make to determine whether I'm in a GUI or non-GUI thread. I have a utility function that displays a message that tells me whether my signals and slots really exist. This helps me know if I've incorrectly specified a signal or slot. The only problem is, when I go to display the message via QMessageBox::information(), if I'm in a non-gui thread I get the message that I can't create a GUI element in a non-gui thread.

How can I tell?

Here's the code

bool Util::myConnect(const QObject *from, const char *signal, const QObject *to, const char *slot)
{
if (!connect(from, signal, to, slot))
{
QString text = QString (tr("QT connect() API failed\nFrom class: %1\n\tSIGNAL: '%2\nTo class: %3\n\tSLOT: '%4'\n"))
.arg(from->metaObject()->className())
.arg(signal)
.arg(to->metaObject()->className())
.arg(slot);
// This doesn't work if we are not in the main GUI thread
// How can I check to see if we are in the GUI thread?
**** QMessageBox::critical(NULL, tr("Run-time coding API Error"), text);
return false;
}
return true;
}

- BRC

jpn
23rd February 2007, 18:19
if (QThread::currentThread() == qApp->thread())
{
// this is the main thread
}