![]() Registered Member ![]()
|
Baaah. The following is driving me mad for quite some time now. First off, I'm not an experienced Qt/C++ programmer, so please forgive me any stupidity or the overlooking blatant obviousness.
Basically I'm building a Qt application which uses a mainwindow designed in Qt Designer. I have a lot of functions belonging to my MainWindow class which makes the class bloated and many of those functions could be reused by other classes. What I wanted is some sort of separate base class (called DataModel) which provides several methods for manipulating GUI elements (e.g. QTreeViewWidgets which need to be filled with data from a database) to be called by different classes. The problem is I can't seem to find out how to access those GUI elements from a separate class. So this might mean that it's either bad practice and should be avoided or I haven't found the holy grail yet. Any ideas? [size=x-small]Or am I too vague...[/size]
I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
|
![]() Administrator ![]()
|
This should be possible. As long as the other classes have access to the object, they should be able to do it, by passing that to the DataModel class. ( or make the DataModel class handle the Window, avoiding that )
Hope that helped you, I can probably explain it better if you post the sources.
KDE Sysadmin
[img]content/bcooksley_sig.png[/img] |
![]() Registered Member ![]()
|
I know that in theory you should pass the object, in practice the only that happens is that my application segfaults.
![]() Below are my mainwindow.h, mainwindow.cpp, datamodel.h and datamodel.cpp. I've stripped out most of the unnecessary functions, declarations, etc. for the sake of clarity. mainwindow.h
mainwindow.cpp
datamodel.h
datamodel.cpp
I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
|
![]() Administrator ![]()
|
Looks fine to me, although I wouldn't make MainWindow a subclass of Ui::MainWindow. You should be able to pass around a pointer to MainWindow.ui without any problems ( unless it somehow is destroyed, which would require destroying MainWindow )
Without seeing how it is actually crashing, I would speculate that either the pointer became invalid, or you were passing around the Ui::MainWindow instance directly ( copying it ) instead of a pointer.
Last edited by bcooksley on Wed Apr 08, 2009 10:10 am, edited 1 time in total.
KDE Sysadmin
[img]content/bcooksley_sig.png[/img] |
![]() Registered Member ![]()
|
I've modified my code a bit and try to access a test function belonging to the MainWindow class, to see if the pointer is valid:
When building I get this error:
I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
|
![]() KDE Developer ![]()
|
As the compiler tells you, you try to call a member function (a.k.a method) without having an object to call on.
Cheers, _
anda_skoa, proud to be a member of KDE forums since 2008-Oct.
|
![]() Registered Member ![]()
|
I think that's the root of my problem; not having the right object passed. The declaration above (MainWindow *mainWindow = MainWindow::test();) doesn't seem to be correct.
The following code gives me this error (error: expected primary-expression before ';' token), which seems logical as well:
I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
|
![]() Administrator ![]()
|
This should work:
Then pass the mainWindow pointer to anything that needs it, as part of the function call that uses MainWindow functions.
KDE Sysadmin
[img]content/bcooksley_sig.png[/img] |
![]() Registered Member ![]()
|
It builds that way, but the resulting build won't start. It doesn't crash, but rather seems to stay looping forever.
I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
|
![]() Administrator ![]()
|
You wouldn't be trying to create the MainWindow pointer inside the MainWindow constructor would you?
If so, try this instead:
KDE Sysadmin
[img]content/bcooksley_sig.png[/img] |
![]() Registered Member ![]()
|
I'm afraid not.
![]()
Last edited by mensch on Thu Apr 09, 2009 12:55 pm, edited 1 time in total.
I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
|
![]() Registered Member ![]()
|
Whoops! I know what the culprit was and removed it. I had an old call to my dataModel class still in the MainWindow class. So in a sense I was calling it in the MainWindow constructor. All is well know, on to the next obstacle.
Thanks! Is there a way to call dataModel in my MainWindow class, without triggering the eternal looping?
Last edited by mensch on Thu Apr 09, 2009 12:56 pm, edited 1 time in total.
I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
|
![]() KDE Developer ![]()
|
You can just pass its instance to the MainWindow constructor or the other way around
Cheers, _
anda_skoa, proud to be a member of KDE forums since 2008-Oct.
|
![]() Registered Member ![]()
|
That seem to work.
However, if I try to call m_model->test() from my mainwindow class the application crashes due to a EXC_BAD_ACCESS error. The test function in dataModel should manipulate a QTreeWidget belonging to MainWindow (at the moment it should just printing the widget's name). mainwindow.h
mainwindow.cpp
datamodel.cpp
I have forced myself to contradict myself in order to avoid conforming to my own taste. Marcel Duchamp
|
![]() Administrator ![]()
|
This is because you never initialised m_model.
Change the MainWindow constructor as follows:
Then change the following
Into this
Finally add the following before calling m_model->test();
And change this:
To this:
Hope this helps.
Last edited by bcooksley on Thu Apr 09, 2009 9:34 pm, edited 1 time in total.
KDE Sysadmin
[img]content/bcooksley_sig.png[/img] |
Registered users: Bing [Bot], Google [Bot], Sogou [Bot]