Solved Simple QMessageBox changing parts of program...?
-
Hello, I have come across some unexpected behavior in my Qt application which I inherited from another developer.
Calling this line causes some weird behavior which does not make any sense to me.
QMessageBox::critical(this, QObject::tr("Warning"), QObject::tr(tab->getWarning().toStdString().c_str()));
I put breakpoint before and after this line, and have found out that it is indeed this line which is causing unexpected changes. Once this line is executed, part of my application changes in a way that I never coded.
Shouldn't this line just display the messagebox, and nothing else?
I tried taking the tab->getWarning().... line out and calling it on its own, but nothing out of the ordinary happens. The weird behavior ONLY occurs when the QMessage box line is executed as shown.
My best guess is that there is a signal/slot combo hooked up somewhere that signals when a message box like this is shown? I can't find where that happens in the code though...
Thanks for any help or ideas.
-
I think I found the problem...and it was from a completely different section of the code...Lol. There is really no point of posting the answer though, as it has nothing to do with the QMessageBox.
Basically there was a block of code inside an if statement prior to where the QMessageBox was shown, that wasn't being executed until the QMessageBox appeared. Weird if you ask me, but I'm sure the original writer of this code would understand.
-
@MaxDevI said in Simple QMessageBox changing parts of program...?:
My suspicions fall onto this statement:tab->getWarning().toStdString().c_str()
What does
tab->getWarning()
return (not the type, but the value)?
Meanwhile, try something like this:QByteArray text = tab->getWarning().toLatin1(); QMessageBox::critical(this, QObject::tr("Warning"), QObject::tr(text.constData()));
-
@kshegunov It is supposed to return a QString member variable from the tab's class.
If I do
qDebug() << tab->getWarning().toStdString().c_str())
before the QMessagebox::critical, it prints the string just fine without unwanted changes.
I can even do
qDebug() << tab->getWarning()
-
@MaxDevI
I'd move the translation in thegetWarning()
method, where it's supposed to be in the first place, and would just pass theQString
to the message box.QMessageBox::critical(this, QObject::tr("Warning"), tab->getWarning());
whereas:
QString MyClass::getWarning() { return QObject::tr("This is a translated warning text"); }
-
@kshegunov Thanks, same problem occurring though.
Is there somehow a slot/signal could have been connected which triggers on the QMessageBox appearing?
-
@MaxDevI said in Simple QMessageBox changing parts of program...?:
Is there somehow a slot/signal could have been connected which triggers on the QMessageBox appearing?
No, but the text might be different from what you see in the source, depending on the translation file.
-
@MaxDevI What is actually this "unexpected behavior"?
To me it is completely unclear: does your application crash or something else? -
@jsulm Normally, I make the buttons turn "red" when a certain condition is met, and this happens when the QMessageBox is shown. I really am confused on how this line would effect anything! I must be missing some sort of hidden function call...
-
So this QMessageBox section is triggered by a different condition, not the same condition that triggers the code that turns the buttons red?
-
Hm, you lost me. I thought the problem is that you get a different (than expected) text. So as this doesn't seem to be the case, what exactly is the unexpected behavior? What are doing when you run the application, what are you expecting, and what's happening instead?
-
I think I found the problem...and it was from a completely different section of the code...Lol. There is really no point of posting the answer though, as it has nothing to do with the QMessageBox.
Basically there was a block of code inside an if statement prior to where the QMessageBox was shown, that wasn't being executed until the QMessageBox appeared. Weird if you ask me, but I'm sure the original writer of this code would understand.