• Forum
  • Lounge
  • My first program which realy do somethin

 
My first program which realy do something!!

Pages: 12
hello there,

after stugling for some months with C++ generic coding I would like to publish my first application which diserves to be called "Application" :D

I've uploaded an immage from my machine, have a look:
http://api.ning.com/files/N2ED9lOsybk-bWjVRxF3zZwYW7a2pEkgx1HozHbkE9pticy4ixbf7v5zxDUuS-DRQKftNvSCa0hFVTAqefqa8ny90rHlqhBb/Capture.JPG

hope you like it lol :D
When you create a QWidget that only needs to live on that scope, like your QLabel, you don't need to allocate it dynamically. You can just use

 
QLabel label("Hello QT!", 0);
Hm, mustn't you also delete label;?
Also what font are you using in the editor?
closed account (1yR4jE8b)
The looks like the default Visual Studio 2010 font, Consolas.
thanks for replys..

Hm, mustn't you also delete label;?

No, if you delete it then label wouldn't show at all :)

The looks like the default Visual Studio 2010 font, Consolas.

it's windows actualy.
subsystem = windows
entry point = mainCRTStartup.

Hmm, maybe I should learn Qt.
You should delete label; at some point (just before the program exits). It's not necessary - a modern OS will do it for you - but it is good style.
acctualy base class will do that for you.
of course this is the case only with Qt classes, that's why you dont have to delete label in that case.
QApplication class will delete it via its destructor.
Okay, in that case it's best not to delete it, because then it will be deleted twice which causes heap corruption.
Actually, I think it wouldn't matter. Qt objects are reference-counted, IIRC.
Is this a different programming language? I'm pretty sure that's a plain old pointer, how can it be reference counted?
The object would be reference-counted, not the pointer. If what I said earlier is true, the QtApplication would reference the object, which would notify it when it gets deleted so it can take it off the list of objects.
QHBoxLayout *horizontalLayout = new QHBoxLayout;I don't like it. It looks like java.
closed account (1yR4jE8b)
Pretty much every widget toolkit has layout managers, and Java took new from C++. I fail to see your point.
@ne555
what I dont like are 'Q' letters for every damn class name, that's only thing why I hate Qt.
it would be better (maybe) if all class names etc would be wrapped into "Qt" namespace like std and boost classes are.

however from another side I like Qt only cos it's made on native C++ and easy to learn.
and most important is very well documented. (at least that's my opinion)



From C++ GUI Programming with Qt 4 Second Edition by Jasmin Blanchette and Mark Summerfield (The book Trolltech uses to teach Qt to its own new hires.)

Page 3:

1
2
3
4
5
6
7
8
9
10
#include <QApplication>
#include <QLabel>

int main(int argc, char *argv);
{
   QApplication app(argc, argv);
   QLabel *label = new QLabel( "Hello Qt!");
   label->show();
   return app.exec();
}



Page 4, commentary on the program:

"For simplicity, we don't bother calling delete on the QLabel object at the end of the main() function. This memory leak is harmless in such a small program, since the memory will be reclaimed by the operating system when the program terminates."


A bad habit to get into.
Last edited on
From what I gathered, that last statement is bollocks. When main ends, app is destroyed and thus, it's destructor is called, therefor all items get deallocated and the day is safed. That is, in this example. When you decided to not return app.exec(), but make it a seperate statement, I would personally put delete statements between said statement and the return 0 statement. Then again, in the given example, the label needn't be dynamically allocated.
In given example object label dosn't require dynamic alocaion but in some real project that object(s) may be very big and you'll probably consider about alocation.
@darkestfright: there is no point. I just don't like the syntax.
¿why do I need to tell the type of the variable twice? Besides, there are objects in c++

A new without delete looks weird (especially considering that is assigned to a raw pointer)
But new and delete in the same scope, looks ugly.
Kyon wrote:
From what I gathered, that last statement is bollocks. When main ends, app is destroyed and thus, it's destructor is called, therefor all items get deallocated and the day is safed.
I'm 99.9% sure that in C++, destructors are not automatically called for dynamically allocated data like in the OP and Alrededor's post.

Kyon wrote:
Then again, in the given example, the label needn't be dynamically allocated.
This. Don't ever dynamically allocate data when you don't need to. Thanks Kyon for pointing this out ;)
Pages: 12