How to have multiple widgets initialized from a single form file (no aggregation/inheritance)


  • Qt Champions 2016

    Hello,
    I'd like to have a multiple QWidget instances initialized from a single form file. I do not want, and I will not create a subclass of QWidget - I do not need it. I also would like not to create a separate Ui class instance for each of my widgets if possible. Because of the specifics of my code I'll be making the signal-slot connections by hand, so this would not be a problem.
    Basically I need to have my widgets initialized like this (or something similar):

    Ui::MyFormClass ui;
    QWidget * w1 = new QWidget();
    QWidget * w1 = new QWidget();
    ui.setupUi(w1);
    ui.setupUi(w2);

    I peeked at the code generated by the moc and it seems safe to use only one instance of my Ui class by calling setupUi for each of my widgets, but I'm not sure if this is a stable behavior. By stable I mean it'll not change in the future. Is it acceptable to call setupUi for different QWidget objects? The other thing I looked at is the QUILoader class, which does fit my requirements, still if possible I'd like to use the compiler instead of loading (and parsing) the form files at runtime. What would be the preffered way to proceed?


  • Qt Champions 2016

    Hi
    ui and setupUi is just a normal c++ function and class.
    that new widgets and set the properties according to what is in the UI (XML) file.

    So it's fine to call with different QWidgets as parent as it will just create a new
    instances of the whole UI files's classes.

    So if you don't want UI files, you really don't have to use them at all.

    You can just take the generated code and use as you please.
    Many don't even use the UI files and setup in code only.


  • Qt Champions 2016

    @mrjj said:

    ui and setupUi is just a normal c++ function and class.
    that new widgets and set the properties according to what is in the UI (XML) file.

    I get that. Usually I have one instance of each Ui class corresponding to each of my custom widgets. Currently I'd like to have some minimal setup done for my by the moc, but if possible I'd like to have only one Ui instance to initialize all my QWidget instances. The question is: Is this safe? Will the moc generate some variables that are used later on and mess up the layout/initialization process, because setupUi was called on more than a single widget?

    @mrjj said:

    You can just take the generated code and use as you please.
    Many don't even use the UI files and setup in code only.

    Yes, of course you're right. Any layout, however, I'll need to setup in the code, which is somewhat inflexible and cumbersome. That's why I was asking if it's possible to still use the moc's capabilities but not create a separate Ui::WhateverMyFormIsCalled instance for each of my QWidget objects.


  • Qt Champions 2016

    Hi
    Yes its safe as such. but does have side effects as
    the class variables will point to last run of ui.setupUi;
    so if you do ui->PushButton->X then it only applies to w2.
    so that's the reason it create separate Ui::WhateverMyFormIsCalled instances

    But there are not states or anything like that, used later. (that I have ever seen).

    So even if a bit unusual, it can work.


  • Qt Champions 2016

    Hello,

    Yes its safe as such. but does have side effects as
    the class variables will point to last run of ui.setupUi;

    Yes, I do expect that, but this is not a problem.

    Thank you very much for the help!


  • Qt Champions 2016

    @kshegunov
    You are welcome. Always fun with new ideas :)
    Please mark as solved if possible.


  • Qt Champions 2016

    I would, but there is no button to do that (tools read only "edit" and "delete"). Should I update the original post and add [Solved] or something like this in the beginning?


  • Qt Champions 2016

    @kshegunov
    ok that is strange.
    So the Topic tools button, near reply (not the gear) dont have such function?
    Its sort of in bottom of topic, not top.
    edit: ok u found it. thx.


  • Qt Champions 2016

    Yes indeed. I was looking at the gear button, sorry, my bad.


  • Qt Champions 2016

    @kshegunov
    well you are not the first :)
    At all.


Log in to reply
 

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