Getting QInputDialog::getText() result from C code running in non-GUI thread
I have previously solved a similar problem in this question, where I asked how to display a QMessageBox from a non-GUI thread in Qt running C code.
The solution I was suggested, to use signals & slots and a Qt::QueuedConnection to connect a signal that could be emitted by the C code calling a C++ function that in turn called a C++ method of my mai开发者_开发技巧n GUI window which then emitted the signal, works fine for QMessageBoxes.
However, now I also need to be able to request input from the user from a QInputDialog, from the C code running in the non-GUI thread.
I have thought about using the following slot code and the same principle described above:
void MyWidget::prompt_slot(QString str, QString &answer)
{
answer = QInputDialog::getText(0, "", str);
}
and a Qt::BlockingQueuedConnection, so that the slot modifies the reference to a string that I can then access from the C code.
Unfortunately I get the following runtime error:
QObject::connect: Cannot queue arguments of type 'QString&'
(Make sure 'QString&' is registered using qRegisterMetaType().)
What am I doing wrong? Will by approach even work? If not, what should I use?
Thanks a lot for your answer.
It ought to work if you use QString*
instead of QString&
. You may have to also register QString*
as a metatype - or, if you can't be bothered doing that, use void*
instead and cast back and forth.
精彩评论