Редактирование: Qt:Документация 4.3.2/designer-using-a-component

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск
Внимание: Вы не представились системе. Ваш IP-адрес будет записан в историю изменений этой страницы.
Правка может быть отменена. Пожалуйста, просмотрите сравнение версий, чтобы убедиться, что это именно те изменения, которые вас интересуют, и нажмите «Записать страницу», чтобы изменения вступили в силу.
Текущая версия Ваш текст
Строка 4: Строка 4:
=Using a Component in Your Application<br />=
=Using a Component in Your Application<br />=
-
*[[#compile-time-form-processing | Compile Time Form Processing]]
+
*[[#Compile-Time-Form-Processing | Compile Time Form Processing]]
-
**[[#the-direct-approach | The Direct Approach]]
+
**[[#The-Direct-Approach | The Direct Approach]]
-
**[[#the-single-inheritance-approach | The Single Inheritance Approach]]
+
**[[#The-Single-Inheritance-Approach | The Single Inheritance Approach]]
-
**[[#the-multiple-inheritance-approach | The Multiple Inheritance Approach]]
+
**[[#The-Multiple-Inheritance-Approach | The Multiple Inheritance Approach]]
-
 
+
*[[#Run-Time-Form-Processing | Run Time Form Processing]]
-
*[[#run-time-form-processing | Run Time Form Processing]]
+
**[[#The-Uitools-Approach | The UiTools Approach]]
-
**[[#the-uitools-approach | The UiTools Approach]]
+
*[[#Automatic-Connections | Automatic Connections]]
-
 
+
**[[#A-Dialog-Without-Auto-Connect | A Dialog Without Auto-Connect]]
-
*[[#automatic-connections | Automatic Connections]]
+
**[[#Widgets-and-Dialogs-with-Auto-Connect | Widgets and Dialogs with Auto-Connect]]
-
**[[#a-dialog-without-auto-connect | A Dialog Without Auto-Connect]]
+
-
**[[#widgets-and-dialogs-with-auto-connect | Widgets and Dialogs with Auto-Connect]]
+
With Qt's integrated build tools, [[Qt:Документация 4.3.2/qmake-manual | qmake]] and [[Qt:Документация 4.3.2/uic#uic | uic]], the code for user interface components created with ''Qt Designer'' is automatically generated when the rest of of your application is built. Forms can be included and used directly from your application, or you can use them to extend subclasses of standard widgets. These forms can be processed at compile time or at run time, depending on the approach used.
With Qt's integrated build tools, [[Qt:Документация 4.3.2/qmake-manual | qmake]] and [[Qt:Документация 4.3.2/uic#uic | uic]], the code for user interface components created with ''Qt Designer'' is automatically generated when the rest of of your application is built. Forms can be included and used directly from your application, or you can use them to extend subclasses of standard widgets. These forms can be processed at compile time or at run time, depending on the approach used.
-
<div id="compiletimeformprocessing"></div><div id="compile-time-form-processing"></div>
+
 
==Compile Time Form Processing==
==Compile Time Form Processing==
A compile time processed form can be used in your application with any of the following approaches:
A compile time processed form can be used in your application with any of the following approaches:
Строка 25: Строка 23:
*In the single inheritance approach, you subclass the form's base class ([[Qt:Документация 4.3.2/qwidget | QWidget]] or [[Qt:Документация 4.3.2/qdialog | QDialog]], for example), and include a private instance of the form's user interface object.
*In the single inheritance approach, you subclass the form's base class ([[Qt:Документация 4.3.2/qwidget | QWidget]] or [[Qt:Документация 4.3.2/qdialog | QDialog]], for example), and include a private instance of the form's user interface object.
*In the multiple inheritance approach, you subclass from both the form's base class and the form's user interface object. This allows the widgets defined in the form to be used directly from within the scope of the subclass.
*In the multiple inheritance approach, you subclass from both the form's base class and the form's user interface object. This allows the widgets defined in the form to be used directly from within the scope of the subclass.
-
<div id="thedirectapproach"></div><div id="the-direct-approach"></div>
+
 
===The Direct Approach===
===The Direct Approach===
To demonstrate how user interface components can be used straight from ''Qt Designer'', we create a simple Calculator Form application, based on the original [[Qt:Документация 4.3.2/designer-calculatorform | Calculator Form]] example.
To demonstrate how user interface components can be used straight from ''Qt Designer'', we create a simple Calculator Form application, based on the original [[Qt:Документация 4.3.2/designer-calculatorform | Calculator Form]] example.
Строка 57: Строка 55:
This approach provides a quick and easy way to use simple, self-contained components in your applications, but many components created with ''Qt Designer'' will need to be integrated more closely with the rest of the application code. For instance, the <tt>CalculatorForm</tt> code provided above will compile and run, but the [[Qt:Документация 4.3.2/qspinbox | QSpinBox]] objects will not interact with the [[Qt:Документация 4.3.2/qlabel | QLabel]] as we require a custom slot to carry out the add operation and display the result in the [[Qt:Документация 4.3.2/qlabel | QLabel]]. To achieve this, we need to subclass a standard Qt widget.
This approach provides a quick and easy way to use simple, self-contained components in your applications, but many components created with ''Qt Designer'' will need to be integrated more closely with the rest of the application code. For instance, the <tt>CalculatorForm</tt> code provided above will compile and run, but the [[Qt:Документация 4.3.2/qspinbox | QSpinBox]] objects will not interact with the [[Qt:Документация 4.3.2/qlabel | QLabel]] as we require a custom slot to carry out the add operation and display the result in the [[Qt:Документация 4.3.2/qlabel | QLabel]]. To achieve this, we need to subclass a standard Qt widget.
-
<div id="thesingleinheritanceapproach"></div><div id="the-single-inheritance-approach"></div>
+
 
===The Single Inheritance Approach===
===The Single Inheritance Approach===
In this approach, we subclass a Qt widget and set up the user interface from within the constructor. Components used in this way expose the widgets and layouts used in the form to the Qt widget subclass, and provide a standard system for making signal and slot connections between the user interface and other objects in your application.
In this approach, we subclass a Qt widget and set up the user interface from within the constructor. Components used in this way expose the widgets and layouts used in the form to the Qt widget subclass, and provide a standard system for making signal and slot connections between the user interface and other objects in your application.
Строка 93: Строка 91:
The main advantages of this approach are its simple use of inheritance to provide a [[Qt:Документация 4.3.2/qwidget | QWidget]]-based interface, and its encapsulation of the user interface widget variables within the <tt>ui</tt> data member. We can use this method to define a number of user interfaces within the same widget, each of which is contained within its own namespace, and overlay (or "compose") them. This approach can be used to create individual tabs from existing forms, for example.
The main advantages of this approach are its simple use of inheritance to provide a [[Qt:Документация 4.3.2/qwidget | QWidget]]-based interface, and its encapsulation of the user interface widget variables within the <tt>ui</tt> data member. We can use this method to define a number of user interfaces within the same widget, each of which is contained within its own namespace, and overlay (or "compose") them. This approach can be used to create individual tabs from existing forms, for example.
-
<div id="themultipleinheritanceapproach"></div><div id="the-multiple-inheritance-approach"></div>
+
 
===The Multiple Inheritance Approach===
===The Multiple Inheritance Approach===
Forms created with ''Qt Designer'' can be subclassed along with a standard [[Qt:Документация 4.3.2/qwidget | QWidget]]-based class. This approach makes all the user interface components defined in the form directly accessible within the scope of the subclass, and enables signal and slot connections to be made in the usual way with the [[Qt:Документация 4.3.2/qobject#connect | connect()]] function.
Forms created with ''Qt Designer'' can be subclassed along with a standard [[Qt:Документация 4.3.2/qwidget | QWidget]]-based class. This approach makes all the user interface components defined in the form directly accessible within the scope of the subclass, and enables signal and slot connections to be made in the usual way with the [[Qt:Документация 4.3.2/qobject#connect | connect()]] function.
Строка 124: Строка 122:
Subclassing using multiple inheritance gives us more direct access to the contents of the form, is slightly cleaner than the single inheritance approach, but does not conveniently support composition of multiple user interfaces.
Subclassing using multiple inheritance gives us more direct access to the contents of the form, is slightly cleaner than the single inheritance approach, but does not conveniently support composition of multiple user interfaces.
-
<div id="runtimeformprocessing"></div><div id="run-time-form-processing"></div>
+
 
==Run Time Form Processing==
==Run Time Form Processing==
Alternatively, forms can be processed at run time, producing dynamically-generated user interfaces. This can be done using the [[Qt:Документация 4.3.2/qtuitools | QtUiTools]] module, which provides the [[Qt:Документация 4.3.2/quiloader | QUiLoader]] class to handle forms created with ''Qt Designer''.
Alternatively, forms can be processed at run time, producing dynamically-generated user interfaces. This can be done using the [[Qt:Документация 4.3.2/qtuitools | QtUiTools]] module, which provides the [[Qt:Документация 4.3.2/quiloader | QUiLoader]] class to handle forms created with ''Qt Designer''.
-
<div id="the-uitools-approach"></div>
+
 
===The UiTools Approach===
===The UiTools Approach===
A resource file containing a <tt>.ui</tt> file is required to process forms at run time. Also, the application needs to be configured to use the [[Qt:Документация 4.3.2/qtuitools | QtUiTools]] module. This is done by including the following declaration in a qmake project file, ensuring that the application is compiled and linked appropriately.
A resource file containing a <tt>.ui</tt> file is required to process forms at run time. Also, the application needs to be configured to use the [[Qt:Документация 4.3.2/qtuitools | QtUiTools]] module. This is done by including the following declaration in a qmake project file, ensuring that the application is compiled and linked appropriately.
Строка 153: Строка 151:
     ui_lineEdit = qFindChild<QLineEdit*>(this, "lineEdit");</source>  
     ui_lineEdit = qFindChild<QLineEdit*>(this, "lineEdit");</source>  
Processing forms at run-time gives the user the freedom to change a program's user interface, just by changing the <tt>.ui</tt> file. This is useful when customizing programs to suit various user needs.
Processing forms at run-time gives the user the freedom to change a program's user interface, just by changing the <tt>.ui</tt> file. This is useful when customizing programs to suit various user needs.
-
<div id="automaticconnections"></div><div id="automatic-connections"></div>
+
 
==Automatic Connections==
==Automatic Connections==
The signals and slots connections defined for compile time or run time forms can either be set up manually or automatically, using [[Qt:Документация 4.3.2/qmetaobject | QMetaObject]]'s ability to make connections between signals and suitably-named slots.
The signals and slots connections defined for compile time or run time forms can either be set up manually or automatically, using [[Qt:Документация 4.3.2/qmetaobject | QMetaObject]]'s ability to make connections between signals and suitably-named slots.
Generally, in a [[Qt:Документация 4.3.2/qdialog | QDialog]], if we want to process the information entered by the user before accepting it, we need to connect the clicked() signal from the '''OK''' button to a custom slot in our dialog. We will first show an example of the dialog in which the slot is connected by hand then compare it with a dialog that uses automatic connection.
Generally, in a [[Qt:Документация 4.3.2/qdialog | QDialog]], if we want to process the information entered by the user before accepting it, we need to connect the clicked() signal from the '''OK''' button to a custom slot in our dialog. We will first show an example of the dialog in which the slot is connected by hand then compare it with a dialog that uses automatic connection.
-
<div id="adialogwithoutautoconnect"></div><div id="a-dialog-without-auto-connect"></div>
+
 
===A Dialog Without Auto-Connect===
===A Dialog Without Auto-Connect===
We define the dialog in the same way as before, but now include a slot in addition to the constructor:
We define the dialog in the same way as before, but now include a slot in addition to the constructor:
Строка 193: Строка 191:
  }</source>  
  }</source>  
This custom slot does the minimum necessary to ensure that the data entered by the user is valid - it only accepts the input if a name was given for the image.
This custom slot does the minimum necessary to ensure that the data entered by the user is valid - it only accepts the input if a name was given for the image.
-
<div id="widgetsanddialogswithautoconnect"></div><div id="widgets-and-dialogs-with-auto-connect"></div>
+
 
===Widgets and Dialogs with Auto-Connect===
===Widgets and Dialogs with Auto-Connect===
Although it is easy to implement a custom slot in the dialog and connect it in the constructor, we could instead use [[Qt:Документация 4.3.2/qmetaobject | QMetaObject]]'s auto-connection facilities to connect the '''OK''' button's clicked() signal to a slot in our subclass. <tt>uic</tt> automatically generates code in the dialog's <tt>setupUi()</tt> function to do this, so we only need to declare and implement a slot with a name that follows a standard convention:
Although it is easy to implement a custom slot in the dialog and connect it in the constructor, we could instead use [[Qt:Документация 4.3.2/qmetaobject | QMetaObject]]'s auto-connection facilities to connect the '''OK''' button's clicked() signal to a slot in our subclass. <tt>uic</tt> automatically generates code in the dialog's <tt>setupUi()</tt> function to do this, so we only need to declare and implement a slot with a name that follows a standard convention:

Пожалуйста, обратите внимание, что все ваши добавления могут быть отредактированы или удалены другими участниками. Если вы не хотите, чтобы кто-либо изменял ваши тексты, не помещайте их сюда.
Вы также подтверждаете, что являетесь автором вносимых дополнений, или скопировали их из источника, допускающего свободное распространение и изменение своего содержимого (см. Wiki.crossplatform.ru:Авторское право). НЕ РАЗМЕЩАЙТЕ БЕЗ РАЗРЕШЕНИЯ ОХРАНЯЕМЫЕ АВТОРСКИМ ПРАВОМ МАТЕРИАЛЫ!


Шаблоны, использованные на текущей версии страницы: