CopperSpice API
1.9.2
|
A UI file is a human readable XML document. The elements in the file contain a description of the widgets and controls for a particular form which is then used to create a graphical user interface window or dialog. The UI files are used at compile time to generate source code, which is then linked with your application.
Although you can create a UI file by hand this approach is not recommended since it is error prone and tedious. The preferred approach is to use the CS Designer application to create your UI files.
UI file when building your application. In the process of compiling your program the UIC program will parse each UI file and generate a corresponding header file containing a C++ class. This class contains the following data members and methods.
The generated header is used in your application in one of the following ways.
To use the direct approach include the "ui_calculatorform.h" header in main.cpp. The first step is to create a QWidget and then construct the calculator UI by calling the generated setupUI() method. This will setup the user interface and any signal and slot connections which were defined in the UI file.
The direct approach provides a quick and easy way to use a UI in your application for simple user interfaces. If your UI requires a custom slot method or more complicated connections, then the single inheritance approach will be required.
To use the single inheritance approach create a new class which inherits from the top level widget. In this example we inherit from QWidget. If the UI form had been designed on a QMainWindow or a QDialog, then the corresponding classes should be used as the base class.
There are two ways to implement the single inheritance approach. The first way involves declaring a data member of type "Ui::CalculatorForm". The second way is to declare a data member which is a pointer to type "Ui::CalculatorForm".
The constructor of the My_CalculatorForm class is responsible for calling the m_ui.setupUi() method which will instantiate all of the widgets and controls required to display the user interface.
The private "m_ui" variable can be used to access the elements of the user interface. The first parameter in the call to connect() is the Sender and in this example it will be either m_ui.spinBox1 or m_ui.spinBox2.
The corresponding source file is shown below.
The other way this code can be written is to declare the private "m_ui" variable as a pointer to the Ui::CalculatorForm. Using a pointer for m_ui means you will need to implement a destructor method.
One advantage of this second approach is the "class CalculatorForm" could be forward declared in the UI namespace, in my_calculatorform.h. In this case both "ui_calculatorform.h" and "my_calculatorform.h" need to be included in the cpp file.
The corresponding source file is shown below.