I am trying to learn, but this just doesn't make sense to me



  • First, hi people, I am new at the forum so I want to say hi to everybody. Also I am form Chile, so my english not really good, I am sorry about it. (I couldn't find any forum about Qt in spanish)

    Well, I learned some Java the last year. Programing looked something really hard, so, I took it as a challenge, and Java was the first languaje that I found. Well, so, in Java there is some libraries like swt and swing, so I though that Qt would be like the same.

    C++ is really my faorite programing langaje. But it has been really hard to find some libraries for GUIs.

    ok so, my first problem is that I never can compile succesfully, the .pro file the qmake generate is never well done. I have to all the time edit it to make it work, and I don't really know how to do that, I just try diferent things from inteernet untl it works.

    But then, my real problem is that I don't now why in every tutorial that i see they never instantiate the classes.

    #include <QtGui>

    int main(int argc, char *argv[])
    {
    QApplication aplication(argc, argv);
    QLabel whatever("Hello World!");
    whatever.show();

    return aplicacion.exec&#40;&#41;;
    

    }

    That is exsample, so wheer is the;
    QApplication aplication = new QAplication;
    or QLabel whatever = new QLabel;

    Do I need to learn more C++ or something? I don't undertad, it is something between a object and a function or mathod.

    I am sorry if the question are stupid or if I wrote something uncorrectly.

    I hope someone could send a link or something. I really like C++ and I wouldn't like to leave it just cons I can't make GUI's


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    There's the "Qt Spain group":http://qt-project.org/groups/qt_spain that you can contact.

    QApplication is generally allocated on the stack since it will live as long as main. You can allocate it on the heap and delete before returning exec's value but that doesn't provide any advantage and you need to not forget to delete it. QLabel (and generally the other main widgets you will use) follows the same pattern in this case. For the rest I recommend you read Qt's documentation, examples and demos, they provide the basics to build upon (a good book would also be a plus)

    As for code generation/compilation problem, without knowing which platform your on and which Qt version you are using, it's impossible to help you.



  • Yes, you need to understand C++ a little better. You need to grasp the difference between an object and a pointer to an object. You can have an object, like an actual instance of QLabel, or a pointer to an object, like what you are expecting to see (probably from seeing Java).

    The example you show creates two objects directly (QApplication and a QLabel) . These objects live until they go out of scope at the end of the enclosing block (i.e. the closing } ). This type of object is often referred to as "created on the stack" in reference to how the compiler handles it.

    In contrast, a pointer to an object is a variable that holds the address in memory of an object, not the object itself. The new operator creates an object of the specified type and returns the address at which it exists. The pointer variable and the thing it points to have different life times, with the object persisting until the delete operator is used with a pointer to the object. These objects are often referred to as created "on the heap".

    In a Qt program the QApplication object and master window object are generally stack based in main(), and almost everything else in the GUI is heap based. This is clearly shown in the examples. In the case of Qt widgets the delete is handled for you: destroying a parent widget destroys its child widgets before itself.



  • As ChrisW67 say, you need to understand c++ a little better.Especially the idea of "RAII", it is a very important technique to protect your resources(memory, files, mutex and so on) in c++. if you know how to use it(it is easy to learn, not some rocket science), you will become much more happy with c++;If you don't know it, you have a high chance to write buggy, unstable, hard to maintain codes.

    "RAII":http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

    please give c++ primer 5, ch1~ch16 a view.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.