Ok, so how do I get the parent object?
Max
Ok, so how do I get the parent object?
Max
Use QObject::parent(), although I think you shouldn't need that. What are you trying to achieve?
I'm trying to create a data table model out of data column objects where each column has a dynamically defined data type. Each column object should be managed by the Qt memory cleanup subsystem so it must derive from QObject and have the Q_OBJECT macro in it's class definition.
Here's my solution:
To add a column execute something like:
int MyTableModel::columnAppendNew(QString name)
{
MyColumn *column = new MyColumn(name, this);
columns_.append(column);
}
The MyColumn class looks like:
class MyColumn : public QObject
{
Q_OBJECT
public:
explicit MyColumn(QString header, QObject *parent = 0)
: header_(header), QObject(parent)
{
}
~AbstractColumn()
{
}
protected:
QString header_;
}
So now, I hope, the MyColumn object is created as a "memory child" of MyTable. N'est pas? Why MyTable is deleted, then all the columns will likewise be deleted.
Would someone please tell the trolls their dual usage of the term "parent" is confusing.
Thanks,
Max
Do you need to derive those columns from QObject only because you want them cleaned up when the model gets deleted? If so, then you might just call qDeleteAll() on your columns_ object in the model destructor. And you don't need the Q_OBJECT macro even if you want to keep the QObject legacy. As long as you don't need the meta object for the class (like when declaring new signals or slots), Q_OBJECT is not required.
Max Yaffe (15th June 2007)
Yes, I wanted to use the cleanup mechanism. I think I read that one can explicitly delete new'ed object, but, let's face it, I'm just lazy. Actually, the cleanup is a great feature of Qt. I also hope to be able to send signals to the columns to force things like change column format, etc.
Thanks for your help.
Max
Last edited by wysota; 15th June 2007 at 15:21. Reason: Missing closing tag
I think you should manipulate columns this way. The model should be responsible for that and it doesn't need signals and slots. So in general I think you shouldn't derive the columns from QObject. It just doesn't seem to have any advantages and is causing you problems.
Bookmarks