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. Access variable from a slot
Forum Updated to NodeBB v4.3 + New Features

Access variable from a slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 146 Views 1 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.
  • J Offline
    J Offline
    jamat13
    wrote last edited by
    #1

    I can't believe that I have never done this in all the years ..

    main: emits to another "tell me page"
    other says "2"

    main dbus handler says
    answered = true;
    answer = 2;

    main loop:
    never sees answered
    never sees answer

    main.h
    class
    volatile bool answered
    volatile int answer
    ;

    What am I missing
    ~

    Pl45m4P 1 Reply Last reply
    0
    • J jamat13

      I can't believe that I have never done this in all the years ..

      main: emits to another "tell me page"
      other says "2"

      main dbus handler says
      answered = true;
      answer = 2;

      main loop:
      never sees answered
      never sees answer

      main.h
      class
      volatile bool answered
      volatile int answer
      ;

      What am I missing
      ~

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote last edited by
      #2

      @jamat13 said in Access variable from a slot:

      What am I missing

      A proper description of your setup/situation.
      What do you emit where and what is transported?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jamat13
        wrote last edited by
        #3

        I was tring to be succient

        record1 ()
        {
        ...
        if ((asbuiltEnabled)) {
        answered = false;
        emit action ("toolbar", "1whatpage");
        QElapsedTimer timer;
        for (timer.start (); !timer.hasExpired (2000); ) {
        if (answered == true) {
        *dbg << "answered " << timer.elapsed () << "ms page " << page[1] << "\n"; dbg->flush ();
        answered = false;
        break;
        }
        }
        if (timer.elapsed () > 1990)
        asbuiltEnabled = false;
        }
        *dbg << "emit 1record " << QDateTime::currentDateTime ().toString (" hh:mm:ss.z\n"); dbg->flush ();
        emit action ("toolbar", QString ("1record %1").arg (page[1]));
        page[1] = 0;
        }
        }

        void MainWindow::dbusAction (const QString &nickname, const QString &text)
        {
        ..
        else if (text.contains ("page")) {
        *dbg << "------------- page [" << text.left (1).toInt () << "] = " << text.right (1).toInt () << " set answered=true\n";
        page[text.left (1).toInt ()] = text.right (1).toInt ();
        answered = true;
        }
        }

        jsulmJ JonBJ 2 Replies Last reply
        0
        • J jamat13

          I was tring to be succient

          record1 ()
          {
          ...
          if ((asbuiltEnabled)) {
          answered = false;
          emit action ("toolbar", "1whatpage");
          QElapsedTimer timer;
          for (timer.start (); !timer.hasExpired (2000); ) {
          if (answered == true) {
          *dbg << "answered " << timer.elapsed () << "ms page " << page[1] << "\n"; dbg->flush ();
          answered = false;
          break;
          }
          }
          if (timer.elapsed () > 1990)
          asbuiltEnabled = false;
          }
          *dbg << "emit 1record " << QDateTime::currentDateTime ().toString (" hh:mm:ss.z\n"); dbg->flush ();
          emit action ("toolbar", QString ("1record %1").arg (page[1]));
          page[1] = 0;
          }
          }

          void MainWindow::dbusAction (const QString &nickname, const QString &text)
          {
          ..
          else if (text.contains ("page")) {
          *dbg << "------------- page [" << text.left (1).toInt () << "] = " << text.right (1).toInt () << " set answered=true\n";
          page[text.left (1).toInt ()] = text.right (1).toInt ();
          answered = true;
          }
          }

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote last edited by jsulm
          #4

          @jamat13 Please format your code properly, so others can read it. And also explain what the problem is in an understandable way.
          This blocks Qt event loop:

          for (timer.start (); !timer.hasExpired (2000); )
          

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

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jamat13
            wrote last edited by
            #5

            Without doubt the event loop is my (silly) problem: thanks
            My code is formatted, pasting eats whitespace. How to paste? as :"?

            J.HilkJ 1 Reply Last reply
            0
            • J jamat13

              Without doubt the event loop is my (silly) problem: thanks
              My code is formatted, pasting eats whitespace. How to paste? as :"?

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote last edited by
              #6

              @jamat13 this button adds a code block for you:
              bd6cf9cb-0f04-4a96-9e1f-500918eca0bd-image.png

              alternatively can you also manually type ``` at the beginning of your code segment and at the end, if you don't want to use the button. In general the textinput supports markdown-esk syntax most of the time. Doesn't work for title and subtitle, but Codeblocks, Lists, Bold/Cursive, links etc are supported


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • J jamat13

                I was tring to be succient

                record1 ()
                {
                ...
                if ((asbuiltEnabled)) {
                answered = false;
                emit action ("toolbar", "1whatpage");
                QElapsedTimer timer;
                for (timer.start (); !timer.hasExpired (2000); ) {
                if (answered == true) {
                *dbg << "answered " << timer.elapsed () << "ms page " << page[1] << "\n"; dbg->flush ();
                answered = false;
                break;
                }
                }
                if (timer.elapsed () > 1990)
                asbuiltEnabled = false;
                }
                *dbg << "emit 1record " << QDateTime::currentDateTime ().toString (" hh:mm:ss.z\n"); dbg->flush ();
                emit action ("toolbar", QString ("1record %1").arg (page[1]));
                page[1] = 0;
                }
                }

                void MainWindow::dbusAction (const QString &nickname, const QString &text)
                {
                ..
                else if (text.contains ("page")) {
                *dbg << "------------- page [" << text.left (1).toInt () << "] = " << text.right (1).toInt () << " set answered=true\n";
                page[text.left (1).toInt ()] = text.right (1).toInt ();
                answered = true;
                }
                }

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote last edited by JonB
                #7

                @jamat13
                Do not use signals/slots for either receiving an immediate reply from any attached slot(s) nor for "waiting" for something to happen as a consequence. That is not what they are intended for --- they are a "notify and continue" paradigm.

                If you want a "response" instead either use a direct function call or in the signaller emit the signal, save whatever state is necessary and continue to the Qt event loop, while the slot emits another signal when it is finished which the originally signalling side acts on in its own slot when it arrives. Use a QTimer at the signalling side if you need to know that the slot has not "replied" within a period of time.

                answered = false;
                emit action ("toolbar", "1whatpage");
                QElapsedTimer timer;
                for (timer.start (); !timer.hasExpired (2000); ) {
                if (answered == true) {
                *dbg << "answered " << timer.elapsed () << "ms page " << page[1] << "\n"; dbg->flush ();
                answered = false;
                break;
                }
                

                Your loop is very "busy": it burns your CPU and completely blocks the thread it is in. Since it does not enter the Qt event loop it does not allow normal processing to continue. Behaviour of your code probably depends on whether attached slot(s) run in the same thread or a different one.

                1 Reply Last reply
                2

                • Login

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