Create a Qt Widget Based application-windows

Source: Internet
Author: User

This turtorial describes how to use QT Creator to create a small QT application, Text Finder. It is a simplified version of the Qt UI Tools Text Finder Example. The application user interface is constructed from QT widgets by using QT Designer. The application logic is a written in C + + by using the Code Editor.

Creating the Text Finder Project

1. Select File > New file or Project > Application > Qt Widgets Application > Choose.

The Introduction and Project Location dialog opens.

2. In the Name field, type Textfinder.

3. In the "Create" field, enter the path for the project files. For example, C:\Users\John\Documents, and then click Next.

The Kit Selection dialog opens.

4. Select Build and run kits for your project, and click Next.

Note:if only one kit are specified in Tools > Options > Build & Run > Kits.

The Class Information dialog opens.

5. In the Class Name field, type Textfinder as the class name.

6. In the base class list, select Qwidget as the base class type.

Note:the Header file, Source file and Form file fields is automatically updated to match the name of the class.

7. Click Next.

The Project Management dialog opens.

8. Review the project settings, and click Finish to create the project.

Note:the project opens in the Edit mode, and these instructions is hidden. To return to these instructions, open the Help mode.

The Textfinder project now contains the following files:

> Textfinder.h

> Textfinder.cpp

> main.cpp

> Textfinder.ui

> Textfinder.pro

The. h and. cpp files come with the necessary boilerplate code. The. Pro file is complete.

Filling in the Missing Pieces

Begin by designing the user interface and then move over to filling in the missing code. Finally, add the Finde functionality.

Designing the User Interface

1. In the Edit mode, double-click the Textfinder.ui file in the Projects view to launch the integrated Qt Designer.

2. Drag and drop the following widgets to the form.

> Label (Qlabel)

> Line Edit (qlineedit)

> Push Button (Qpushbutton)

Note:to easily Locate the widgets, use the search box at the top of the Sidebar. For example, to finde the label widget, start typing the word label.

3. Double-click the Label widget and enter the text Keyword.

4. Double-click the Push Button widget and enter the text Find.

5. In the Properties pane, change the ObjectName to Findbutton.

6. Press CTRL + A (or cmd+a) to select the widgets and click Lay out horizontally (or press ctrl+h on Windows) to apply A Ho Rizontal layout (qhboxlayout).

7. Drag and drop a Text Edit widget (qtextedit) to the form.

8. Select the screen area and click Lay out vertically (or press ctrl-l) to apply a vertical layout (qvboxlayout).

Applying the horizontal and vertical layouts ensures that the application UI scales to different screen sizes.

9. To call a Find function, if users press the Finde button, you use the QT signals and slots mechanism. A signal is emitted when a particular event occurs and a slot are a function that's called in response to a particular sig Nal. QT Widgets has predefined signals and slots that's can use directly from Qt Designer. To add a slots for the Find function:

> Right-click the Find button to open a context-menu.

> select Go to Slots > clicked (), and then select OK.

A private slot, on_findbutton_clicked (), is added to the header files, textfinder.h and a private function, Textfinder:: O N_finderbutton_clicked (), is added to the source file, Textfinder.cpp.

Press Ctrl+s to save your changes.

Completing the Header File

The Textfinder.h file already has the necessary #includes, a constructor, a destructor, and the Ui object. Need to add a private function, Loadtextfile (), to read and display the contents of the input text file in the qtexted It.

1. The Projects pane in the "Edit View", double-click the Textfinder.h file to open it for editing.

2. Add a private function to the private section, after the Ui::textfinder pointer, as illustrated by the following code s Nippet:

Private
Slots: void on_findbutton_clicked (); Private : Ui::textfinder * UI; void loadtextfile ();

Completing the Source File

Now, the header file was complete, and move on to the source file, Textfinder.cpp.

1. In the "Project pane in the" Edit view, double-click the Textfinder.cpp file to open it for editing.

2. ADD code to load a text file using QFile, read it with Qtextstream, and then display it in TextEdit with Qtextedit:: SE Tplaintext ().

This was illustrated by the following code snippet:

void Textfinder::loadtextfile () {    QFile inputfile (":/input.txt");    Inputfile.open (qiodevice::readonly);     inch (&Inputfile);     inch . ReadAll ();    Inputfile.close ();    UI->textedit->Setplaintext (line);     = ui->textedit->textCursor ();     1 );}

3. To use QFile and Qtextstream, add the following #includes to Textfinder.cpp:

#include <QFile><QTextStream>

4. For the on_findbutton_clicked () slot, add code to extract the searching string and use the Qtextedit::find () function t o Look for the search string within the text file. This was illustrated by the following code snippet:

void textfinder::on_findbutton_clicked () {    = ui->lineedit->text ();    UI->textedit->Find (SearchString, qtextdocument::findwholewords);}

5. Once both of these functions is complete, add a line to call Loadtextfile () in the constructor, as illustrated by the Following code snippet:

Textfinder::textfinder (Qwidget *parent)    : Qwidget (parent), UI (new  ui::textfinder) {    UI->setupui (this);    Loadtextfile ();}

The on_findbutton_clicked () slot is called automatically in the UIC generated ui_textfinder.h file by this line of code:

Qmetaobject::connectslotsbyname (Textfinder);

Creating a Resource File

You need a resource file (. qrc) Wihin which you embed the input text file. The input file can is any. txt file wih a paragraph of text. Create a text file called Input.txt and store it in the Textfinder folder.

To add a resource file:

1. Select File > New file or Project > qt > qt Resource file > Choose.

The Choose the location dialog opens.

2. In the Name field, enter Textfinder.

3. In the Path field, enter C:\User\John\Documents\TextFinder, and click Next.

The Project Management dialog opens.

4. In the ADD to Project field, select Textfinder.pro and click Finish or do to open the file in the Code Editor.

5. Select Add > Add Prefix.

6. In the Prefix field, replace the default Prefix with a slash (/).

7. Select Add > Add Files, to locate and add input.txt.

Compiling and Running Your program

Now the necessary files, click the button to compile and run your program.

Ref:http://doc.qt.io/qtcreator/creator-writing-program.html

Create a Qt Widget Based application-windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.