qtquick-uiforms-tutorial.qdoc 18.5 KB
Newer Older
Leena Miettinen's avatar
Leena Miettinen committed
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Leena Miettinen's avatar
Leena Miettinen committed
5
**
6
** This file is part of the Qt Creator documentation.
Leena Miettinen's avatar
Leena Miettinen committed
7
**
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Leena Miettinen's avatar
Leena Miettinen committed
15
**
16
** GNU Free Documentation License Usage
Leena Miettinen's avatar
Leena Miettinen committed
17 18
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
19 20 21 22
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
Leena Miettinen's avatar
Leena Miettinen committed
23 24
**
****************************************************************************/
25

Leena Miettinen's avatar
Leena Miettinen committed
26 27
/*!
    \contentspage {Qt Creator Manual}
28
    \previouspage {Creating a Mobile Application}
Leena Miettinen's avatar
Leena Miettinen committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    \example uiforms
    \nextpage creator-project-managing.html

    \title Using Qt Quick UI Forms

    This tutorial describes how to develop an application that uses \e ui.qml
    files to separate the application logic from the UI. The tutorial uses \QMLD
    to implement a simplified version of the \l{Qt Quick Controls - UI Forms}
    {UI Forms example}, which provides an interface to a customer database and
    is purely written in QML and JavaScript.

    \image qmldesigner-uiforms-example.png

    \e {UI forms} consist of \e .qml and \e .js files that implement the
    business logic, and corresponding \e .ui.qml files that only contain the
    purely declarative description of the UI. The \e .ui.qml files should be
    edited only in the \uicontrol Design mode of \QC. However, \QMLD does not
    fully support all QML controls, such as the TableView, so you sometimes need
    to edit UI forms also in the \uicontrol Edit mode. You can keep this to the
    minimum by creating custom QML types that you edit in the \uicontrol Edit
    mode.

51
    \include creator-tutorial-create-qq-project.qdocinc
Leena Miettinen's avatar
Leena Miettinen committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    \section1 Creating the UI Forms Main View

    The main view of the application displays a customer list in a table view
    and detailed information about the selected customer in a tab view.

    \image qmldesigner-uiforms-example-main-view.png

    To create the main view:

    \list 1

        \li In the \uicontrol Projects view (1), double-click the
            \e MainForm.ui.qml file to open it in \QMLD.

        \li In the \uicontrol Navigator (2), select the \uicontrol RowLayout and
            press \key Delete to delete it.

        \li In \uicontrol Library > \uicontrol {QML Types} (3), select
            \uicontrol SplitView and drag and drop it to the \uicontrol Item in
            the navigator.

        \li Select the split view in the navigator, then select the
            \uicontrol Layout tab in \uicontrol Properties (4), and then click
76
            the \inlineimage anchor-fill-normal.png
Leena Miettinen's avatar
Leena Miettinen committed
77 78 79 80 81 82
            (\uicontrol {Fill to Parent}) button to anchor the split view to the
            item.

        \li Drag and drop a \uicontrol TableView and a \uicontrol {Tab View}
            from the library to the split view in the navigator.

83
        \li Select the \inlineimage export_unchecked.png
Leena Miettinen's avatar
Leena Miettinen committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
            (\uicontrol Export) button in the navigator to export the table view
            as a property.

        \li Right-click \uicontrol TabView to open the context menu and select
            \uicontrol {Add Tab} to create a Tab element.

            \QC creates the element as a separate QML file with the name that
            you enter in the dialog. By default, the element is called
            \uicontrol Tab.

        \li Select the tab in the navigator and enter \b {Customer Settings} in
            the \uicontrol Title field in the properties.

        \li Press \key Ctrl+C to copy the tab to the clipboard, and then press
            \key Ctrl+V twice to create two more tabs that you name
            \b {Customer Notes} and \b {Customer History}. \QC uses the \l Tab
            type in the \e MainForm.ui.qml file. You will create separate UI
            forms for the tab contents later.

    \endlist

    \section2 Editing the Table View

    \QMLD does not support adding columns to TableView types, and therefore, you
    must use the code editor to add them. To keep editing the \e MainForm.ui.qml
    file in the \uicontrol Edit mode to the minimum, move the TableView type to
    a separate QML file called \e CustomerTableView.qml:

    \list 1

        \li Click \uicontrol Edit to open \e MainForm.ui.qml in \uicontrol Edit
            mode.

        \li To move the TableView type to a separate QML file, right-click it
            and select \uicontrol Refactoring >
            \uicontrol {Move Component into Separate File}.

        \li Add the code from the \l {uiforms/CustomerTableView.qml}
            {CustomerTableView.qml} example file to the file that \QC creates
            for you.

    \endlist

    \section1 Implementing the Application Logic for the Main View

    The new project wizard adds boilerplate code to the \e main.qml file to
    create menu items and push buttons. Modify the boilerplate code by removing
    obsolete code and by adding new code. You removed the push buttons from the
    UI Form, so you also need to remove the corresponding code from
    \e main.qml (or the application cannot be built).

    You will want to keep the dialog box and menu bar, but change their
    contents, as instructed later.

    Edit the \e main.qml file in the code editor, as described in the following
    sections.

    \section2 Specifying Main View Size

    The wizard creates an ApplicationWindow type and a MainForm type that
    specify the application main view. Enter the application name as the
    value of the \c title property.

    Clean up the MainForm code by removing the obsolete lines that call
    functions when buttons are clicked:

    \badcode
    MainForm {
        anchors.fill: parent
        button1.onClicked: messageDialog.show(qsTr("Button 1 pressed"))
        button2.onClicked: messageDialog.show(qsTr("Button 2 pressed"))
    }
    \endcode

    Remove the \c width and \c height properties from the ApplicationWindow
    type and use a Layout type in the MainForm type to set the minimum and
    preferred size of the main view.

    To use the Layouts, import QtQuick Layouts:

    \quotefromfile uiforms/main.qml
    \skipto QtQuick.Layouts
    \printline Layouts

    Then specify the following properties for the MainForm:

    \skipto MainForm
    \printuntil Layout.preferredHeight

    \section2 Creating the Table View Model

    Use a list model to display customer data in the table view. Because the
    list model is accessed from several different \e .qml files, access it
    through a singleton type defined in \e CustomerModelSingleton.qml and
    registered in \e main.cpp. This way, you do not have to rely on the QML
    context scoping rules to access the list model.

    \list 1

        \li In the \uicontrol Projects view, right-click \uicontrol qml.qrc and
            select \uicontrol {Add New} > \uicontrol Qt >
            \uicontrol {QML File (Qt Quick 2)} to create the
            \e CustomerModelSingleton.qml file and to add it to the project.

        \li Copy the implementation from \l{uiforms/CustomerModelSingleton.qml}
            {CustomerModelSingleton.qml}.

        \li Add the following code to the MainForm in \e main.qml to access the
            list model:

            \quotefromfile uiforms/main.qml
            \skipto tableView1.model: CustomerModel
            \printuntil tableView1.selection

        \li To register the singleton type in the \e main.cpp file, include the
            Qt QML module and call the \c qmlRegisterSingletonType() function in
            the initialization function:

            \quotefromfile uiforms/main.cpp
            \dots
            \skipto QtQml
            \printuntil "CustomerModel");

        \li To use the registered singleton type in \e main.qml, you must import
            the singleton type:

            \quotefromfile uiforms/main.qml
            \skipto my.customermodel.singleton
            \printline 1.0

    \endlist

    \section1 Creating Tabs

    You can use the new file wizard to create UI forms that specify tab
    contents and functionality. You set the QML files as sources for the tabs
    in the \e MainForm.ui.qml file and modify the corresponding UI forms in the
    \uicontrol Design mode.

    \section2 Creating UI Forms for Tab Contents

    To create UI forms for the tab contents:

    \list 1

        \li Select \uicontrol File > \uicontrol {New File or Project} >
            \uicontrol Qt > \uicontrol {QtQuick UI File} > \uicontrol Choose.

        \li In the \uicontrol {Component name} field, enter \b Settings.

        \li Click \uicontrol Next.

        \li Click \uicontrol Finish to create the UI form,
            \e SettingsForm.ui.qml, and a corresponding QML file,
            \e Settings.qml.

    \endlist

    Create the UI form, \e NotesForm.ui.qml, and the corresponding QML file,
    \e Notes.qml, for the notes tab in the same way. You will not need an
    \e ui.qml file for the history tab, so you will create the QML file for it
    later.

    \section2 Creating the Settings Tab

    The \uicontrol {Customer Settings} tab contains information about the
    selected user.

    \image qmldesigner-uiforms-example-settings-tab.png

    To create the tab contents:

    \list 1

        \li Double-click \e SettingsForm.ui.qml in the \uicontrol Projects
            view to open it for editing in the \uicontrol Design mode.

        \li Select \uicontrol Item in the \uicontrol Navigator and enter
            \b content in the \uicontrol Id field in \uicontrol Properties.

        \li In \uicontrol Library, select \uicontrol Imports >
            \uicontrol {Add Import} and import Qt Quick Controls and Layouts to
            make the QML types in those modules visible in the library.

        \li Drag and drop a \uicontrol {Grid Layout} from the library to the
            \b content item in the navigator.

        \li Select \uicontrol Layout > \uicontrol Top, \uicontrol Left, and
            \uicontrol Right buttons to anchor the grid layout to the parent.

        \li In the \uicontrol Margin fields for the anchors, set the margins
            to \b 12.

        \li In \uicontrol Properties, set \uicontrol {Column spacing} and
            \uicontrol {Row spacing} to \b 8, \uicontrol Columns to \b 3, and
            \uicontrol Rows to \b 4. If you add more fields, add the necessary
            amount of rows.

        \li Drag and drop four \uicontrol Label controls, a
            \uicontrol {Combo Box}, and three \uicontrol {Text Field} controls
            from the library to the navigator.

        \li Use the down arrow in the navigator to move one label down to the
            position above the last text field.

        \li In the properties, change the label text for each field in the
            \uicontrol Text field. You need the following labels: \b Title,
            \b {First Name}, \b {Last Name}, and \b {Customer Id}.

        \li In the properties, change the placeholder text for each text field
            in the \uicontrol {Placeholder text} field to be the same as the
            field label.

        \li Select the customer ID text field, then select \uicontrol Layout
            in properties and set \uicontrol {Column span} to 3 and check
            \uicontrol {Fill width} to make the ID field span the length of
            the grid layout.

        \li Drag and drop a \uicontrol {Row Layout} from the library to the
            \b content item in the navigator and click on it.

        \li Reset the height of the row layout in properties.

            \image qmldesigner-uiforms-reset-height.png

        \li Select \uicontrol Layout > \uicontrol Bottom and \uicontrol Right
            buttons to anchor the row layout to the parent.

        \li In the \uicontrol Margin fields for the anchors, set the margins
            to \b 12.

        \li Drag and drop two \uicontrol Button controls from the library to the
            row layout in the navigator.

        \li In the properties, change the button labels in the \uicontrol Text
            field to \b Save and \b Cancel.

        \li Select \uicontrol Layout and > \uicontrol {Fill width} and
            \uicontrol {Fill height} in properties for each button to have the
            buttons fill the row layout.

        \li In the navigator, select \uicontrol Export for each field to export
            the field IDs as properties. The following items should be exported,
            so that they can be referred to in \e Settings.qml:

            \quotefromfile uiforms/SettingsForm.ui.qml
            \skipto property
            \printuntil title

    \endlist

    \section2 Creating the Notes Tab

    The \uicontrol {Customer Notes} tab contains a text area for entering notes
    about the selected customer and buttons for saving or canceling the changes.

    To create the tab contents:

    \list 1

        \li Double-click \e NotesForm.ui.qml in the \uicontrol Projects
            view to open it for editing in the \uicontrol Design mode.

        \li Select \uicontrol Item in the \uicontrol Navigator and enter
            \b content in the \uicontrol Id field in \uicontrol Properties.

        \li In \uicontrol Library, select \uicontrol Imports >
            \uicontrol {Add Import} and import Qt Quick Controls and Layouts.

        \li Drag and drop a \uicontrol {Column Layout} from the library to the
            \b content item in the navigator.

        \li Select \uicontrol Layout > \uicontrol Top, \uicontrol Left, and
            \uicontrol Right buttons to anchor the grid layout to the parent
            and set the margins to \b 12.

        \li Drag and drop a \uicontrol {Text Area} from the library to the
            column layout.

        \li Select \uicontrol Layout and > \uicontrol {Fill width} and
            \uicontrol {Fill height} in properties to have the text area fill
            the column layout.

        \li Create the \uicontrol Save and \uicontrol Cancel buttons as
            instructed in \l {Creating the Settings Tab}. You can also copy and
            paste the row layout from SettingsForm.ui.qml.

        \li In the navigator, select \uicontrol Export for each field to export
            the field IDs as properties. The following items should be exported,
            so that they can be referred to in \e Notes.qml:

            \quotefromfile uiforms/NotesForm.ui.qml
            \skipto property
            \printuntil save

    \endlist

    \section2 Creating the History Tab

    The \uicontrol {Customer History} tab contains a table view that displays
    the transactions performed by the customer. Create a custom HistoryTableView
    type that you can edit in the \uicontrol Edit mode. For the history tab, you
    do not need an \e ui.qml file at all.

    To create the history tab:

    \list 1

        \li In the \uicontrol Projects view, right-click \uicontrol qml.qrc and
            select \uicontrol {Add New} > \uicontrol Qt >
            \uicontrol {QML File (Qt Quick 2)} to create the
            \e HistoryTableView.qml file and to add it to the project.

        \li Copy the implementation from \l{uiforms/HistoryTableView.qml}
            {HistoryTableView.qml}.

        \li Add the code from the example \l{uiforms/History.qml}{History.qml}
            file to your \e History.qml file to access the code model.

    \endlist

    \section1 Adding Tab Functionality

    Add functions for displaying data from the customer model in the tabs. You
    have already created the files that you need. You now need to copy the
    implementation for the settings tab from the \l {uiforms/Settings.qml}
    {Settings.qml} file and for the notes tab from the \l {uiforms/Notes.qml}
    {Notes.qml} file.

    To display the tab contents in the main view, set the QML files as sources
    for the tabs in the \uicontrol Design mode. Select the settings tab in the
    \uicontrol Navigator and specify for example \e Settings.qml in the
    \uicontrol Source field in the \uicontrol Properties view. Similarly,
    specify the sources for the notes and history tabs.

    You can then remove the \e Tab.qml file that the wizard generated but that
    you no longer need by selecting \uicontrol {Remove File} in the context
    menu.

    \section1 Creating Menus

    The wizard adds a menu bar to the \e main.qml file that contains a
    \uicontrol File menu with the \uicontrol Open and \uicontrol Exit menu
    items. Keep the menu and the \uicontrol Exit menu item, and add the
    \uicontrol Edit and \uicontrol Help menus with standard menu items.

    The wizard creates the following code:

    \badcode
    menuBar: MenuBar {
        Menu {
            title: qsTr("&File")
            MenuItem {
                text: qsTr("&Open")
                onTriggered: messageDialog.show(qsTr("Open action triggered"));
            }
            MenuItem {
                text: qsTr("E&xit")
                onTriggered: Qt.quit();
            }
        }
    }
    \endcode

    Remove the \uicontrol Open menu item and add the following code to create
    the new menus:

    \quotefromfile uiforms/main.qml
    \skipto menuBar
    \printuntil activeFocusItem.paste
    \printuntil }

    \section1 Creating Dialogs

    \image qmldesigner-uiforms-example-about-dialog.png

    The wizard creates a message box in the \e main.qml file that you should
    turn into an \uicontrol About dialog for the purpose of this example:

    \badcode
    MessageDialog {
        id: messageDialog
        title: qsTr("May I have your attention, please?")

        function show(caption) {
            messageDialog.text = caption;
            messageDialog.open();
        }
    \endcode

    Modify the code created by the wizard to add an icon and some text:

    \quotefromfile uiforms/main.qml
    \skipto MessageDialog
    \printuntil }

    Enable access to the \uicontrol About dialog from the \uicontrol Help menu
    that you create next.

    \section1 Running the Application

    The application is complete and ready to be run on the desktop or deployed
    to a device. To run the application, press \key {Ctrl+R}.
*/