PDA

View Full Version : Adding nonQt classes to QtApplication



codebehind
22nd April 2007, 21:02
Hello!

I'm writting my first application (without tutors) and having problems to run it.
Before anything apeares it ends with Segmentation fault.

the structuture is something like this:
I have written a dialog with some buttons, lineedit, a table, etc.
when i run the program (just the GUI) it works fine.
so i make one more step.. I include some of my classes.

when i compile the project it doesn't show any errors. but when i run it... BOOM!
I just put one line in init():
myClass= new MyClass();

i was looking for some getting started on how to add your own classes but found nothing useful.

Another question: My own classes have to be derivered from QObject?

marcel
22nd April 2007, 21:08
Another question: My own classes have to be derivered from QObject?
No, unless you want to emit signals in them or you want implement slots in them.



was looking for some getting started on how to add your own classes but found nothing useful.
Nothing special has to be done with non-qt classes. Most likely you get that segfault because of something wrong that happens in the constructor of your class.

Could we see that class you are trying to instantiate? At least the constructor implementation and the declaration.

regards

viking
23rd April 2007, 05:49
I have written a dialog with some buttons, lineedit, a table, etc.
when i run the program (just the GUI) it works fine.
so i make one more step.. I include some of my classes.

i personally feel, you should try to write your class as the main class and include the GUI object as a member variable in it. this way, all you have to do is initialize the GUI in "your class"'s constructor. all you need to do is call show() after the operations in your class have been done with.

this way, if there's any segfaults arising out of your class, you will be able to debug better. just a suggestion!

high_flyer
23rd April 2007, 09:32
i personally feel, you should try to write your class as the main class and include the GUI object as a member variable in it. this way, all you have to do is initialize the GUI in "your class"'s constructor. all you need to do is call show() after the operations in your class have been done with.

this way, if there's any segfaults arising out of your class, you will be able to debug better. just a suggestion!
This is a design question.
No one can say this is better in his hase, than another solution.
You should always fit your tools and solutions to the problem you have, and not stick to one tool/solution/design without knowing what the requirements are.

codebehind
23rd April 2007, 21:26
Thanx to all!

i've attached the src. to me it seems every thing OK.

marcel
23rd April 2007, 21:30
i've attached the src.

Well, where is it?

codebehind
23rd April 2007, 21:30
Sorry, I forgot to upload.. :P

well, th main makefile is in GUI/ dir

marcel
23rd April 2007, 21:33
Try again! :)
It's the manage attachments button.

codebehind
23rd April 2007, 21:35
Invalid file format... i havent saw the msg

marcel
23rd April 2007, 21:50
Ok, here is your mistake, in file Automat.cpp:



Automat::Automat()
{
_maxStAkcij=MAX_AKC;
_verAkcije= new double(_maxStAkcij);
//_verAkcije= (double *)malloc(_maxStAkcij);

for(int i=0; i < _maxStAkcij; i++)
_verAkcije[i]=1/(float)_maxStAkcij;

_kazen = KAZEN;
_nagrada = NAGRADA;
}
The line:


_verAkcije= new double(_maxStAkcij);
is causing your crash, because will create only ONE double and will assign _maxStAkcij value to it. It crashed in the next for loop, when you populate the array. It will cause a buffer overrun in your array.

You have to use square brackets to allocate an array. Like this:


_verAkcije= new double[_maxStAkcij];
Try it and it will work.

Regards

marcel
23rd April 2007, 21:51
I actually don't have Qt3 installed, so I couldn't properly test your code. But I am almost certain that is the error.

Regards

codebehind
23rd April 2007, 22:08
It works fine. thx again!