Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to have multiple widgets initialized from a single form file (no aggregation/inheritance)
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
formswidgetsmoc
10 Posts 2 Posters 5.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by kshegunov
    #1

    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?

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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.

      kshegunovK 1 Reply Last reply
      0
      • mrjjM mrjj

        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.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @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.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          kshegunovK 1 Reply Last reply
          1
          • mrjjM mrjj

            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.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            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!

            Read and abide by the Qt Code of Conduct

            mrjjM 1 Reply Last reply
            0
            • kshegunovK kshegunov

              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!

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              kshegunovK 1 Reply Last reply
              0
              • mrjjM mrjj

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

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                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?

                Read and abide by the Qt Code of Conduct

                mrjjM 1 Reply Last reply
                1
                • kshegunovK kshegunov

                  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?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @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.

                  kshegunovK 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @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.

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #9

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

                    Read and abide by the Qt Code of Conduct

                    mrjjM 1 Reply Last reply
                    1
                    • kshegunovK kshegunov

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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved