Dialog UI and heritage



  • Hi Sorry if this question is too simple :)

    I have an class
    namespace Ui { class DialogInvoiceList; }
    class DialogInvoiceList : public QDialog { ...}

    I want to create an other class from DialogInvoiceList
    class DialogPurchase : public DialogInvoiceList

    Here it's ok, but How to access to ui functions in my DialogPurchase class?
    ui -> dateEdit -> setDate( QDate::currentDate());


  • Qt Champions 2016

    Hi Sorry if this question is too simple :)
    No such thing :)

    well the UI is declared private and you inherited public.
    The best solution (iMHO) is to define access funtions in
    DialogInvoiceList
    Like
    SetDate( QDate &)
    and use those from DialogPurchase
    You can also give access to the ui but it get
    using acces function will also let you validate stuff and overall
    makes better design than let all access the widgets directly.



  • The best solution (iMHO) is to define access funtions in DialogInvoiceList
    Like SetDate( QDate &) and use those from DialogPurchase

    I'm agree with you, but I'm the only dev in this case and I want to access directly if possible, I tried to declare ui in protected but I have some error :)

    D:\code\mcercle-trunk\src\dialog\dialogpurchase.cpp:17: error: invalid use of incomplete type 'class Ui::DialogInvoiceList' ui -> dateEdit -> setDate( QDate::currentDate());
    and
    D:\code\mcercle-trunk\src\dialog\dialoginvoicelist.h:10: error: forward declaration of 'class Ui::DialogInvoiceList' class DialogInvoiceList;


  • Qt Champions 2016

    the implementation of your function must be in the .cpp file.

    so where you call ui -> dateEdit -> setDate( QDate::currentDate());
    should be in a .cpp file and not .h

    Is it in .cpp?



  • @mrjj Yes, not in header file


  • Qt Champions 2016

    @cfdev
    ok, so not that error.
    Well, I have not tried to move it to protected.
    I can try it a bit later and see :)



  • @cfdev How did you embed Ui::DialogInvoiceList into your DialogInvoiceList? In your example the ui-class is nowhere!

    Anyways, if you wanted to create a hierarchy of classes that derive from your ui-class, then the logical thing I'd do:

    namespace Ui { class DialogInvoiceList; }
    
    class DialogInvoiceList: protected DialogInvoiceList, public QDialog
    {
        Q_OBJECT
    public:
        explicit DialogInvoiceList(QWidget* parent) 
            : QDialog(parent)
        {
            setupUi(this);
        }
    }
    
    class DialogPurchase : protected DialogInvoiceList
    {
        Q_OBJECT
    public:
        explicit DialogPurchase(QWidget* parent)
            : DialogInvoiceList(parent)
        {}
    }
    

    I haven't tested, but I'm almost certain you can now access anything in the ui class directly, i.e. a member checkbox can be accessed directly in the DialogPurchase class.



  • @cfdev To be more precize, you'd use the 'Multiple Inheritance' approach described here http://doc.qt.io/qt-5/designer-using-a-ui-file.html#the-multiple-inheritance-approach



  • @Jakob If I use this code, I've some error 'inaccessible' :

    D:\code\qt\4.8.7\src\gui\dialogs\qdialog.h:90: error: 'void QDialog::setModal(bool)' is inaccessible void setModal(bool modal);

    D:\code\mcercle-trunk\src\mainwindow.cpp:323: error: 'QDialog' is not an accessible base of 'DialogPurchase'
    ...

    PS: What is the markdown code to use the syntax color ?!



  • It's ok like this:

    namespace Ui { class DialogInvoiceList; }

    class DialogInvoiceList: public QDialog, protected Ui::DialogInvoiceList
    {
    Q_OBJECT
    public:
    explicit DialogInvoiceList(QWidget* parent)
    : QDialog(parent)
    {
    setupUi(this);
    }
    }

    class DialogPurchase : public DialogInvoiceList
    {
    Q_OBJECT
    public:
    explicit DialogPurchase(QWidget* parent)
    : DialogInvoiceList(parent)
    {}
    }

    Many Thanks ;)



  • @cfdev Aaahhh, of course, that actually rings some bells somewhere in the back of my head. I think I read somewhere that if you use multiple inheritance, the QObject-derived class must be the first in the list. This has something to do with the moc compiler I think.

    You get the syntax highlighted using a single backtick for inline highlighting and backtick plus return for a code block.



  • @cfdev Btw, please remember to mark as 'Solved' using the 'Topic Tools' at the bottom right corner of the topic thread.



  • @cfdev Damn browser cache, never mind the remark as marking as 'Solved' - you already did .......


  • Lifetime Qt Champion

    Hi,

    Just a side note, you have now a "Code" button with </> as an icon to help you write code blocks


Log in to reply
 

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