Create a QT project using Qtcreator

Source: Internet
Author: User

The previous article documented the compilation of Qt library files and development-related tools and now uses these to write a useful GUI.

First, create the project using qtcreator-v2.4.1 :

① Configuration Compilation tool : Tools-->options-->build & Run-->qtversion/tool Chains


############################ Add the generated qmake before compiling, this is used to generate makefile files ################################################### ###################


################################ #添加编译工具gcc的g ++################################################################# ####

Compiler version:

gcc-4.6.3 and Cross compiler gcc4.4.3


② Create a new project:


############################################################################################################### ####################


################################### next will come to the following dialog box ############################################################ ##################


After the operation in addition to their own circumstances to change the name of the next, until finish.

A QT project is built to complete! ^_^


second, the following record oneself write a serial port receives GUI project : serial port receives

Hardware platform: ARM9--TQ2440 Development Board

Linux kernel version: 2.6.30.4

Project Documents at a glance:


The specific code implementation

① window class file:

Mainwindow.h

#ifndef mainwindow_h#define mainwindow_h#include <QMainWindow> #include <QString> #include "thread.h" Namespace Ui {    class MainWindow;} Class Mainwindow:public qmainwindow{    q_objectpublic:    explicit MainWindow (Qwidget *parent = 0);    ~mainwindow ();    QString s_buf;private Slots:    void Show_rec ();//for displaying received data    void on_textbrowser_highlighted (const QString & ARG1);    void on_pushbutton_clicked ();    void on_pushbutton_2_clicked ();    void on_pushbutton_3_clicked ();p rivate:    ui::mainwindow *ui;    MyThread *threada;  Declares a thread    unsigned int count;}; #endif//Mainwindow_h
mainwindow.cpp: Is the implementation of the class members in the Mainwindow.h file

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> #include "mainwindow.h" #include "ui_mainwindow.h" #include <strings.h    >mainwindow::mainwindow (Qwidget *parent): Qmainwindow (parent), UI (new Ui::mainwindow) {ui->setupui (this);    This->setwindowtitle ("Debug Interface");    /* thread */Threada = new MyThread; Threada->flag_stop = 1;    Do not open UART count = 0;    Threada->start (); Connect (threada,signal (finished ()), This,slot (Show_rec ()));} Mainwindow::~mainwindow () {Delete UI;}    void mainwindow::on_textbrowser_highlighted (const QString &arg1) {count++;  Ui->textbrowser->settext (ARG1); Displays the received character Ui->textbrowser->movecursor (Qtextcursor::end) on the Textbrowser,//Displays the latest information in the window memset (threada->rec _buf, ' n ', sizeof (THREADA-&GT;REC_BUF));      Emptying the SRC_BUF array/* Below is a case where the textbrowser buffer is not enough to handle the condition */if (count = =) {count = 0;  S_buf = "";    Ui->textbrowser->setplaintext ("");   }}void Mainwindow::show_rec () {QString tmp=threada->rec_buf;              Convert the character array into qstring type S_BUF = s_buf+tmp; Accumulate all the received characters on_textbrowser_highlighted (S_BUF);} /***************************************** ***open key slot function: Change the status identifier to enable the thread to read the serial data ********************************** /void mainwindow::on_pushbutton_clicked () {threada->flag_stop = 0;} /***************************************** ***close key slot function: Changing the status identifier causes the thread to stop reading the serial data ********************************* /void mainwindow::on_pushbutton_3_clicked () {threada->flag_stop = 1;} /***************************************** ***clear Key Slot function * * * function: Change status identification clear display ***************************************    /void mainwindow::on_pushbutton_2_clicked () {s_buf = ""; Ui->textbrowser->setplaintext ("");}
There's a project in the back. The generated header file called ui_mainwindow.h in silence: He was generated by Qtcreator's own designer tools, just after we used some controls

/********************************************************************************** Form generated from reading UI File ' mainwindow.ui ' * * * Created:sun Apr 21:52:59 2015** by:qt User Interface Compiler version 4.5.3**** WARNING ! All changes made in this file would be lost when Recompiling UI file!****************************************************** /#ifndef ui_mainwindow_h#define ui_mainwindow_h#include <QtCore/QVariant> #include <QtGui/QAction> #include <QtGui/QApplication> #include <QtGui/QButtonGroup> #include <qtgui/ qheaderview> #include <QtGui/QMainWindow> #include <QtGui/QPushButton> #include <qtgui/qspinbox > #include <QtGui/QTabWidget> #include <QtGui/QTextBrowser> #include <QtGui/QToolBar> #include    <qtgui/qwidget>qt_begin_namespaceclass Ui_mainwindow{public:qwidget *centralwidget;    Qtabwidget *tabwidget;    Qwidget *tab;    Qtextbrowser *textbrowser; Qpushbutton *pushbutton;    Qpushbutton *pushbutton_2;    Qspinbox *serl;    Qpushbutton *pushbutton_3;    Qwidget *tab_2;    Qtoolbar *maintoolbar; void Setupui (Qmainwindow *mainwindow) {if (Mainwindow->objectname (). IsEmpty ()) Mainwindow->set        ObjectName (Qstring::fromutf8 ("MainWindow"));        Mainwindow->resize (304, 212);        Centralwidget = new Qwidget (MainWindow);        Centralwidget->setobjectname (Qstring::fromutf8 ("Centralwidget"));        Tabwidget = new Qtabwidget (centralwidget);        Tabwidget->setobjectname (Qstring::fromutf8 ("Tabwidget"));        Tabwidget->setgeometry (qrect (0, 0, 291, 191));        tab = new Qwidget ();        Tab->setobjectname (Qstring::fromutf8 ("tab"));        Textbrowser = new qtextbrowser (tab);        Textbrowser->setobjectname (Qstring::fromutf8 ("Textbrowser"));        Textbrowser->setgeometry (qrect (0, 0, 221, 161));        pushbutton = new Qpushbutton (tab); Pushbutton->setobjectname (Qstring::fromutf8 ("Pushbutton "));        Pushbutton->setgeometry (Qrect (230, 50, 51, 27));        pushbutton_2 = new Qpushbutton (tab);        Pushbutton_2->setobjectname (Qstring::fromutf8 ("pushbutton_2"));        Pushbutton_2->setgeometry (Qrect (230, 90, 51, 31));        Serl = new Qspinbox (tab);        Serl->setobjectname (Qstring::fromutf8 ("Serl"));        Serl->setgeometry (Qrect (230, 10, 51, 27));        Pushbutton_3 = new Qpushbutton (tab);        Pushbutton_3->setobjectname (Qstring::fromutf8 ("Pushbutton_3"));        Pushbutton_3->setgeometry (Qrect (230, 130, 51, 27));        Tabwidget->addtab (tab, QString ());        tab_2 = new Qwidget ();        Tab_2->setobjectname (Qstring::fromutf8 ("tab_2"));        Tabwidget->addtab (tab_2, QString ());        Mainwindow->setcentralwidget (Centralwidget);        Maintoolbar = new Qtoolbar (MainWindow);        Maintoolbar->setobjectname (Qstring::fromutf8 ("Maintoolbar"));        Mainwindow->addtoolbar (Qt::toptoolbararea, Maintoolbar); ReTranslateui (MainWindow);        Tabwidget->setcurrentindex (0);    Qmetaobject::connectslotsbyname (MainWindow); }//Setupui void Retranslateui (Qmainwindow *mainwindow) {mainwindow->setwindowtitle (qapplication::transl Ate ("MainWindow", "MainWindow", 0, Qapplication::unicodeutf8)); #ifndef Qt_no_statustip Mainwindow->setstatustip ( QString ()); #endif//Qt_no_statustip Pushbutton->settext (qapplication::translate ("MainWindow", "open", 0, Qappli        Cation::unicodeutf8));        Pushbutton_2->settext (Qapplication::translate ("MainWindow", "clear", 0, Qapplication::unicodeutf8));        Pushbutton_3->settext (Qapplication::translate ("MainWindow", "Close", 0, Qapplication::unicodeutf8)); Tabwidget->settabtext (Tabwidget->indexof (tab), Qapplication::translate ("MainWindow", "UART", 0, Qapplication        :: UnicodeUTF8)); Tabwidget->settabtext (Tabwidget->indexof (tab_2), Qapplication::translate ("MainWindow", "sound", 0, Qapplication::unicODEUTF8));    Q_unused (MainWindow);  }//Retranslateui};namespace Ui {class mainwindow:public Ui_mainwindow {};}//Namespace UIQT_END_NAMESPACE#ENDIF// Ui_mainwindow_h
② thread class files

Thread.h

Thread.h#ifndef thread_h#define thread_h#include <qthread>class mythread:public QThread{Q_OBJECTpublic:    MyThread ();    Char rec_buf[30];    volatile bool flag_stop;protected:virtual void Run ();}; #endif
Thread.cpp

#include "thread.h" #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include < Termios.h>//Serial # include <stdio.h> #include <stdlib.h> #include <unistd.h> #include < strings.h> #include <memory.h>/* MyThread class constructor */mythread::mythread () {}/******************************* Function: Open serial device file, and serial port setting, be responsible for reading and signaling when working. Set properties: 115200,8,n,1*****************************/void Mythread::run ()//This is the specific work of the thread    made a {int fd; FD = open ("/dev/tq2440_serial1", o_rdwr|    O_noctty);    if (fd<0) {printf ("Open serial error!");    } struct Termios opt;    Tcgetattr (FD, &opt);    Opt.c_cc[vmin] = 20;//Set the minimum number of characters received Opt.c_iflag = 0;    Opt.c_oflag = 0;    Opt.c_lflag = 0; Opt.c_cflag = B115200 | CS8 | clocal | cread;//Basic Properties: 8N1 tcsetattr (FD, Tcsanow, &opt);    Tcsanow:the change occurs immediately, changes immediately occur tcflush (Fd,tciflush);//flush Receive buffer unsigned long i=0; while (1) {if (! Flag_stop) {//read reCeive data!!! Read (fd,rec_buf,sizeof (REC_BUF));  No resource readable will go to sleep emit finished (); Send a signal after reading. Who will receive it?            Don't worry about it here! Tcflush (Fd,tciflush);        Clear buffer printf ("%ld\n", i++);    } else Tcflush (Fd,tciflush);    } close (FD); Quit ();}

Linux under the compiler run interface, of course, this compilation tool is the choice of the X11, or Ubuntu certainly can not run: will also be replaced by the arm version of the QT Library and tools, this operation and the new project some of the operation is similar, here is no longer to be described.


Tq2440 the interface that runs on the Development Board: here does not upload, the cell phone took the picture not to pull to the computer ^_^

Get! Come on! Come on! One step, two steps, two steps!

Create a QT project using Qtcreator

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.