How to Develop Qt Program Simple Examples (5)

97
How to Develop Qt Program Simple Examples (5) 2011. 2 . 2 Mr. Kwon

description

How to Develop Qt Program Simple Examples (5). 2011. 2 . 2 Mr. Kwon. Contents. I2C Control EEPROM Serial Communication Simple Serial Communication Program GPS Receiver UDP Socket Timer Server/Client Mp3 Player. Simple Qt Example for MINI6410 I2C -EEPROM. I2C-EEPROM. - PowerPoint PPT Presentation

Transcript of How to Develop Qt Program Simple Examples (5)

Page 1: How to Develop Qt Program Simple Examples (5)

How to Develop Qt ProgramSimple Examples (5)

2011. 2 . 2Mr. Kwon

Page 2: How to Develop Qt Program Simple Examples (5)

• I2C Control EEPROM

• Serial Communication Simple Serial Communication Program

GPS Receiver

• UDP Socket Timer Server/Client

• Mp3 Player

Contents

Page 3: How to Develop Qt Program Simple Examples (5)

Simple Qt Example for MINI6410

I2C -EEPROM

Page 4: How to Develop Qt Program Simple Examples (5)

I2C-EEPROM

Page 5: How to Develop Qt Program Simple Examples (5)

Create New Project (Abstract)

Qt4 Application Type Projects >> Qt4 Gui Application

Project Name I2C

Project Location /nfs

Class Name MyWidget

Base Class QWidget

Window Size (LCD) 420 X 240 (Width-Height)

•Create New Project

Page 6: How to Develop Qt Program Simple Examples (5)

Design UI

Window Size : 420 X 240 Design the form below with: - QLabel - QPushButton - QTextEdit

QLabel

PushButton

TextEdit

Page 7: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.h

Page 8: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.h

Page 9: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 10: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 11: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 12: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

#-------------------------------------------------## Project created by QtCreator 2011-02-08T10:49:57##-------------------------------------------------

TARGET = I2CTEMPLATE = app

SOURCES += main.cpp\ mywidget.cpp

HEADERS += mywidget.h

FORMS += mywidget.ui

I2C.pro

Page 13: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.h#ifndef MYWIDGET_H#define MYWIDGET_H

#include <QWidget>#include <QMessageBox>#include <QFileDialog>

#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <fcntl.h>#include <getopt.h>#include <errno.h>#include <string.h>

namespace Ui { class MyWidget;}

class MyWidget : public QWidget { Q_OBJECTpublic: MyWidget(QWidget *parent = 0); ~MyWidget();

protected: void changeEvent(QEvent *e);

private: Ui::MyWidget *ui; int m_fd; int fd; int value;

private slots: void on_close_clicked(); void on_read_clicked(); void on_write_clicked(); void update();};#endif // MYWIDGET_H

Page 14: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

main.cpp

#include <QtGui/QApplication>#include "mywidget.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MyWidget w; w.show(); return a.exec();}

Page 15: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (1/4)

#include "mywidget.h"#include "ui_mywidget.h"

MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget){ ui->setupUi(this); value=0;

setWindowTitle("I2C EEPROM on MINI6410");

m_fd = ::open("/dev/i2c/0",O_RDONLY); if(m_fd<0){ QMessageBox::information(this, "ERROR","Fail to open /dev/I2C"); return; }}

Page 16: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (2/4)

MyWidget::~MyWidget(){ delete ui; ::close(m_fd);}

void MyWidget::changeEvent(QEvent *e){ QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}

Page 17: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (3/4)

void MyWidget::on_write_clicked(){ ::system("i2c -w >> temp.txt");

ui->textEdit->clear(); QFile file("temp.txt"); if(file.open((QFile::ReadOnly | QFile::Text))) { ui->textEdit->setPlainText(file.readAll()); } file.remove("temp.txt");

}

Page 18: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (4/4)

void MyWidget::on_read_clicked(){ ::system("i2c -r >> temp.txt");

ui->textEdit->clear(); QFile file("temp.txt"); if(file.open((QFile::ReadOnly | QFile::Text))) { ui->textEdit->setPlainText(file.readAll()); } file.remove("temp.txt");}

void MyWidget::on_close_clicked(){ close();}

Page 19: How to Develop Qt Program Simple Examples (5)

Run on Host PC

Page 20: How to Develop Qt Program Simple Examples (5)

Run on MINI 6410

Page 21: How to Develop Qt Program Simple Examples (5)

Simple Qt Example for MINI6410

Serial Communication

Page 22: How to Develop Qt Program Simple Examples (5)

Serial Communication

Page 23: How to Develop Qt Program Simple Examples (5)

Create New Project (Abstract)

Qt4 Application Type Projects >> Qt4 Gui Application

Project Name SerialCom

Project Location /nfs

Class Name MyWidget

Base Class QWidget

Window Size (LCD) 420 X 240 (Width-Height)

•Create New Project

Page 24: How to Develop Qt Program Simple Examples (5)

Design UI

Window Size : 420 X 240 Design the form below with: - QLabel - QPushButton - QTextEdit

QLabel

PushButton

TextEdit

Page 25: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.h

Page 26: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.h

Page 27: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 28: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 29: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 30: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 31: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

#-------------------------------------------------## Project created by QtCreator 2011-02-04T19:08:46##-------------------------------------------------

TARGET = SerialComTEMPLATE = app

SOURCES += main.cpp\ mywidget.cpp

HEADERS += mywidget.h

FORMS += mywidget.ui

SerialCom.pro

Page 32: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.h#ifndef MYWIDGET_H#define MYWIDGET_H

#include <QWidget>#include <QSocketNotifier>#include <QTimer>#include <QMessageBox>#include <QStringList>

#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <fcntl.h>#include <linux/fs.h>#include <errno.h>#include <string.h>#include <termio.h>

namespace Ui { class MyWidget;}

class MyWidget : public QWidget { Q_OBJECTpublic: MyWidget(QWidget *parent = 0); ~MyWidget();

protected: void changeEvent(QEvent *e);

private: Ui::MyWidget *ui;

int m_fd; QSocketNotifier *m_notifier; int openSerialPort();

Page 33: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.hprivate slots: void on_send_clicked(); void remoteDataIncoming();};

#endif // MYWIDGET_H

Page 34: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

main.cpp

#include <QtGui/QApplication>#include "mywidget.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MyWidget w; w.show(); return a.exec();}

Page 35: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (1/5)

#include "mywidget.h"#include "ui_mywidget.h"

MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget){ ui->setupUi(this); m_fd = openSerialPort(); if(m_fd<0){ QMessageBox::warning(this, tr("Error"), tr("Fail to Open Serial Port!")); return; } m_notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this); connect(m_notifier, SIGNAL(activated(int)), this, SLOT(remoteDataIncoming())); setWindowTitle("Serial Communication");}

Page 36: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (2/5)

MyWidget::~MyWidget(){ delete ui;

if(m_notifier){ delete m_notifier; m_notifier=0; }

if(m_fd>=0){ ::close(m_fd); m_fd=-1; }}

Page 37: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (3/5)

void MyWidget::changeEvent(QEvent *e){ QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}

Page 38: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (4/5)

void MyWidget::on_send_clicked(){ QString text(ui->send_text->toPlainText()); if(text.isEmpty()){ return; } ::write(m_fd, text.toLatin1(), text.length()); ui->send_text->clear();}void MyWidget::remoteDataIncoming(){ char c; if(read(m_fd, &c, sizeof c) != 1){ QMessageBox::warning(this, tr("Error"), tr("Receive Error")); return; } ui->recv_text->insertPlainText(QString(char(c)));}

Page 39: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (5/5)

int MyWidget::openSerialPort(){ int fd=-1; const char *devName="/dev/ttySAC0"; fd = ::open(devName, O_RDWR | O_NONBLOCK); if(fd<0){ return -1; } termios serialAttr; memset(&serialAttr, 0, sizeof serialAttr); serialAttr.c_iflag = IGNPAR; serialAttr.c_cflag = B9600 | HUPCL | CS8 | CREAD | CLOCAL; serialAttr.c_cc[VMIN] = 1; if(tcsetattr(fd, TCSANOW, &serialAttr) != 0){ return -1; } return fd;}

Page 40: How to Develop Qt Program Simple Examples (5)

Run on Host PC

Page 41: How to Develop Qt Program Simple Examples (5)

Run on MINI 6410

Page 42: How to Develop Qt Program Simple Examples (5)

Simple Qt Example for MINI6410

GPS Receiver

Page 43: How to Develop Qt Program Simple Examples (5)

GPS Receiver

Page 44: How to Develop Qt Program Simple Examples (5)

GPS

GPS Module (UIGGUB01-R003) -Integrated Antena-Serial Interface- High Speed Acquisition (10Hz)- Small Size, Efficient Power Man-agement - Max. update : 4Hz- Sensitivity : Acquisition : -160 dBm Tracking : -160 dBm Cold starts : -160 dBm- Operating Temp. -40°C to 85°C- Protocol : NMEA

Page 45: How to Develop Qt Program Simple Examples (5)

How to Connect GPS and Host PC

GPS Host PC

Page 46: How to Develop Qt Program Simple Examples (5)

GPS Data Format (NMEA)

(GPRMS : Recommended Minimum Specific GNSS Data)

Page 47: How to Develop Qt Program Simple Examples (5)

Create New Project (Abstract)

Qt4 Application Type Projects >> Qt4 Gui Application

Project Name MyGPS

Project Location /nfs

Class Name MyWidget

Base Class QWidget

Window Size (LCD) 420 X 240 (Width-Height)

•Create New Project

Page 48: How to Develop Qt Program Simple Examples (5)

Design UI

Window Size : 420 X 240 Design the form below with: - Qlabel - QTextEdit

QLabel

TextEdit

Page 49: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.h

Page 50: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.h

Page 51: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 52: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 53: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 54: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 55: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

#-------------------------------------------------## Project created by QtCreator 2011-02-08T15:16:01##-------------------------------------------------

TARGET = MyGPSTEMPLATE = app

SOURCES += main.cpp\ mywidget.cpp

HEADERS += mywidget.h

FORMS += mywidget.ui

MyGPS.pro

Page 56: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.h#ifndef MYWIDGET_H#define MYWIDGET_H

#include <QWidget>#include <QSocketNotifier>#include <QTimer>#include <QMessageBox>#include <QStringList>

#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <fcntl.h>#include <linux/fs.h>#include <errno.h>#include <string.h>#include <termio.h>

namespace Ui { class MyWidget;}

class MyWidget : public QWidget { Q_OBJECTpublic: MyWidget(QWidget *parent = 0); ~MyWidget();

protected: void changeEvent(QEvent *e);

private: Ui::MyWidget *ui;

int m_fd; QSocketNotifier *m_notifier; int fd; int openSerialPort();

Page 57: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.hprivate slots: void remoteDataIncoming();};

#endif // MYWIDGET_H

Page 58: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

main.cpp

#include <QtGui/QApplication>#include "mywidget.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MyWidget w; w.show(); return a.exec();}

Page 59: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (1/6)

#include "mywidget.h"#include "ui_mywidget.h"

MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget){ ui->setupUi(this); setWindowTitle("My GPS"); m_fd = openSerialPort(); if(m_fd<0){ QMessageBox::warning(this, tr("Error"), tr("Fail to Open Serial Port!")); return; } m_notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this); connect(m_notifier, SIGNAL(activated(int)), this, SLOT(remoteDataIncoming())); openSerialPort();}

Page 60: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (2/6)

MyWidget::~MyWidget(){ delete ui;

if(m_notifier){ delete m_notifier; m_notifier=0; }

if(m_fd>=0){ ::close(m_fd); m_fd=-1; }}

Page 61: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (3/6)

void MyWidget::changeEvent(QEvent *e){ QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}

Page 62: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (4/6)

int MyWidget::openSerialPort(){ int fd=-1; const char *devName="/dev/ttyS1"; fd = ::open(devName, O_RDWR | O_NONBLOCK); if(fd<0){ return -1; } termios serialAttr; memset(&serialAttr, 0, sizeof serialAttr); serialAttr.c_iflag = IGNPAR; serialAttr.c_cflag = B9600 | HUPCL | CS8 | CREAD | CLOCAL; serialAttr.c_cc[VMIN] = 1; if(tcsetattr(fd, TCSANOW, &serialAttr) != 0){ return -1; } return fd;}

Page 63: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (5/6)

void MyWidget::remoteDataIncoming(){ char c;

if(read(m_fd, &c, sizeof c) != 1){ QMessageBox::warning(this, tr("Error"), tr("Receive Error")); return; } else{ ui->status->setText("ttyS1 Connected"); }

QString tString = "";

tString = ui->textEdit_2->toPlainText();

Page 64: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (6/6)

if(char(c)==13){ ui->textEdit_2->clear(); tString.clear(); } else{ tString.append(char(c)); ui->textEdit_2->setText(tString); } }

Page 65: How to Develop Qt Program Simple Examples (5)

Run on Host PC

Page 66: How to Develop Qt Program Simple Examples (5)

Run on MINI 6410

Page 67: How to Develop Qt Program Simple Examples (5)

Simple Qt Example for MINI6410

UDP Socket Timer Server/Client

Page 68: How to Develop Qt Program Simple Examples (5)

UDP Socket Timer Server

Page 69: How to Develop Qt Program Simple Examples (5)

Create New Project (Abstract)

Qt4 Application Type Projects >> Qt4 Gui Application

Project Name UDPTimeServer

Project Location /nfs

Class Name MyWidget

Base Class QWidget

Window Size (LCD) 320 X 240 (Width-Height)

•Create New Project

Page 70: How to Develop Qt Program Simple Examples (5)

Design UI

Window Size : 320 X 240 Design the form below with: - QLabel - QLineEdit - QPushButton

QLabel

LineEdit

PushButton

Page 71: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.h

Page 72: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 73: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 74: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 75: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

#-------------------------------------------------## Project created by QtCreator 2011-02-08T19:46:56##-------------------------------------------------

QT += network

TARGET = UDPTimerServerTEMPLATE = app

SOURCES += main.cpp\ mywidget.cpp

HEADERS += mywidget.h

FORMS += mywidget.ui

UDPTimeServer.pro

Page 76: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.h#ifndef MYWIDGET_H#define MYWIDGET_H

#include <QWidget>#include <QTimer>#include <QUdpSocket>#include <QDateTime>

namespace Ui { class MyWidget;}

class MyWidget : public QWidget { Q_OBJECTpublic: MyWidget(QWidget *parent = 0); ~MyWidget();

protected: void changeEvent(QEvent *e);

private: Ui::MyWidget *ui;

QUdpSocket *udpSocket; QTimer *timer; int portNumber;

private slots: void start_clicked(); void brodcastingDiagram();};

#endif // MYWIDGET_H

Page 77: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

main.cpp

#include <QtGui/QApplication>#include "mywidget.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MyWidget w; w.show(); return a.exec();}

Page 78: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (1/6)

#include "mywidget.h"#include "ui_mywidget.h"

MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget){ ui->setupUi(this);

timer = new QTimer(this); udpSocket = new QUdpSocket();

connect(ui->start, SIGNAL(clicked()), this , SLOT(start_clicked())); connect(ui->stop, SIGNAL(clicked()), this, SLOT(close())); connect(timer, SIGNAL(timeout()), this, SLOT(brodcastingDiagram()));

setWindowTitle("Time UDP Server");}

Page 79: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (2/6)

MyWidget::~MyWidget(){ delete ui;}

void MyWidget::changeEvent(QEvent *e){ QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}

Page 80: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (3/6)

void MyWidget::start_clicked(){ portNumber = QString (ui->lineEdit->text()).toInt(); ui->start->setEnabled(false); timer->start(1000);}

Page 81: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (3/6)

void MyWidget::brodcastingDiagram(){ QDateTime currentTime = QDateTime::currentDateTime(); QString time = currentTime.toString();

QByteArray datagram(time.toAscii()); ui->time_show->setText(time);

udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, portNumber);}

Page 82: How to Develop Qt Program Simple Examples (5)

Run on Host PC

Page 83: How to Develop Qt Program Simple Examples (5)

Run on MINI 6410

Page 84: How to Develop Qt Program Simple Examples (5)

UDP Client

Page 85: How to Develop Qt Program Simple Examples (5)

Create New Project (Abstract)

Qt4 Application Type Projects >> Qt4 Gui Application

Project Name UDPClient

Project Location /nfs

Class Name MyWidget

Base Class QWidget

Window Size (LCD) 420 X 240 (Width-Height)

•Create New Project

Page 86: How to Develop Qt Program Simple Examples (5)

Design UI

Window Size : 420 X 240 Design the form below with: - QLabel - QLineEdit - QTextEdit - QPushButton

QLabel

TextEdit

PushButton

LineEdit

Page 87: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.h

Page 88: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 89: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class

mywidget.cpp

Page 90: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

#-------------------------------------------------## Project created by QtCreator 2011-02-08T20:13:19##-------------------------------------------------

QT += network

TARGET = UDPClientTEMPLATE = app

SOURCES += main.cpp\ mywidget.cpp

HEADERS += mywidget.h

FORMS += mywidget.ui

UDPClient.pro

Page 91: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.h#ifndef MYWIDGET_H#define MYWIDGET_H

#include <QWidget>#include <QTimer>#include <QUdpSocket>#include <QTextCodec>

namespace Ui { class MyWidget;}

class MyWidget : public QWidget { Q_OBJECTpublic: MyWidget(QWidget *parent = 0); ~MyWidget();

protected: void changeEvent(QEvent *e);

private: Ui::MyWidget *ui; QUdpSocket *udpSocket; int portNumber;

private slots: void processPendingdatagram(); void startClient();};

#endif // MYWIDGET_H

Page 92: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

main.cpp

#include <QtGui/QApplication>#include "mywidget.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MyWidget w; w.show(); return a.exec();}

Page 93: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (1/6)

#include "mywidget.h"#include "ui_mywidget.h"

MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget){ ui->setupUi(this);

//codec = QTextCodec::codecForName("eucKR");

udpSocket = new QUdpSocket(this); connect(ui->start, SIGNAL(clicked()), this, SLOT(startClient())); connect(ui->close, SIGNAL(clicked()), this, SLOT(close()));

setWindowTitle("Time UDP Client");}

Page 94: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (2/6)

MyWidget::~MyWidget(){ delete ui;}

void MyWidget::changeEvent(QEvent *e){ QWidget::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}

Page 95: How to Develop Qt Program Simple Examples (5)

Implementing the MyWidget Class (Details)

mywidget.cpp (3/6)

void MyWidget::processPendingdatagram(){ while(udpSocket->hasPendingDatagrams()){ QByteArray datagram; datagram.resize(udpSocket->pendingDatagramSize()); udpSocket->readDatagram(datagram.data(), datagram.size());

ui->textEdit->append(datagram.data()); ui->start->setEnabled(false); }}void MyWidget::startClient(){ portNumber = QString(ui->lineEdit->text()).toInt(); udpSocket->bind(portNumber); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingdatagram()));}

Page 96: How to Develop Qt Program Simple Examples (5)

Run on Host PC

Page 97: How to Develop Qt Program Simple Examples (5)

Run on MINI 6410