C++ constructor and Qt - c++

There is a statement in the foudations of Qt Development book that goes as follows:
MyClass::MyClass(const string& test, QObject *parent) : QObject( parent )
What is meant when we put : QObject( parent )?
Thanks.

Are you sure that there aren't two constructor declarations? The : QObject(parent) is an initializer list; it is initializing the base class QObject with the QObject::QObject(QObject*) constructor.

In short this means that MyClass inherits properties (and methods) from QObject
http://www.cplusplus.com/doc/tutorial/inheritance/
http://www.anyexample.com/programming/cplusplus/cplusplus_inheritance_example.xml
So MyClass is a QObject

When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is.
Reference

Related

How do I call the constructor of a class' member?

I wanted to declare a QGraphicsView which has a previously declared member as an argument of the constructor, but the compiler interprets it as a function.
(Code for reference)
class Widnow : public QMainWindow
{
Q_OBJECT
// constructors, member functions, etc...
private:
Ui::Widnow *ui;
QTimer timer01;
QGraphicsScene gaem;
QGraphicsView wiev(&gaem); //this gets interpreted as a function
}
Trying to call the constructor as QGraphicsView wiev = QGraphicsView(&gaem); also causes an error, as the copy constructor has been deleted... Is there a way to declare this member without errors?
The C++ compiler thinks that it is a method declaration where "wiev" as method that returns an instance of QGraphicsView. Use this way
class Widnow : public QMainWindow
{
private:
QGraphicsScene gaem;
QGraphicsView wiev; //this gets interpreted as a function
public:
Widnow() : wiev(&gaem)
{}
};

Should I define a destructor for a derivate of QWidget

I would like to know if I should define a destructor of a derivate of QWidget.
Example
class CustomWidget : public QWidget{
QLabel* field;
CustomWidget(QWidget* parent) :
QWidget( parent ),
field( new QLabel(this) ) {};
}
I've already read about the "Widget tree" that will call the destructor of all the child if the parent is destructed, but i mean, just to be sure
For the given one, no. The parenting system will take care of field. But u should add Q_OBJECT macro.

Ownership of memory in user-defined classes deriving from Qt classes

I know that, in Qt, parent object takes ownership of its child objects. However, if I have my own class deriving from a Qt class, do I need to control memory in my derived-class' destructor or does Qt do it for me? Below is an example:
#include <QWidget>
#include <QPushButton>
class MyWidget: public QWidget{
public:
MyWidget(QWidget* parent = 0): QWidget(parent) {
this->setAttribute(Qt::WA_DeleteOnClose);
m_button = new QPushButton(this);
}
~MyWidget() { delete m_button; } // do I need this to prevent leaks?
private:
QPushButton* m_button;
}
So my question is, do I need ~MyWidget() to prevent memory leaks? Or would QWidget somehow manage the memory for me?
No you don't, QObject handles that. Everything derived will be collected as long as it is in a parent-children tree. QWidget inherits QObject and you inherit QWidget. So you are all set.
Note that there are still many Qt types which do not inherit QObject. Better look at the doc to be sure.

class constructor initialization?

I am trying to understand the following code.(I am learning C++)
class DefaultDevice : public Device {
public:
DefaultDevice() :
ui(new DefaultUI) {
}
private:
RecoveryUI* ui;
};
class DefaultUI : public ScreenRecoveryUI {
...
}
I am having little trouble understanding ui(new DefaultUI) part.
As I understand it is part of initialization before DefaultDevice() constructor is executed.
Then, from my understanding, it is going to call ReocoveryUI constructor with new DefaultUI argument. But, RecoveryUI class does not have any constructor with such argument.(sorry for not posing RecoveryUI class. it's too long :( if anyone interested in, it is Android Open source code)
so what does this 'new DefaultUI' do?
If that is a valid and working code, It seems RecoveryUI is a base class for DefaultUI.
ui(new DefaultUI) creates an object and assigns it to ui.
So, ui points to its child object.
It is not run before the ctor, it is part of the ctor. It's called an initializer list and it's used to initialize the (non-static) member variables of a class.

Qt "no matching function for call"

I have
no matching function for call to 'saveLine::saveLine()'
error when compiling my application. The construcor is never actually called.
saveLine class definition:
class saveLine
{
public:
saveLine(QWidget *parent);
private:
QPushButton *selectButton, *acceptButton;
QLabel *filePath;
QLineEdit *allias;
};
saveLine is used in another class which is defined as follows:
class MWindow : public QWidget
{
Q_OBJECT
public:
MWindow(QWidget *parent=0);
private:
saveLine line1;
};
error points to MWindow constructor implementation
MWindow::MWindow(QWidget *parent):QWidget(parent)
{
this->setWindowTitle("Launcher");
this->resize(600,600);
}
What should i do? I intend to use saveLine class in a vector, to create lines at runtime.
EDIT: i've misdeclared line1, it should read
saveLine *line1;
but now it gives another error
ISO C++ forbids declaration of 'saveLine' with no type
and
expected ';' before '*' token
on this line. It seems like saveLine is no longer considered a class, how so?
Since you provide a user-declared constructor for the class saveLine, the default constructor is not provided by the compiler. Your constructor is not a default constructor (it has one required parameter), so you cannot default construct an object of type saveLine.
Since you have a saveLine object in your MWindow class, you need to initialize it using your constructor, in the MWindow constructor's initializer list:
MWindow::MWindow(QWidget *parent)
: QWidget(parent), line1(parent)
{
//...
}
(I'm assuming that parent pointer is the one you mean to pass; if you need to give it something else, then give it what it needs)
Another option would be to provide a default argument for the parameter in saveLine's constructor:
saveLine(QWidget *parent = 0);
This would allow that constructor to be called with no arguments (and would make it a default constructor). Whether this makes sense to do depends on whether the parent pointer really is optional. Obviously, if you do this, you'll need to check to be sure the pointer is not null before you dereference and use it.
but now it gives another error
ISO C++ forbids declaration of
'saveLine' with no type
You need to add a forward declaration to tell the compiler that saveLine class exists:
Like this:
//declare that there will be a class saveLine
class saveLine;
class MWindow : public QWidget
{
Q_OBJECT
public:
MWindow(QWidget *parent=0);
private:
saveLine *line1;
};
You have to call the constructor of saveLine in the constructor of MWindow giving him the desired parent.
Use:
MWindow::MWindow(QWidget *parent) : QWidget(parent), line1(parent)
{
this->setWindowTitle("Launcher");
this->resize(600,600);
}
You are declaring an instance of saveLine in the class declaration, instead of a pointer to a saveLine.
You could change the reference in MWindow to
saveLine* line1;
OR
you could implement like this:
MWindow::MWindow(QWidget *parent):QWidget(parent), line1(parent)
{
this->setWindowTitle("Launcher");
this->resize(600,600);
}
Try adding class saveLine; just above the class MWindow line. This often occurs when the .h files for the saveLine class and the MWindow class include each other (either directly or indirectly). For example, see the first few posts of http://www.allegro.cc/forums/thread/594307