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. [solved]: Global parameter
QtWS25 Last Chance

[solved]: Global parameter

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 2.8k 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.
  • R Offline
    R Offline
    romeo.rbw
    wrote on last edited by
    #1

    Hi..
    I want to ask about global parameter in Qt.
    I have a class that have a method with 'count' variable. For the time being this 'count' I placed on private.

    In the mainwindow.cpp, I create an object of that class every 200ms.
    I cannot detect if the count reaches 3 (count==3) because I thought that it become 0 when the object created every 200ms.
    Please help where should I place this global parameter so that I can detect when the count reaches 3.
    Thanks

    1 Reply Last reply
    0
    • W Offline
      W Offline
      Wilk
      wrote on last edited by
      #2

      Hello.
      One of the possible ways of achieving what you want is to make count a static private field, increment it in constructor and decrement it in destructor:
      @
      class Your_Class {
      private:
      static unsigned int count = 0;
      public:
      Your_Class () {
      count++;
      }
      ~Your_Class () {
      count--;
      }
      }
      @
      But I think it's not a good solution:

      It's not thread safe.

      You should watch out for temp objects.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        romeo.rbw
        wrote on last edited by
        #3

        Thanks for your reply,
        I tried but there is a message:
        ' ..only static const integral data members can be initialized within a class'

        I change the parameter to

        static unsigned int count;

        the message become: ' unresolved external symbol...'

        1 Reply Last reply
        0
        • W Offline
          W Offline
          Wilk
          wrote on last edited by
          #4

          Sorry, my fault. Change to @static unsigned int count@ and in you .cpp file write @unsigned int Your_Class::count = 0.@

          1 Reply Last reply
          0
          • R Offline
            R Offline
            romeo.rbw
            wrote on last edited by
            #5

            Thanks for reply...
            I already try it, I tried adding unsigned int MyClass::count=0, in mainwindow.cpp, it did not work. And neither in main.cpp.
            Is there any other solution for Global parameter?
            thanks in advance

            1 Reply Last reply
            0
            • W Offline
              W Offline
              Wilk
              wrote on last edited by
              #6

              Hello again.
              I'm sorry, but it seems like you're doing something wrong. Because I'm looking at my own code with the same structure. It works. Could you please post code, that doesn't work and compiler output with error?

              1 Reply Last reply
              0
              • R Offline
                R Offline
                romeo.rbw
                wrote on last edited by
                #7

                Hi...
                yes may be there is something wrong in my code, here is the code, please give suggestion:

                mylaserclick.h:

                @
                #ifndef MYLASERCLICK_H
                #define MYLASERCLICK_H

                #include <QObject>
                #include <opencv2/core/core.hpp>

                using namespace std;
                using namespace cv;

                class MyLaserClick : public QObject
                {
                Q_OBJECT
                public:
                explicit MyLaserClick(bool inspace, bool laser_on, QObject *parent = 0);

                signals:

                public slots:
                bool cek_click();

                private:
                bool inspace;
                bool on;

                static unsigned int ready_klik;
                static unsigned int count;
                

                };

                #endif // MYLASERCLICK_H
                @

                mylaserclick.cpp:
                @
                #include "mylaserclick.h"

                MyLaserClick::MyLaserClick(bool inspace, bool laser_on, QObject *parent) :
                QObject(parent)
                {
                this->inspace = inspace;
                this->on = laser_on;

                }

                bool MyLaserClick::cek_click()
                {
                if (inspace==true && on==true && ready_klik != 2)
                ready_klik=1;

                if (on==false && ready_klik==1 && inspace==true){
                    ready_klik=2;
                    count= count+1;
                }
                if (count==1 && inspace==false){     
                    ready_klik=0;
                    count=0;
                }
                
                if (on==true && ready_klik==2 && inspace==true){
                    count = count+1;
                }
                
                if (count==2){
                    return true;
                    count=0;
                    ready_klik=0;
                }
                

                else
                return false;
                }

                EDIT1: :) I modify by change the 'return true;' to the last line
                if (count==2){
                count=0;
                ready_klik=0;
                return true;
                }

                @

                mainwindow.cpp:
                @
                ....
                MyLaserClick *laserklik;
                laserklik = new MyLaserClick(inspace,on);
                laser_click_inspace = laserklik->cek_click();
                ....
                @

                main.cpp:
                @
                #include <QtGui/QApplication>
                #include "mainwindow.h"

                int main(int argc, char *argv[])
                {
                QApplication a(argc, argv);

                unsigned int MyLaserClick::ready_klik=0;
                unsigned int MyLaserClick::count=0;
                
                MainWindow *dialog = new MainWindow;
                dialog->show();
                
                return a.exec(&#41;;
                

                }
                @

                the error message are:
                'MyLaserClick' is not a class or namespace name main.cpp line 9

                'MyLaserClick' is not a class or namespace name main.cpp line 10

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  Wilk
                  wrote on last edited by
                  #8

                  Yep, wrong code.
                  The right one:
                  mylaserclick.cpp:
                  @
                  #include "mylaserclick.h"

                  unsigned int MyLaserClick::ready_klik=0;
                  unsigned int MyLaserClick::count=0;

                  /* ... */
                  @

                  And delete 9 and 10 lines from main.cpp.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    romeo.rbw
                    wrote on last edited by
                    #9

                    It works, no error message again. And the count run well. Many thanks...
                    Thank you very very much...:)

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      Wilk
                      wrote on last edited by
                      #10

                      Do not mention it

                      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