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.

  • Qt Champions 2017

    @MaxDevI said in Simple QMessageBox changing parts of program...?:
    My suspicions fall onto this statement:


    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()

  • Qt Champions 2017

    I'd move the translation in the getWarning() method, where it's supposed to be in the first place, and would just pass the QString to the message box.

    QMessageBox::critical(this, QObject::tr("Warning"), tab->getWarning());


    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?

  • Qt Champions 2017

    @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.

  • Moderators

    @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?

  • Qt Champions 2017

    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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.