Qt World Summit: Submit your Presentation

Newbie Question - Form design or Code?

  • Hi,

    Just starting out with Qt and using Qt Creator 4.4.1 (Based on Qt 5.9)
    I'm a little fuzzy on the whole "create in forms/create in code" topic.

    So, if I create an object (e.g. button, tickbox, slider etc) in code, it appears, but if I create said object in the form editor, it doesn't create any corresponding code to account for this?
    e.g. If I create a slider and a progress bar, I can link the two graphically, but there will be no line of code connecting the signal to the slot?
    Is this correct?

    If I then go and create something in code, does this play well with the objects created graphically?
    Does this mean that, during debug, you must remember what was created on the form and what was created in code so change the details there?

    I was watching the videos by VoidRealms on YouTube.
    I could have sworn that he created something graphically and then went to change the code that had been magically created as a result of the object that he created.
    Am I mistaken?

    Is this a mode that you can switch on or off?

    Can anyone clear this up for me?



  • Lifetime Qt Champion

    @Uberlinc said in Newbie Question - Form design or Code?:

    it doesn't create any corresponding code

    It does. This is what moc tool is used for. No magic involved :-) Take a look at moc_* files in your build directory after building your project.

  • Lifetime Qt Champion


    Yes code is generated for you. When using designer you get a .ui file that is an XML describing the widgets you are going to use. That file will be processed by uic to generate the corresponding C++ code.

    There's no problem mixing designer created widgets with code create ones.

    No you don't, when debugging you'll be able to easily distinguish the debugger shows the variable names so you can easily spot which are yours and which comes from designer.

    Don't modify the code generated (moc_ or ui_ files), there are already warnings in them for that. These are generated at compilation time, so don't touch them.

    What mode are you thinking about ?

    [edit: Fixed wrong naming, thanks @aha_1980 SGaist]

  • Lifetime Qt Champion

    I fully ack what @SGaist said, with the addition that the form files end in .ui and the generated files start with ui_

    the compiler is named uic, indeed.

  • I am also in full agreeance with @SGaist!

    There is a neat little secret when using form designer and needing connections. You do not need to add signals/slots to the form. Just do the following:

    In header...
    void on_<objectName>_<signal name> (...);

    In source...
    void Class::on_<objectName>_<signal name> (...)
    --- do stuff ---

    This will automaticly pickup your object and connect the signal/slot. No fuss, no muss.

    For example:
    void on_button_clicked (bool checked);

    void Class:on_button_clicked (bool checked)
    --- do something ---

    This is fun-with-designer 101!

Log in to reply