2011-09-27 11:59:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (c) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-09-27 11:59:26 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator
|
2011-09-27 11:59:26 +02:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Free Documentation License
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Free
|
|
|
|
|
** Documentation License version 1.3 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file included in the packaging of this
|
|
|
|
|
** file.
|
|
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
// **********************************************************************
|
|
|
|
|
// NOTE: the sections are not ordered by their logical order to avoid
|
|
|
|
|
// reshuffling the file each time the index order changes (i.e., often).
|
|
|
|
|
// Run the fixnavi.pl script to adjust the links to the index order.
|
|
|
|
|
// **********************************************************************
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\contentspage index.html
|
2012-08-23 16:10:58 +02:00
|
|
|
\previouspage creator-qml-application.html
|
2011-09-27 11:59:26 +02:00
|
|
|
\page creator-writing-program.html
|
2011-11-14 15:43:03 +01:00
|
|
|
\nextpage creator-project-managing.html
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\title Creating a Qt Widget Based Application
|
|
|
|
|
|
|
|
|
|
This tutorial describes how to use \QC to create a small Qt application,
|
2013-03-13 10:29:46 +01:00
|
|
|
Text Finder. It is a simplified version of the Qt UI Tools
|
|
|
|
|
\l{http://qt-project.org/doc/qt-5.0/qtuitools/textfinder.html}{Text Finder
|
|
|
|
|
example}. The application user interface is constructed from Qt widgets by
|
2011-09-27 11:59:26 +02:00
|
|
|
using \QD. The application logic is written in C++ by using the code editor.
|
|
|
|
|
|
|
|
|
|
\image qtcreator-textfinder-screenshot.png
|
|
|
|
|
|
|
|
|
|
\section1 Creating the Text Finder Project
|
|
|
|
|
|
|
|
|
|
\list 1
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select \gui{File > New File or Project > Applications > Qt Gui
|
2011-09-27 11:59:26 +02:00
|
|
|
Application > Choose}.
|
|
|
|
|
|
|
|
|
|
\image qtcreator-new-qt-gui-application.png "New File or Project dialog"
|
|
|
|
|
|
|
|
|
|
The \gui{Introduction and Project Location} dialog opens.
|
|
|
|
|
|
|
|
|
|
\image qtcreator-intro-and-location-qt-gui.png "Introduction and Project Location dialog"
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui{Name} field, type \b {TextFinder}.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui {Create in} field, enter the path for the project files.
|
2012-10-05 10:46:51 +02:00
|
|
|
For example, \c {C:\Qt\examples}, and then click \gui{Next} (on
|
|
|
|
|
Windows and Linux) or \gui Continue (on Mac OS).
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2012-09-03 17:50:37 +02:00
|
|
|
The \gui {Kit Selection} dialog opens.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2012-09-03 17:50:37 +02:00
|
|
|
\image qtcreator-new-project-qt-versions-qt-gui.png "Kit Selection dialog"
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select build and run \l{glossary-buildandrun-kit}{kits} for your project,
|
2012-10-05 10:46:51 +02:00
|
|
|
and click \gui Next or \gui Continue.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2012-09-03 17:50:37 +02:00
|
|
|
\note If only one kit is specified in \gui Tools > \gui Options >
|
|
|
|
|
\gui {Build & Run} > \gui Kits, this dialog is
|
2011-09-27 11:59:26 +02:00
|
|
|
skipped.
|
|
|
|
|
|
|
|
|
|
The \gui{Class Information} dialog opens.
|
|
|
|
|
|
|
|
|
|
\image qtcreator-class-info-qt-gui.png "Class Information dialog"
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui{Class name} field, type \b {TextFinder} as the class
|
2011-09-27 11:59:26 +02:00
|
|
|
name.
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui{Base class} list, select \b {QWidget} as the base
|
2011-09-27 11:59:26 +02:00
|
|
|
class type.
|
|
|
|
|
|
|
|
|
|
\note The \gui{Header file}, \gui{Source file} and \gui{Form file}
|
|
|
|
|
fields are automatically updated to match the name of the class.
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Click \gui Next or \gui Continue.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
The \gui{Project Management} dialog opens.
|
|
|
|
|
|
|
|
|
|
\image qtcreator-new-project-summary-qt-gui.png "Project Management dialog"
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Review the project settings, and click \gui{Finish} (on Windows and
|
2012-10-05 10:46:51 +02:00
|
|
|
Linux) or \gui Done (on Mac OS) to create the project.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
|
2011-10-05 17:20:29 +02:00
|
|
|
\note The project opens in the \gui Edit mode, and these instructions are
|
|
|
|
|
hidden. To return to these instructions, open the \gui Help mode.
|
|
|
|
|
|
2011-09-27 11:59:26 +02:00
|
|
|
The TextFinder project now contains the following files:
|
|
|
|
|
|
|
|
|
|
\list
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li textfinder.h
|
|
|
|
|
\li textfinder.cpp
|
|
|
|
|
\li main.cpp
|
|
|
|
|
\li textfinder.ui
|
|
|
|
|
\li textfinder.pro
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
|
|
\image qtcreator-textfinder-contents.png "TextFinder project contents"
|
|
|
|
|
|
|
|
|
|
The .h and .cpp files come with the necessary boiler plate code.
|
|
|
|
|
The .pro file is complete.
|
|
|
|
|
|
|
|
|
|
\section1 Filling in the Missing Pieces
|
|
|
|
|
|
|
|
|
|
Begin by designing the user interface and then move on to filling
|
|
|
|
|
in the missing code. Finally, add the find functionality.
|
|
|
|
|
|
|
|
|
|
\section2 Designing the User Interface
|
|
|
|
|
|
|
|
|
|
\image qtcreator-textfinder-ui.png "Text Finder UI"
|
|
|
|
|
|
|
|
|
|
\list 1
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui{Editor} mode, double-click the textfinder.ui file in the
|
2011-09-27 11:59:26 +02:00
|
|
|
\gui{Projects} view to launch the integrated \QD.
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Drag and drop the following widgets to the form:
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\list
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li \gui{Label} (QLabel)
|
|
|
|
|
\li \gui{Line Edit} (QLineEdit)
|
|
|
|
|
\li \gui{Push Button} (QPushButton)
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
|
|
\image qtcreator-textfinder-ui-widgets.png "Adding widgets to Text Finder UI"
|
|
|
|
|
|
|
|
|
|
\note To easily locate the widgets, use the search box at the top of the
|
|
|
|
|
\gui Sidebar. For example, to find the \gui Label widget, start typing
|
2013-02-06 08:50:23 +01:00
|
|
|
the word \b label.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\image qtcreator-texfinder-filter.png "Filter field"
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Double-click the \gui{Label} widget and enter the text
|
|
|
|
|
\b{Keyword}.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Double-click the \gui{Push Button} widget and enter the text
|
|
|
|
|
\b{Find}.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui Properties pane, change the \gui objectName to
|
|
|
|
|
\b findButton.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\image qtcreator-textfinder-objectname.png "Changing object names"
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Press \key {Ctrl+A} (or \key {Cmd+A}) to select the widgets and
|
2012-10-05 10:46:51 +02:00
|
|
|
click \gui{Lay out Horizontally} (or press \gui{Ctrl+H} on Linux or
|
|
|
|
|
Windows or \key {Ctrl+Shift+H} on Mac OS) to apply a horizontal
|
|
|
|
|
layout (QHBoxLayout).
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\image qtcreator-texfinder-ui-horizontal-layout.png "Applying horizontal layout"
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Drag and drop a \gui{Text Edit} widget (QTextEdit) to the form.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select the screen area and click \gui{Lay out Vertically} (or press
|
2011-09-27 11:59:26 +02:00
|
|
|
\gui{Ctrl+L}) to apply a vertical layout (QVBoxLayout).
|
|
|
|
|
|
|
|
|
|
\image qtcreator-textfinder-ui.png "Text Finder UI"
|
|
|
|
|
|
|
|
|
|
Applying the horizontal and vertical layouts ensures that the
|
|
|
|
|
application UI scales to different screen sizes.
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li To call a find function when users press the \gui Find button, you
|
2011-09-27 11:59:26 +02:00
|
|
|
use the Qt signals and slots mechanism. A signal is emitted when a
|
|
|
|
|
particular event occurs and a slot is a function that is called in
|
|
|
|
|
response to a particular signal. Qt widgets have predefined signals
|
|
|
|
|
and slots that you can use directly from \QD. To add a slot for the
|
|
|
|
|
find function:
|
|
|
|
|
|
|
|
|
|
\list
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Right-click the \gui Find button to open a context-menu.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select \gui {Go to Slot > clicked()}, and then select
|
2011-09-27 11:59:26 +02:00
|
|
|
\gui OK.
|
|
|
|
|
|
|
|
|
|
A private slot, \c{on_findButton_clicked()}, is added to the
|
|
|
|
|
header file, textfinder.h and a private function,
|
|
|
|
|
\c{TextFinder::on_findButton_clicked()}, is added to the
|
|
|
|
|
source file, textfinder.cpp.
|
|
|
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Press \gui{Ctrl+S} (or \key {Cmd+S}) to save your changes.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
|
|
For more information about designing forms with \QD, see the
|
2013-03-13 10:29:46 +01:00
|
|
|
\l{http://qt-project.org/doc/qt-5.0/qtdesigner/qtdesigner-manual.html}{Qt Designer Manual}.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\section2 Completing the Header File
|
|
|
|
|
|
|
|
|
|
The textfinder.h file already has the necessary #includes, a constructor,
|
|
|
|
|
a destructor, and the \c{Ui} object. You need to add a private function,
|
|
|
|
|
\c{loadTextFile()}, to read and display the contents of the input text file
|
|
|
|
|
in the QTextEdit.
|
|
|
|
|
|
|
|
|
|
\list 1
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui{Projects} pane in the \gui {Edit view}, double-click the
|
2011-09-27 11:59:26 +02:00
|
|
|
\c{textfinder.h} file to open it for editing.
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Add a private function to the \c{private} section, after the
|
2011-09-27 11:59:26 +02:00
|
|
|
\c{Ui::TextFinder} pointer, as illustrated by the following code
|
|
|
|
|
snippet:
|
|
|
|
|
|
2012-12-17 11:14:21 +01:00
|
|
|
\snippet textfinder/textfinder.h 0
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
|
|
\section2 Completing the Source File
|
|
|
|
|
|
|
|
|
|
Now that the header file is complete, move on to the source file,
|
|
|
|
|
textfinder.cpp.
|
|
|
|
|
|
|
|
|
|
\list 1
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui{Projects} pane in the \gui Edit view, double-click the
|
2011-09-27 11:59:26 +02:00
|
|
|
textfinder.cpp file to open it for editing.
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Add code to load a text file using QFile, read it with QTextStream,
|
2011-09-27 11:59:26 +02:00
|
|
|
and then display it on \c{textEdit} with
|
2013-03-13 10:29:46 +01:00
|
|
|
\l{http://qt-project.org/doc/qt-5.0/qtwidgets/qtextedit.html#plainText-prop}
|
2011-09-27 11:59:26 +02:00
|
|
|
{setPlainText()}.
|
|
|
|
|
This is illustrated by the following code snippet:
|
|
|
|
|
|
2012-12-17 11:14:21 +01:00
|
|
|
\snippet textfinder/textfinder.cpp 0
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li To use QFile and QTextStream, add the following #includes to
|
2011-09-27 11:59:26 +02:00
|
|
|
textfinder.cpp:
|
|
|
|
|
|
2012-12-17 11:14:21 +01:00
|
|
|
\snippet textfinder/textfinder.cpp 1
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li For the \c{on_findButton_clicked()} slot, add code to extract the
|
2011-09-27 11:59:26 +02:00
|
|
|
search string and use the
|
2013-03-13 10:29:46 +01:00
|
|
|
\l{http://qt-project.org/doc/qt-5.0/qtwidgets/qtextedit.html#find}{find()}
|
2012-08-20 12:40:26 +02:00
|
|
|
function
|
2011-09-27 11:59:26 +02:00
|
|
|
to look for the search string within the text file. This is
|
|
|
|
|
illustrated by the following code snippet:
|
|
|
|
|
|
2012-12-17 11:14:21 +01:00
|
|
|
\snippet textfinder/textfinder.cpp 2
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Once both of these functions are complete, add a line to call
|
2011-09-27 11:59:26 +02:00
|
|
|
\c{loadTextFile()} in the constructor, as illustrated by the
|
|
|
|
|
following code snippet:
|
|
|
|
|
|
2012-12-17 11:14:21 +01:00
|
|
|
\snippet textfinder/textfinder.cpp 3
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
|
|
The \c{on_findButton_clicked()} slot is called automatically in
|
|
|
|
|
the uic generated ui_textfinder.h file by this line of code:
|
|
|
|
|
|
|
|
|
|
\code
|
|
|
|
|
QMetaObject::connectSlotsByName(TextFinder);
|
|
|
|
|
\endcode
|
|
|
|
|
|
|
|
|
|
\section2 Creating a Resource File
|
|
|
|
|
|
|
|
|
|
You need a resource file (.qrc) within which you embed the input
|
|
|
|
|
text file. The input file can be any .txt file with a paragraph of text.
|
|
|
|
|
Create a text file called input.txt and store it in the textfinder
|
|
|
|
|
folder.
|
|
|
|
|
|
|
|
|
|
To add a resource file:
|
|
|
|
|
|
|
|
|
|
\list 1
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select \gui{File > New File or Project > Qt > Qt Resource File >
|
2011-09-27 11:59:26 +02:00
|
|
|
Choose}.
|
|
|
|
|
|
|
|
|
|
\image qtcreator-add-resource-wizard.png "New File or Project dialog"
|
|
|
|
|
|
|
|
|
|
The \gui {Choose the Location} dialog opens.
|
|
|
|
|
|
|
|
|
|
\image qtcreator-add-resource-wizard2.png "Choose the Location dialog"
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui{Name} field, enter \b{textfinder}.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui{Path} field, enter \c{C:\Qt\examples\TextFinder},
|
2012-10-05 10:46:51 +02:00
|
|
|
and click \gui Next or \gui Continue.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
The \gui{Project Management} dialog opens.
|
|
|
|
|
|
|
|
|
|
\image qtcreator-add-resource-wizard3.png "Project Management dialog"
|
|
|
|
|
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui{Add to project} field, select \b{TextFinder.pro}
|
2012-10-05 10:46:51 +02:00
|
|
|
and click \gui{Finish} or \gui Done to open the file in the code
|
|
|
|
|
editor.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select \gui{Add > Add Prefix}.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li In the \gui{Prefix} field, replace the default prefix with a slash
|
2011-09-27 11:59:26 +02:00
|
|
|
(/).
|
|
|
|
|
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Select \gui{Add > Add Files}, to locate and add input.txt.
|
2011-09-27 11:59:26 +02:00
|
|
|
|
|
|
|
|
\image qtcreator-add-resource.png "Editing resource files"
|
|
|
|
|
|
|
|
|
|
\endlist
|
|
|
|
|
|
|
|
|
|
\section1 Compiling and Running Your Program
|
|
|
|
|
|
|
|
|
|
Now that you have all the necessary files, click the
|
|
|
|
|
\inlineimage qtcreator-run.png
|
|
|
|
|
button to compile and run your program.
|
|
|
|
|
|
|
|
|
|
*/
|