PDA

View Full Version : QObject, properties



Archa4
9th February 2011, 11:44
I want to ask:
i created and object type class name Movie:

#ifndef MOVIE_H
#define MOVIE_H

#include <QObject>

QT_BEGIN_NAMESPACE
class QLabel;
QT_END_NAMESPACE

class Movie : public QObject
{
Q_OBJECT
public:
Movie(QObject *parent = 0);
~Movie();
public slots:
QString name()
{return _name;}
void setName(QString name_in)
{_name = name_in;}

QString date()
{return _date;}
void setDate(QString date_in)
{_date = date_in;}

QString about()
{return _about;}
void setAbout(QString about_in)
{_about = about_in;}

QString picture()
{return _picture;}
void setPicture(QString picture_in)
{_picture = picture_in;}

private:
QString _name;
QString _date;
QString _about;
QString _picture;
};

#endif // MOVIE_H

In mine main application i Use the following code:

QObject *asd = new Movie;
asd->setName(titleString);

I get the following error:
'class QObject' has no member named 'setName'

What am I doing wrong?

agarny
9th February 2011, 12:00
On line 16, why do you have public slots:? Shouldn't it simply be public?

FelixB
9th February 2011, 12:00
What am I doing wrong?

you're calling setName() on a QObject. QObject does not have a member called "setName()"

Archa4
9th February 2011, 12:11
I found the problem - when i used
QObject *asd = new Movie;
i should really had to use:
Movie *asd = new Movie;

Now i have another problem:
i try to make QVector<Movie> movie_list, and it works
but when i try something like
movie_list.append(asd);

I get 4 errors and 20 warnings...
What am I doing wrong this time?

Well I solved this also.
I needed to make the QVector like this:
QVector<Movie *>;

wysota
9th February 2011, 22:47
QVector requires its content to be copyable which is not true for QObject derivatives. That's why you need to use pointers which can be copied.

Archa4
10th February 2011, 07:27
Then I need to ask another thing related to objects:
I need to make few of those and then pass QVector of them to another function. What should I do to prevent information loss? (objects are being created inside a cycle, and after it ends i think the information disappears, or does it)?

wysota
10th February 2011, 19:15
All C++principles apply here.

By the way I don't see anything in your code that would justify making it a QObject. Is there a particular reason for it?

Archa4
11th February 2011, 07:10
Is there a better way to create something like object?

wysota
11th February 2011, 08:05
You can implement a class that is not derived fro QObject. Inheriting QObject makes sense if the class is to benefit from the meta-object system (e.g. it has signals or slots). Your class seems to be a regular data carrier.