Reuse Qt UI class [SOLVED]
sachi last edited by sachi
I am new to Qt. I have created user class with gui form. now I want to extend this class to Employee class. i need add section for reset password. Is it possible in Qt. I am using Qt 5.
mcosta last edited by
Hi and welcome to devnet,
In general you can reuse ui files in other classes; if you paste some code we could help you in a better way
I have files user.cpp user.h user.ui, this class is used show then user's details on application. now i want to extend this class to employee. employee class have password reset fuction additionally. In employe ui file it must have textfield for password. all other are same as user class. now i want to use user.ui form to create employee.ui form. just want to add some textfield additionally.
@mcosta I want to extend ui and C++ class together. if this possible in qt?
ChrisW67 last edited by
There is no inheritance of UI xml files, and the generated form classes in the Ui:: namespace reflect this.
A few approaches:
- Build a single form containing everything for the employee case and use that UI in both C++ classes. In the UserForm class setup the UI and hide the Employee extra widgets, and in the EmployeeForm set up the UI and just use the form as-is.
- Build a single form containing everything for the employee case and use that UI in a single C++ class that can be constructed in two modes. In one mode hide the extra widgets.
- Build a single form containing only the UserForm widgets. Use that form in both classes but in the EmployeeForm class programmtically add more widgets to the UI.
@ChrisW67 thank you for your help. it was very helpful. i will try your solutions.
@ChrisW67 i have tried your solution i have created user class and employee class by extending user class. but i can't access user class widgets. i have defined them as protected. can you show me correct way to do it?