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