OverLoad of class that inherited QObject



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


  • Qt Champions 2018

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



  • 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;
    }
    

  • Qt Champions 2018

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



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


  • Qt Champions 2018

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



  • 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);


  • Lifetime Qt Champion

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

  • Qt Champions 2018

    @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)
    {
    ...
    }
    


  • Finally I could understand
    Thank you so much everyone


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.