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. QMessageBox show() not displaying text

QMessageBox show() not displaying text

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 6 Posters 12.1k Views
  • 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.
  • mrjjM mrjj

    Hi
    Since you only have one device with few States,
    switch case will work pretty well as
    something like
    http://digint.ch/tinyfsm/doc/introduction.html
    is most likely overkill for such simple machine.
    (Qt also have http://doc.qt.io/qt-5/qstate.html#details )

    However, an object orientated approach makes it easier to add test code and extra conditions
    on transit and will in general be more solid with future features added.

    but in my experience, if only a few states and few possible paths, a switch case and
    function to handle the transits is just right as using a Full State system is pretty verbose.

    mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #13

    @mrjj thanks for the link. I tend to agree that for this program, a true state machine is overkill (though I will probably need one in a few weeks on another project). This project has been a learning experience for me on a few levels: it's really the first time I've tried to do anything with Qt, it's my first attempt at OOD and I'm using some data structures for the first time. Lots of opportunity for mistakes.

    Currently I'm trying to run a timer at most once a second. Here's a code fragment:

                if (devState == RESETTING)
                {
                    if (timer.isActive()) // only do one at a time.
                    {
                        qDebug() << "\t\ttimer already queued.";
                    }
                    else
                    {
                        timer.singleShot(1000, this, SLOT(sendDiscoveryRequest()));
                        qDebug() << "\t\tqueueing timer.";
                    }
                }
    

    This code is inside a slot function that is called whenever the target writes to the host. I never hit the "timer already queued". Am I misusing the singleshot feature?

    mrjjM 1 Reply Last reply
    0
    • mzimmersM mzimmers

      @mrjj thanks for the link. I tend to agree that for this program, a true state machine is overkill (though I will probably need one in a few weeks on another project). This project has been a learning experience for me on a few levels: it's really the first time I've tried to do anything with Qt, it's my first attempt at OOD and I'm using some data structures for the first time. Lots of opportunity for mistakes.

      Currently I'm trying to run a timer at most once a second. Here's a code fragment:

                  if (devState == RESETTING)
                  {
                      if (timer.isActive()) // only do one at a time.
                      {
                          qDebug() << "\t\ttimer already queued.";
                      }
                      else
                      {
                          timer.singleShot(1000, this, SLOT(sendDiscoveryRequest()));
                          qDebug() << "\t\tqueueing timer.";
                      }
                  }
      

      This code is inside a slot function that is called whenever the target writes to the host. I never hit the "timer already queued". Am I misusing the singleshot feature?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #14

      Hi
      Normally you use the static version of singleshot

      QTimer::singleShot(1000, this, SLOT(timerDone()));

      It requires no instance.

      But seems you need an instance for others checks so a variable should be fine.

      so when u send DiscoveryRequest, can it also timeout ?

      mzimmersM 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Normally you use the static version of singleshot

        QTimer::singleShot(1000, this, SLOT(timerDone()));

        It requires no instance.

        But seems you need an instance for others checks so a variable should be fine.

        so when u send DiscoveryRequest, can it also timeout ?

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #15

        @mrjj are you asking if the sendDiscoveryRequest() routine might be timing out? That's unlikely...it just writes a short byte array to the serial port and flushes it.

        mrjjM 1 Reply Last reply
        0
        • mzimmersM mzimmers

          @mrjj are you asking if the sendDiscoveryRequest() routine might be timing out? That's unlikely...it just writes a short byte array to the serial port and flushes it.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #16

          @mzimmers
          Oh, sorry for being unclear,
          i mean, do u need to be able to have a timeout on each command if no answer is received ?
          Often it goes
          SendCMD
          OnReplyDo/ TimeOut-> restart sequence.

          mzimmersM 1 Reply Last reply
          0
          • mrjjM mrjj

            @mzimmers
            Oh, sorry for being unclear,
            i mean, do u need to be able to have a timeout on each command if no answer is received ?
            Often it goes
            SendCMD
            OnReplyDo/ TimeOut-> restart sequence.

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #17

            @mrjj I'm still not sure I understand your question, so let me give some supplementary information.

            In normal operation, the host forms requests and sends them to the target, then reads a response from the target. Unfortunately, the target also spews out unsolicited (and unwanted messages) when in a certain state. During the first 10-15 seconds of this state, the target will ignore any input from the host. After this 10-15 second period, the target will listen for requests. A discovery request will cause the target to cease sending the (unwanted) debug messages.

            Because the messages are binary and encrypted, there's no way of knowing at first glance whether I've received a "real" response, or just the debugger output. My solution is, when the target is in the "resetting" state, send it a discovery request every second, until I get a valid discovery response. THAT is what I'm trying to do with my timer.

            I don't "wait" for responses to my requests; I just process any serial input with a slot.

            I don't know whether this answers your question or not...

            mrjjM 1 Reply Last reply
            1
            • mzimmersM mzimmers

              @mrjj I'm still not sure I understand your question, so let me give some supplementary information.

              In normal operation, the host forms requests and sends them to the target, then reads a response from the target. Unfortunately, the target also spews out unsolicited (and unwanted messages) when in a certain state. During the first 10-15 seconds of this state, the target will ignore any input from the host. After this 10-15 second period, the target will listen for requests. A discovery request will cause the target to cease sending the (unwanted) debug messages.

              Because the messages are binary and encrypted, there's no way of knowing at first glance whether I've received a "real" response, or just the debugger output. My solution is, when the target is in the "resetting" state, send it a discovery request every second, until I get a valid discovery response. THAT is what I'm trying to do with my timer.

              I don't "wait" for responses to my requests; I just process any serial input with a slot.

              I don't know whether this answers your question or not...

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #18

              @mzimmers
              Yes that a very nice explanation.
              Its seems like a valid approach.
              Sometimes i use a list of small classes to hold each state, and use
              virtual function to make sure i dont get too many if structures

              But i wonder one thing
              timer.singleShot(1000, this, SLOT(sendDiscoveryRequest()));

              so you call sendDiscoveryRequest one time. will it then start the timer again?

              1 Reply Last reply
              1
              • mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #19

                The sendDiscoveryMessage() call is a "kick start" to the communications process. One of two things will happen:

                • the target is in running state, and will respond with a valid response. In this case, no new timer is desired.
                • the target is in resetting state, and will not respond, BUT will continue to send those spurious debug messages. When it sends one of these, the routine the above code is in is invoked (as a slot).

                So, in either case, the host will receive some input from the target, and normal processing can continue.

                These debug messages can come very quickly, and I don't wish to respond to each with a discovery request. This is the purpose of the timer -- to "gate" my requests. But...it's not working, as timer.isActive() never returns true.

                mrjjM 1 Reply Last reply
                0
                • mzimmersM mzimmers

                  The sendDiscoveryMessage() call is a "kick start" to the communications process. One of two things will happen:

                  • the target is in running state, and will respond with a valid response. In this case, no new timer is desired.
                  • the target is in resetting state, and will not respond, BUT will continue to send those spurious debug messages. When it sends one of these, the routine the above code is in is invoked (as a slot).

                  So, in either case, the host will receive some input from the target, and normal processing can continue.

                  These debug messages can come very quickly, and I don't wish to respond to each with a discovery request. This is the purpose of the timer -- to "gate" my requests. But...it's not working, as timer.isActive() never returns true.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #20

                  @mzimmers

                  • timer.isActive() never returns true.
                    Well if it takes more than 1 sec before the containing slot is called again then is it then not true that timer.isActive() is false?
                    also the single shot might not even set active. maybe only start() does.
                    (just speculating)
                  mzimmersM 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @mzimmers

                    • timer.isActive() never returns true.
                      Well if it takes more than 1 sec before the containing slot is called again then is it then not true that timer.isActive() is false?
                      also the single shot might not even set active. maybe only start() does.
                      (just speculating)
                    mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by mzimmers
                    #21

                    @mrjj said in QMessageBox show() not displaying text:

                    Well if it takes more than 1 sec before the containing slot is called again then is it then not true that timer.isActive() is false?

                    If it takes more than 1 second (which it never does), the isActive should report false, and a new singleShot should be called. At least that's what I'm attempting to do.

                    also the single shot might not even set active. maybe only start() does.
                    (just speculating)

                    Interesting...I'll replace the singleShot with a conventional timer and report back.

                    kshegunovK 1 Reply Last reply
                    0
                    • mzimmersM mzimmers

                      @mrjj said in QMessageBox show() not displaying text:

                      Well if it takes more than 1 sec before the containing slot is called again then is it then not true that timer.isActive() is false?

                      If it takes more than 1 second (which it never does), the isActive should report false, and a new singleShot should be called. At least that's what I'm attempting to do.

                      also the single shot might not even set active. maybe only start() does.
                      (just speculating)

                      Interesting...I'll replace the singleShot with a conventional timer and report back.

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

                      singleShot is a static function, it calls setSingleShot(true) and then start(). As this function doesn't really require an object it also does not modify your object in any way, effectively you're calling QTimer::singleShot.

                      PS.
                      Use like this:

                      timer.setSingleShot(true);
                      QObject::connect(&timer, &QTimer::timeout, this, &WhateverYourClassIsNamed::sendDiscoveryRequest);
                      // ...
                      if (devState == RESETTING)  {
                          if (timer.isActive()) // only do one at a time.
                              qDebug() << "\t\ttimer already queued.";
                          else  {
                              timer.start(1000);
                              qDebug() << "\t\tqueueing timer.";
                          }
                      }
                      

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      1
                      • mzimmersM Offline
                        mzimmersM Offline
                        mzimmers
                        wrote on last edited by
                        #23

                        OK, I found why the timer wasn't working for me. It turns out I defined the QTimer * variable twice: once as a member of my Worker class, and once again in the Worker constructor. General hilarity ensued.

                        I'm not saying this wasn't my fault, but I'd have expected the damn compiler to at least give me a warning when I did this.

                        Thanks to all for the help on the QTimer. Now that my communications sequence is intact, I can review my approach to the original problem with the QMessageBox. I'll report back when I have something.

                        kshegunovK 1 Reply Last reply
                        0
                        • mzimmersM mzimmers

                          OK, I found why the timer wasn't working for me. It turns out I defined the QTimer * variable twice: once as a member of my Worker class, and once again in the Worker constructor. General hilarity ensued.

                          I'm not saying this wasn't my fault, but I'd have expected the damn compiler to at least give me a warning when I did this.

                          Thanks to all for the help on the QTimer. Now that my communications sequence is intact, I can review my approach to the original problem with the QMessageBox. I'll report back when I have something.

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

                          @mzimmers said in QMessageBox show() not displaying text:

                          I'm not saying this wasn't my fault, but I'd have expected the damn compiler to at least give me a warning when I did this.

                          Not a thing that warrants a warning. Shadowing is a normal feature of the language, and you can see it quite a lot in my code. Without this I'd get a ton of warnings for correct usage of a valid language construct. For example for a generic QObject subclass (a situation that happens all the time):

                          class MyClass : public QObject
                          {
                          public:
                              MyClass(QObject * parent = nullptr) //< `parent` shadows QObject::parent()
                                  : QObject(parent)
                              {
                              }
                          };
                          

                          Or even more elaborate:

                          void QMpiCompoundRequestPrivate::requestFinished(QMpiRequest * child)
                          {
                              if (!subrequests.remove(child))
                                  return;
                          
                              Q_Q(QMpiCompoundRequest);
                              emit q->finished(child);
                              if (subrequests.size() == 0)
                                  emit q->QMpiRequest::finished();
                          }
                          

                          See how the second signal emission is done. That's because in the derived class (QMpiCompoundRequest) there's a function that shadows the base class' (QMpiRequest) method (even though with different set of parameters). So in this case you must be explicit which function exactly you're calling.

                          Read and abide by the Qt Code of Conduct

                          mzimmersM 1 Reply Last reply
                          2
                          • kshegunovK kshegunov

                            @mzimmers said in QMessageBox show() not displaying text:

                            I'm not saying this wasn't my fault, but I'd have expected the damn compiler to at least give me a warning when I did this.

                            Not a thing that warrants a warning. Shadowing is a normal feature of the language, and you can see it quite a lot in my code. Without this I'd get a ton of warnings for correct usage of a valid language construct. For example for a generic QObject subclass (a situation that happens all the time):

                            class MyClass : public QObject
                            {
                            public:
                                MyClass(QObject * parent = nullptr) //< `parent` shadows QObject::parent()
                                    : QObject(parent)
                                {
                                }
                            };
                            

                            Or even more elaborate:

                            void QMpiCompoundRequestPrivate::requestFinished(QMpiRequest * child)
                            {
                                if (!subrequests.remove(child))
                                    return;
                            
                                Q_Q(QMpiCompoundRequest);
                                emit q->finished(child);
                                if (subrequests.size() == 0)
                                    emit q->QMpiRequest::finished();
                            }
                            

                            See how the second signal emission is done. That's because in the derived class (QMpiCompoundRequest) there's a function that shadows the base class' (QMpiRequest) method (even though with different set of parameters). So in this case you must be explicit which function exactly you're calling.

                            mzimmersM Offline
                            mzimmersM Offline
                            mzimmers
                            wrote on last edited by
                            #25

                            @kshegunov thanks for the explanation. I had never seen shadowing before, probably because I'd done very little work with derived classes. Good stuff to know going forward.

                            1 Reply Last reply
                            1

                            • Login

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