使用QtCreator创建Qt工程

来源:互联网 发布:egd网络黄金裴蕾被通缉 编辑:程序博客网 时间:2024/05/05 18:06

前面一篇文章记录了Qt库文件和开发相关工具的编译,现在就来使用这些东东来写一实用的GUI。

一、使用QtCreator—V2.4.1创建工程

①配置编译工具:Tools-->Options-->Build & Run -->QtVersion/Tool Chains


############################   添加之前编译生成的qmake,这个用来生成Makefile文件   ######################################################################


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

编译器版本:

gcc-4.6.3和交叉编译器gcc4.4.3


②新建一个工程:


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


################################### Next之后就来到下面这个对话框 ##############################################################################


之后的操作除了根据自己的情况改改名字之外一路Next,直到finish。

一个Qt工程就新建完成! ^_^


二、下面记录自己写的一个串口接收GUI工程串口接收

硬件平台:arm9--tq2440开发板

Linux内核版本:2.6.30.4

工程文件一览:


具体的代码实现

①窗口类文件:

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(); //用于显示接收的数据    void on_textBrowser_highlighted(const QString &arg1);    void on_pushButton_clicked();    void on_pushButton_2_clicked();    void on_pushButton_3_clicked();private:    Ui::MainWindow *ui;    MyThread *threadA;  //声明一个线程    unsigned int count;};#endif // MAINWINDOW_H
mainwindow.cpp:是对mainwindow.h文件里边类成员的实现

#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("调试界面");    /* 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);  //在textBrowser上显示接收到的字符    ui->textBrowser->moveCursor(QTextCursor::End);//将最新的信息显示在窗口    memset(threadA->rec_buf,'\0',sizeof(threadA->rec_buf)); //将src_buf数组清空    /* 下面是为了处理可能是textBrowser缓冲区不够卡死的情况 */    if(count == 40)    {        count =0;        s_buf ="";        ui->textBrowser->setPlainText("");    }}void MainWindow::show_rec(){    QString tmp=threadA->rec_buf;   //将字符型数组转化成Qstring型    s_buf = s_buf+tmp;              //累积所有收到的字符    on_textBrowser_highlighted(s_buf);}/***************************************** ***open 按键槽函数 ***功能:改变状态标识使线程进行读串口数据 ******************************************/void MainWindow::on_pushButton_clicked(){    threadA->Flag_stop = 0;}/***************************************** ***close 按键槽函数 ***功能:改变状态标识使线程停止读串口数据 ******************************************/void MainWindow::on_pushButton_3_clicked(){    threadA->Flag_stop = 1;}/***************************************** ***clear 按键槽函数 ***功能:改变状态标识清除显示 ******************************************/void MainWindow::on_pushButton_2_clicked(){    s_buf ="";    ui->textBrowser->setPlainText("");}
在这背后还有一个工程自动帮忙生成的头文件叫ui_mainwindow.h在默默工作:他是由QtCreator自带的designer工具生成的,就在我们使用了一些控件之后

/********************************************************************************** Form generated from reading ui file 'mainwindow.ui'**** Created: Sun Apr 12 21:52:59 2015**      by: Qt User Interface Compiler version 4.5.3**** WARNING! All changes made in this file will 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->setObjectName(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::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));#ifndef QT_NO_STATUSTIP        MainWindow->setStatusTip(QString());#endif // QT_NO_STATUSTIP        pushButton->setText(QApplication::translate("MainWindow", "open", 0, QApplication::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.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>    //串口用到的#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <strings.h>#include <memory.h>/* MyThread类的构造函数 */MyThread::MyThread(){}/*******************************功能:打开串口设备文件,并进行串口设置,工作时负责读和发信号***设置属性:115200,8,n,1*****************************/void MyThread::run()          //这就是线程的具体工作了{    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;//设置最少接收字符数    opt.c_iflag = 0;    opt.c_oflag = 0;    opt.c_lflag = 0;    opt.c_cflag = B115200 | CS8 | CLOCAL | CREAD;//基本属性:8N1    tcsetattr(fd, TCSANOW, &opt); //TCSANOW:the change occurs immediately,改变立即发生    tcflush(fd,TCIFLUSH);//刷新接收缓冲区    unsigned long i=0;    while(1)    {        if(!Flag_stop)        {            //read receive data!!!            read(fd,rec_buf,sizeof(rec_buf)); //无资源可读会进入睡眠            emit finished();  //读完发一个信号。谁会接收呢?不用这里关心!            tcflush(fd,TCIFLUSH); //清除缓冲            printf("%ld\n",i++);        }        else            tcflush(fd,TCIFLUSH);    }    close(fd);    quit();}

Linux下编译运行界面,当然这个编译工具是选择x11的那一个,不然ubuntu肯定运行不了:待会还要换回arm版本的Qt库和工具,这个操作和新建工程的一些操作大同小异,这里就不再累述。


tq2440开发板上运行的界面:这里就不上传了,手机拍了照还没拉到电脑^_^

搞定!加油!加油!一步两步一步两步!生气

0 0
原创粉丝点击