Nominate our 2022 Qt Champions!

Inheritance of a Designer form class problems

  • Hi!
    I am needing you help. The thing is I have a Qt Designer Form Class A (.ui, .h and .cpp). I have defined some methods using such ui. Now, I want to create a new class B that can define another methods but use the same .ui and have access to the first methods too.
    I was able to use the same UI by inheriting QWidget in the new class B and declaring in B.h:

        Ui::A * ui; //at .h

    And in the constructor doing:

    B::B(QWidget *parent):ui(new Ui::A)

    The problem is that I am missing the access to the methods (remember these methods use the UI of A, so the same .ui is needed). I thought inheriting directly from class A would work, but I am having no luck. The error I get is "invalid use of incomplete type 'class A'".
    Is it possible to do this? All I can find on the internet is about using a .ui file. Nothing like this unfortunately.
    Thank you very much.

  • Lifetime Qt Champion


    Why not just inherit from A ?


    #include "a.h"
    class B: public A
        B (QWidget *parent = nullptr);
    // rest of your code

  • @SGaist Well, I have tried that. Getting the error that is in the last paragraph.

  • Lifetime Qt Champion

    Maybe a silly question but did you include a.h properly ?

    Usually when you get "incomplete type 'class XXX'" it means that you are missing the include of the file that declares 'class XXX'

  • Moderators

    Since Ui::A is a generated class, how exactly are you adding methods to it? Whatsoever you add to the "ui_a.h" file will be overwritten on the next run of the user interface compiler, so maybe elaborate a bit?

    Kind regards.

  • @kshegunov Well, I have a full class. The methods are added to A.h. I use the files that the wizard creates when adding a new Qt Designer Form class.

  • Moderators

    @danifujii said:

    I use the files that the wizard creates when adding a new Qt Designer Form class.

    The question is how, since I personally don't derive from the generated form classes, so can we assume you do in your "a.h"?
    That is, do you have something like this in "a.h":

    #include "ui_a.h"
    class A : public QWidget, public Ui::A
        // Some code

Log in to reply