Problem with inheritance

M

Martin Jensen

Hi

I have a problem with Qt. My class definition is this:

class Button : public QObject, public Tk_Object {
Q_OBJECT
public:
Button() {}
Button(Tk_Object &p);
~Button();
void pack();

void setKeyword(string key, StringVar value);
void setKeyword(string key, string value);
void setKeyword(string key, double value);
void setKeyword(string key, void value());
QWidget* getWidget();
private:
QPushButton* qPushButton;
Tk_Object* parent;

/* Keywords */
string text;
StringVar* textvariable;
int width;
string relief;
};

There are a lot more classes, but this is the problem... When I try to
compile, I get this error:

/usr/lib/qt-3.1/include/qobject.h: In member function `Tkinter::Button&
Tkinter::Button::eek:perator=(const Tkinter::Button&)':
/usr/lib/qt-3.1/include/qobject.h:212: error: `QObject&
QObject::eek:perator=(const QObject&)' is private
output/example6.tk_sml.py.main.cpp:38: error: within this context

This is probabaly because I use this in the main-program to instantiate the
Button-class:

int main() {
...
Tkinter::Button compute = Tkinter::Button(top);
...
}

The compiler compains that the = operator in the QObject class is private.
This seems to mean that I can't instatiate a class which inherits the
QObject class in this way. Can this be right? Can somebody help me get
around this? Or is there something obvious I have done wrong? Can I overload
the = operator of the QObject class or someting?



Thanks
Martin Jensen
 
R

Rolf Magnus

Martin said:
Hi

I have a problem with Qt. My class definition is this:

class Button : public QObject, public Tk_Object {
Q_OBJECT
public:
Button() {}
Button(Tk_Object &p);

Classes derived from QObject should usually at least be able to take a
pointer to a parent QObject and usually a char* for the name. Maybe
there is some special reason why you don't want that here.
~Button();
void pack();

void setKeyword(string key, StringVar value);
void setKeyword(string key, string value);
void setKeyword(string key, double value);
void setKeyword(string key, void value());

QWidget* getWidget();
private:
QPushButton* qPushButton;
Tk_Object* parent;

/* Keywords */
string text;
StringVar* textvariable;
int width;
string relief;
};

There are a lot more classes, but this is the problem... When I try to
compile, I get this error:

/usr/lib/qt-3.1/include/qobject.h: In member function
`Tkinter::Button&
Tkinter::Button::eek:perator=(const Tkinter::Button&)':
/usr/lib/qt-3.1/include/qobject.h:212: error: `QObject&
QObject::eek:perator=(const QObject&)' is private
output/example6.tk_sml.py.main.cpp:38: error: within this context

Seems you are trying to copy your object somewhere. QObject (and thus
also its derived classes) is not copyable.
This is probabaly because I use this in the main-program to
instantiate the Button-class:

int main() {
...
Tkinter::Button compute = Tkinter::Button(top);
...
}

No. This can't be the problem. It doesn' use operator=, but rather the
copy constructor. But that one is also not avaiable.
The compiler compains that the = operator in the QObject class is
private. This seems to mean that I can't instatiate a class which
inherits the QObject class in this way. Can this be right?
Yes.

Can somebody help me get around this?

Write:

Tkinter::Button compute(top);

It does the same, except that it doesn't require a copy constructor.
Or is there something obvious I have done wrong? Can I overload the =
operator of the QObject class or someting?

No. QObjects are not supposed to be copied.
 
M

Martin Jensen

Rolf Magnus said:
Classes derived from QObject should usually at least be able to take a
pointer to a parent QObject and usually a char* for the name. Maybe
there is some special reason why you don't want that here.

Well, the Tk_Object is the parent and I don't really need the name yet. I
changed it so the Tk_Object is the one inheriting the QObject.

Seems you are trying to copy your object somewhere. QObject (and thus
also its derived classes) is not copyable.
Ok.


No. This can't be the problem. It doesn' use operator=, but rather the
copy constructor. But that one is also not avaiable.


Write:

Tkinter::Button compute(top);

It does the same, except that it doesn't require a copy constructor.

Thanks, this works. My problem now is that I can't really do it this way
because I didn't write this code manually. The code in the main program is
generated code, so making too many changes is difficult. I wrote the
codegenerator too, so minor changes I can do. The program actually looks
more like this:

<lots of includes>
Tkinter::Button compute;
int main() {
computer = Tkinter::Button(top); // top is the parent.
}

When I write this:

int main() {
Tkinter::Button compute(top);
}

everything works fine. But because of the way the code is generated, I kind
of have to create the Tkinter::Button before I call the constructor.

This doesn't work:

Tkinter::Button compute;
int main() {
compute(top);
}

I get this error:
output/example6.tk_sml.py.main.cpp: In function `int main(int, char**)':
output/example6.tk_sml.py.main.cpp:43: error: no match for call to `(
Tkinter::Button) (Tkinter::Frame&)'

(Frame is the variable top, the parent class which also inherits Tk_Object.)

Why can't I do this? Calling the copy-constructor works, but not with Qt.
How can I get around this?



Thanks
Martin Jensen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top