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. OverLoad of class that inherited QObject
Forum Updated to NodeBB v4.3 + New Features

OverLoad of class that inherited QObject

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 3.3k 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.
  • taku-sT Offline
    taku-sT Offline
    taku-s
    wrote on last edited by taku-s
    #1

    Hi all.
    I want to use a dynamic array for my own class member variable.
    To do this, I'll need to pass the array quantity as an argument in the constructor of the class and generate an array.
    For classe that inherit from QObject, can I pass numbers as arguments in the constructor?
    I would like to use QTimer in my own class and use the timeout event.

    Thank you google translate.

    jsulmJ 1 Reply Last reply
    0
    • taku-sT taku-s

      Hi all.
      I want to use a dynamic array for my own class member variable.
      To do this, I'll need to pass the array quantity as an argument in the constructor of the class and generate an array.
      For classe that inherit from QObject, can I pass numbers as arguments in the constructor?
      I would like to use QTimer in my own class and use the timeout event.

      Thank you google translate.

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

      @taku-s said in OverLoad of class that inherited QObject:

      For classe that inherit from QObject, can I pass numbers as arguments in the constructor?

      Sure, you can pass what ever you want.
      One thing to clarify: what do you mean by "dynamic array"? A container like QVector? std::array? Something else?

      I would like to use QTimer in my own class and use the timeout event.

      You can do this.

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

      1 Reply Last reply
      1
      • taku-sT Offline
        taku-sT Offline
        taku-s
        wrote on last edited by taku-s
        #3

        Thank you for your reply.
        For example, in the case of ANCI C11

        class sub {
        private:
        	int* m_arry;
        	char* m_arry2;
        	int m_col;
        public:
        	sub(int, int);
        	virtual ~sub();
        	void setarry(int, int);
        	void setarry2(int, int, char);
        	int getarry(int);
        	char getarry2(int, int);
        };
        
        sub::sub(int i, int j) {
        	m_arry = new int[i];
        	m_arry2 = new char[i*j];
        	m_col = j;
        }
        sub::~sub() {
        	delete m_arry;
        	delete m_arry2;
        }
        void sub::setarry(int i, int t){
        	*(m_arry+i) = t;
        }
        void sub::setarry2(int i, int j, char d){
        	*(m_arry2+(i*m_col+j)) = d;
        }
        int sub::getarry(int i){
        	return *(m_arry+i);
        }
        char sub::getarry2(int i, int j){
        	return *(m_arry2+(i*m_col+j));
        }
        
        #include <iostream>
        using namespace std;
        
        int main() {
        	int row = 10;
        	int col = 8;
                char data[] = {'A','B','C','D','E','F','G','H'};
        
        	sub s(row, col);	//<-This point
        
        	for(int i=0; i<row; i++){
        		s.setarry(i, i*2);
        		for(int j=0; j<col; j++){
        			s.setarry2(i, j, data[j]);
        		}
        	}
        
        	for(int i=0; i<row; i++){
        		cout << "No " << i << '\t';
        		cout << s.getarry(i) << '\t';
        		for(int j=0; j<col; j++){
        			cout << s.getarry2(i, j) << "  ";
        		}
        		cout << endl;
        	}
        
        	return 0;
        }
        
        jsulmJ 1 Reply Last reply
        0
        • taku-sT taku-s

          Thank you for your reply.
          For example, in the case of ANCI C11

          class sub {
          private:
          	int* m_arry;
          	char* m_arry2;
          	int m_col;
          public:
          	sub(int, int);
          	virtual ~sub();
          	void setarry(int, int);
          	void setarry2(int, int, char);
          	int getarry(int);
          	char getarry2(int, int);
          };
          
          sub::sub(int i, int j) {
          	m_arry = new int[i];
          	m_arry2 = new char[i*j];
          	m_col = j;
          }
          sub::~sub() {
          	delete m_arry;
          	delete m_arry2;
          }
          void sub::setarry(int i, int t){
          	*(m_arry+i) = t;
          }
          void sub::setarry2(int i, int j, char d){
          	*(m_arry2+(i*m_col+j)) = d;
          }
          int sub::getarry(int i){
          	return *(m_arry+i);
          }
          char sub::getarry2(int i, int j){
          	return *(m_arry2+(i*m_col+j));
          }
          
          #include <iostream>
          using namespace std;
          
          int main() {
          	int row = 10;
          	int col = 8;
                  char data[] = {'A','B','C','D','E','F','G','H'};
          
          	sub s(row, col);	//<-This point
          
          	for(int i=0; i<row; i++){
          		s.setarry(i, i*2);
          		for(int j=0; j<col; j++){
          			s.setarry2(i, j, data[j]);
          		}
          	}
          
          	for(int i=0; i<row; i++){
          		cout << "No " << i << '\t';
          		cout << s.getarry(i) << '\t';
          		for(int j=0; j<col; j++){
          			cout << s.getarry2(i, j) << "  ";
          		}
          		cout << endl;
          	}
          
          	return 0;
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @taku-s Why do you want to use C arrays for that?!
          Why not using a container (like QVector) or std::array?

          And is there a question in your last post?

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

          1 Reply Last reply
          2
          • taku-sT Offline
            taku-sT Offline
            taku-s
            wrote on last edited by
            #5

            Even if I use Vecor, I think that it is common to resize(int n) in the constructor.
            This time, it happens to be an array initialization problem, but I think that there are many other cases using a constructor with arguments.

            jsulmJ 1 Reply Last reply
            0
            • taku-sT taku-s

              Even if I use Vecor, I think that it is common to resize(int n) in the constructor.
              This time, it happens to be an array initialization problem, but I think that there are many other cases using a constructor with arguments.

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

              @taku-s I don't see any problem with constructor having two int parameters, so is there ann issue now?

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

              1 Reply Last reply
              0
              • taku-sT Offline
                taku-sT Offline
                taku-s
                wrote on last edited by
                #7

                The constructor of the class that inherited from QObject looks like the following, can I change this to a constructor with arguments?
                Or can I overload?

                explicit Sub (QObject * parent = nullptr);

                I think that I can not receive timerslot() in classes that do not inherit QObject as shown below.

                Sub (int);

                mrjjM jsulmJ 2 Replies Last reply
                0
                • taku-sT taku-s

                  The constructor of the class that inherited from QObject looks like the following, can I change this to a constructor with arguments?
                  Or can I overload?

                  explicit Sub (QObject * parent = nullptr);

                  I think that I can not receive timerslot() in classes that do not inherit QObject as shown below.

                  Sub (int);

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

                  @taku-s
                  Hi
                  You can change constructor any way you like or define new ones.

                  You need to inherit QObject to send/receive signals and also need the Q_OBJECT macro.

                  class sub: public QObject
                  {
                    Q_OBJECT
                  ...
                  
                  1 Reply Last reply
                  3
                  • taku-sT taku-s

                    The constructor of the class that inherited from QObject looks like the following, can I change this to a constructor with arguments?
                    Or can I overload?

                    explicit Sub (QObject * parent = nullptr);

                    I think that I can not receive timerslot() in classes that do not inherit QObject as shown below.

                    Sub (int);

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

                    @taku-s Currently your class does NOT inherit from QObject.
                    And yes, you can have your own constructors in QObject derived classes with any parameters, just call the QObject constructor:

                    Sub::Sub(int p1, int p2, QObject *parent): QObject(parent)
                    {
                    ...
                    }
                    

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

                    1 Reply Last reply
                    2
                    • taku-sT Offline
                      taku-sT Offline
                      taku-s
                      wrote on last edited by
                      #10

                      Finally I could understand
                      Thank you so much everyone

                      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