PDA

View Full Version : Problem of inheritance



giorgik
19th February 2013, 20:55
Hello everyone, I am a inheritance problem with Qt 5.0.1. I have a class declared as:


class WorldGui : public World, public MainWindow

in the MainWindow class I have a function:
void MainWindow::fileLoadCb() that is called through the mechanism of Signal/Slot of MainWindow.
Within this function I would want to call a function of WordlGui
Load( filename );
how can I do ?
I could get an example of code that tell me how to do ?
I'm going crazy with this problem, which I think is not that difficult, but I can not find the solution.

Lykurg
19th February 2013, 21:12
Well your starting point is wrong. How could MainWindow know that it will be base class to WorldGui? So there is no direct possibility to do so.

If you know in MainWindow that it should use a function of WorldGui, then direct implement it there. Or use a complete different design approach.

Santosh Reddy
19th February 2013, 21:26
that is called through the mechanism of Signal/Slot of MainWindow.
Within this function I would want to call a function of WordlGui
When you say signal/slot of MainWindow, it should mean that MainWindow is a QObject based class (if not QWidget), which will lay a restiction on the inheritance sequence.

Please show us the class definition of "World" and "MainWindow", or at-least tell us what those are based on.


how can I do ?
I could get an example of code that tell me how to do ?
I'm going crazy with this problem, which I think is not that difficult, but I can not find the solution.
There are things which one cannot do in Qt (at-least out of box). Please show us more about what are you really looking for.

amleto
20th February 2013, 00:36
Hello everyone, I am a inheritance problem ...
No, you have a design problem. MainWindow should not need to directly call anything on WorldGui. If MainWindow cannot do its job without using WorldGui, then MainWindow is not complete and should not form part of WorldGui - this would be a circular dependency.

giorgik
20th February 2013, 21:16
Thank you all for the answers. In fact, I poorly designed classes. The solution is to use a class WorldGui that does not derive from MainWindow, but use a pointer to MainWindow as a member variable of WorldGui. Then use the technique of signal/slot to communicate MainWindow with WorldGui.

amleto
20th February 2013, 23:32
no, just no.

Santosh Reddy
21st February 2013, 05:02
The solution is to use a class WorldGui that does not derive from MainWindow, but use a pointer to MainWindow as a member variable of WorldGui. Then use the technique of signal/slot to communicate MainWindow with WorldGui.
I don't see any problem with this approach, go ahead and do it. One note is to inherit WorldGui from QObject

lanz
21st February 2013, 06:44
no, just no.

Could you please elaborate on that? I use similar approach sometimes and it would be good to hear about downsides.

anda_skoa
22nd February 2013, 14:14
I would probably do it the other way around, i.e. letting the UI class have a pointer to the non-UI/logic class. This way the logic is more independent of the concrete UI implementation and UI can be changed more freely.

Cheers,
_

amleto
22nd February 2013, 22:50
Could you please elaborate on that? I use similar approach sometimes and it would be good to hear about downsides.
by changing inheritance for composition the overall design hasn't changed, just the implementation. It appears that the two concrete classes still cannot do a job by themselves, and still require each other to do a job.