Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simple QMessageBox changing parts of program...?
Forum Updated to NodeBB v4.3 + New Features

Simple QMessageBox changing parts of program...?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MaxDevI
    wrote on last edited by
    #1

    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.

    kshegunovK 1 Reply Last reply
    0
    • M Offline
      M Offline
      MaxDevI
      wrote on last edited by MaxDevI
      #11

      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.

      1 Reply Last reply
      0
      • M MaxDevI

        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.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #2

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

        Read and abide by the Qt Code of Conduct

        M 1 Reply Last reply
        0
        • kshegunovK kshegunov

          @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()));
          
          M Offline
          M Offline
          MaxDevI
          wrote on last edited by MaxDevI
          #3

          @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()
          
          kshegunovK 1 Reply Last reply
          0
          • M MaxDevI

            @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()
            
            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #4

            @MaxDevI
            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());
            

            whereas:

            QString MyClass::getWarning()
            {
                return QObject::tr("This is a translated warning text");
            }
            

            Read and abide by the Qt Code of Conduct

            M 1 Reply Last reply
            0
            • kshegunovK kshegunov

              @MaxDevI
              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());
              

              whereas:

              QString MyClass::getWarning()
              {
                  return QObject::tr("This is a translated warning text");
              }
              
              M Offline
              M Offline
              MaxDevI
              wrote on last edited by
              #5

              @kshegunov Thanks, same problem occurring though.

              Is there somehow a slot/signal could have been connected which triggers on the QMessageBox appearing?

              kshegunovK jsulmJ 2 Replies Last reply
              0
              • M MaxDevI

                @kshegunov Thanks, same problem occurring though.

                Is there somehow a slot/signal could have been connected which triggers on the QMessageBox appearing?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #6

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

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • M MaxDevI

                  @kshegunov Thanks, same problem occurring though.

                  Is there somehow a slot/signal could have been connected which triggers on the QMessageBox appearing?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @MaxDevI What is actually this "unexpected behavior"?
                  To me it is completely unclear: does your application crash or something else?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MaxDevI
                    wrote on last edited by
                    #8

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

                    M kshegunovK 2 Replies Last reply
                    0
                    • M MaxDevI

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

                      M Offline
                      M Offline
                      mchinand
                      wrote on last edited by
                      #9

                      So this QMessageBox section is triggered by a different condition, not the same condition that triggers the code that turns the buttons red?

                      1 Reply Last reply
                      0
                      • M MaxDevI

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

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by kshegunov
                        #10

                        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?

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          MaxDevI
                          wrote on last edited by MaxDevI
                          #11

                          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.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved