Important: Please read the Qt Code of Conduct -

Adding QTforms to existing project

  • Hello all, im new to Qt and have to say that i am more than impressed.
    Easy to install and works out of the box. Lots of examples to get instant hands on experience and what seems to be a thriving community.

    As a noob i am starting out by using the addressbook tutorial to look at the files and the logic behind CPP programming.

    Within the tutorial it doesn't seem to use a form to build the gui and personally i would like to add one so i can see whats going on.

    If anyone has already done this tutorial or can explain how i can add a form to intergrate and display the existing info and buttons etc, it would be a great help in getting me started.

    Thanks in advance

  • Quick walk-through:

    Create a .ui file

    Add the .ui file to your project

    In your class definition add: @namespace Ui {

    class MyClass;


    class MyClass : public QWidget
    Ui::MyClass *ui;

    In your implementation: @

    #include "ui_myclass.h"

    MyClass::MyClass(QWidget *parent) :
    ui(new Ui::MyClass)

    delete ui;

  • Hi Franzk

    This is what i have done:
    Right click on the folder > Add New > QT > but from there i have 4 choices

    • Qt Designer Form Class
    • Qt Designer Form
    • Qt Resource file
    • Qt QML File

    I have tried with both form and form class and i'm not sure which i need. Either way i click next and have the new options dialog which displays with more choices. I am hesitating between a main window and a widget. I believe widget is the way to go.

    As i am sure you already know Form class will create a .h And a .cpp file alongside the ui file, this already contains the info you mentioned above. So i am guessing i need to use a form without class and add the info you provided to the existing .h and .cpp files. Am i along the right lines or am i barking up the wrong tree?
    If i do need a form without class, do i go main window or widget and finaly and probably the most important:

    Could you please explain what and where is the class definition that i need to add the above info to and when you say implementation, which file is that ( i thought it to be main.cpp)

    Thanks again

  • I was under the impression that you wanted to add a ui to an already existing class. If that's not the case, your approach is the proper one. If you create a new form & class using Qt Creator, a .ui, .h and .cpp will be created (say myclass.ui, myclass.h and myclass.cpp). The classes involved are then MyClass, which is a QWidget derived class, MyClass_Ui which is a helper class that doesn't do much except setting up the UI, and Ui::MyClass, which is the same thing as MyClass_Ui, only in a namespace possibly to avoid ugly naming or so. To use your new widget, your application should then instantiate a MyClass. The Ui::MyClass class is of no use to anything other than MyClass itself.

  • Thanks for your replies Franzk, but i have tried and tried since your last post, and no matter what i do i can't get the info to display on the form. I have tried everyway i can possibly think off and its just not happening.

    yes i know i'm a noob and that Cpp is probably the hardest programming language for a noob to start with, and that i have a whole load of learning and reading (which i am doing) to do, but a little extra help from yourself or anyone else would be greatly appreciated.

    Maybe a do this and then do that approach would help a lot.

    I just want to get the existing addressbook tutorial to display on a form.

    Please anyone, Help me, its so frustrating when your at the bottom of the ladder.

  • So you're starting from the Addressbook example. Could you try to describe exactly what you want to add to it? And probably also what you already have done that worked out (in some way or another) for you? That will make it slightly easier for us to give you a recipe.

  • Basicly i am following the tutorial (not the demo) and its all going as planned, except that something in the tutorial doesn't give the same result as the files included. Maybe i did something wrong, but i distinctly feel that the result and the tutorial examples are not the same.
    From there going directly to part 7 i see the whole example is there and already working (easier to learn with a full example).
    With all the files from part 7 open and browsing around i see there is no form for this working example. I basicly want to add a form and transfer the existing info, labels, buttons etc etc so they display within that new form.
    Maybe i have missed something or haven't made myself clear with my explanations.

    Thanks again, i understand it must be frustrating for hardened programmers such as yourself, when someone like myself has no idea where to start. But believe me i want to learn!

  • [quote author="Atom Byte" date="1298729745"]Thanks again, i understand it must be frustrating for hardened programmers such as yourself, when someone like myself has no idea where to start. But believe me i want to learn![/quote]Don't worry about that. Everyone was a beginner once :P. I'll see if I can get to it somewhere this weekend. Else I hope someone else picks up from here.

  • Don't go out of your way man, weekends are precious. Just a little guidence would more than appreciated.

  • I didn't get to doing anything related to this. You got any progress?

  • For the moment no i haven't, its so frustrating. Then i had my machine go crazy, so i have formatted and am in the process of re-installing everything.

    I think its down to installing wx widgets, dev c++, and a multitude of other programming tools to see what would be the easiest to learn with, but causing an eventual conflict somewhere.

    I am at the point where i am about to re-install QT and nothing else (programming).

Log in to reply