Writing a simple program with QT creator

Source: Internet
Author: User
Tags qt designer

Note:This tutorial assumes that the user has experience writing Basic QT applications, Designing user interfaces with QT designer and using the QT resource system.

In this example, we will describe the steps involve in using QT creator to create a small QT program, text finder. specified red by the qtuitools 'text finder example, we will write a similar but simplified version of it, as shown below.


Setting up your environment

Once you have installed QT creator, it will automatically detect if qt's location is in yourPathVariable. If QT's location is not in yourPath, You can set it in one of the following ways, depending on your platform:

  • On Windows and Linux: inToolsMenu, underOptions.
  • On Mac OS X: InPreferences, UnderQt4.

Note:If QT was compiled with Visual Studio, all environment variables set in Visual Studio will be added to QT Creator as well.


Setting up the project

We begin with a qt4 GUI application project generated by QT creator. the creating a project in QT creator document describes this process in detail. remember to select qwidget as the text finder's base class. if your project is not yet loaded, you can load it by selectingOpenFromFileMenu.

In your project you will have the following files:

  • Textfinder. h
  • Textfinder. cpp
  • Main. cpp
  • Textfinder. UI
  • Textfinder. Pro

The. HAnd. CppFiles come with the necessary boiler plate Code;. ProFile is also complete.


Filling in the missing pieces

We will begin by designing the user interface and then move on to filling in the missing code. Finally, we will add the find functionality.


Designing the user interface

To begin designing the user interface, double-click onTextfinder. UIFile in yourProject Explorer. This will launch the integrated QT designer.

Design the form above using a qlabel, qlineedit, qpushbutton and a qtextedit. we recommend that you use a qgridlayout to lay out the qlabel, qlineedit and qpushbutton. the qtextedit can then be added to a qvboxlayout, along with the qgridlayout. if you are new to designing formsQt designer, You can take a look at the designer manual.


The header file

TheTextfinder. hFile already has the necessary includes, a constructor, A destructor, andUIObject. We need to add a private slot,On_findbutton_clicked (), To carry out our find operation. We also need a private function,Loadtextfile (), To read and display the contents of our input text file in the qtextedit. This is done with the following code:

    private slots:        void on_findButton_clicked();    private:        Ui::Form ui;        void loadTextFile();

Note:TheUi: FormObject is already provided.


The source file

Now that our header file is complete we move on to our source file,Textfinder. cpp. We begin by filling in the functionality to load a text file. The code snippet below describes this:

    void TextFinder::loadTextFile()    {        QFile inputFile(":/input.txt");        inputFile.open(QIODevice::ReadOnly);        QTextStream in(&inputFile);        QString line = in.readAll();        inputFile.close();        ui.textEdit->setPlainText(line);        QTextCursor cursor = ui.textEdit->textCursor();    }

Basically, we load a text file using qfile, read it with qtextstream, and then display it onTexteditWith setplaintext ().

ForOn_findbutton_clicked ()Slot, we extract the search string and use the find () function to look for the search string within the text file. The code snippet below further describes it:

    void TextFinder::on_findButton_clicked()    {        QString searchString = ui.lineEdit->text();        ui.textEdit->find(searchString, QTextDocument::FindWholeWords);    }

Once we have both these functions complete, we callLoadtextfile ()In our constructor.

    TextFinder::TextFinder(QWidget *parent, Qt::WFlags flags)        : QWidget(parent, flags)    {        ui.setupUi(this);        loadTextFile();    }

TheOn_findbutton_clicked ()Slot will be called automatically due to this line of code:

    QMetaObject::connectSlotsByName(Form);

In the UIC generatedUi_textfinder.hFile.


The resource file

We require a resource file (. Qrc) Within which we will embed the input text file. This can be any. TxtFile with a paragraph of text. To add a resource file, right click onResource filesInProject ExplorerAnd selectAdd new file.... You will see the wizard dialog displayed below.

Enter "textfinder" inNameField and use the givenPath. Then, clickContinue.

On this page you can choose to which project you want to add the new file. Make sure thatAdd to projectIs checked and "textfinder" is selected asProject, And clickDone.

Your resource file will now be displayed with the resource editor. Click onAddDrop down box and selectAdd prefix. The prefix we require is just a slash (/). ClickAddAgain but this time, selectAdd File. Locate the text file you are going to use, we useInput.txt.

The screenshot above shows what you can expect CT to see once you have added the resource file successfully.


Compiling and running your program

Now that you have all the necessary files, you can compile your program by clicking on the button.

 

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.