Ich bin sagen wir mal qt Einsteiger möchte Daten über UDP von einem Rechner auf den anderen seden und dort eine geeignete Grafik zu zeichnen.
Als Vorlage habe ich aus dem Buch C++ Programmierung mit Qt 4 von Jasmin Blanchette die UDP Projekte Wetherballoon und Weathrstation übernommen.
Die Beispiele gibt es hier zum Download (auf Download,Quelltext,Kapitel 14) :
Download
Der Sender:
wetherballoon.cpp :
Code: Alles auswählen
#include <QtCore>
#include <QtNetwork>
#include <cstdlib>
#include "weatherballoon.h"
WeatherBalloon::WeatherBalloon(QWidget *parent)
: QPushButton(tr("Quit"), parent)
{
connect(this, SIGNAL(clicked()), this, SLOT(close()));
connect(&timer, SIGNAL(timeout()), this, SLOT(sendDatagram()));
timer.start(2 * 100);
setWindowTitle(tr("Weather Balloon"));
}
double WeatherBalloon::temperature() const
{
return -20.0 + (2.0 * std::rand() / (RAND_MAX + 1.0));
}
double WeatherBalloon::humidity() const
{
return 20.0 + (2.0 * std::rand() / (RAND_MAX + 1.0));
}
double WeatherBalloon::altitude() const
{
return 7000 + (100.0 * std::rand() / (RAND_MAX + 1.0));
}
void WeatherBalloon::sendDatagram()
{
QByteArray datagram;
QDataStream out(&datagram, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_3);
out << QDateTime::currentDateTime() << temperature() << humidity()
<< altitude();
udpSocket.writeDatagram(datagram, QHostAddress("128.7.74.226"), 80);
// udpSocket.writeDatagram(datagram, QHostAddress::LocalHost, 5824);
//QHostAddress("127.0.0.1").
}Zum Empfangen:
weatherstation.cpp
Code: Alles auswählen
#include <QtGui>
#include <QtNetwork>
#include "weatherstation.h"
WeatherStation::WeatherStation(QWidget *parent)
: QDialog(parent)
{
udpSocket.bind(80);
connect(&udpSocket, SIGNAL(readyRead()),
this, SLOT(processPendingDatagrams()));
dateLabel = new QLabel(tr("Date:"));
timeLabel = new QLabel(tr("Time:"));
temperatureLabel = new QLabel(tr("Temperature:"));
humidityLabel = new QLabel(tr("Humidity:"));
altitudeLabel = new QLabel(tr("Altitude:"));
dateLineEdit = new QLineEdit;
timeLineEdit = new QLineEdit;
temperatureLineEdit = new QLineEdit;
humidityLineEdit = new QLineEdit;
altitudeLineEdit = new QLineEdit;
dateLineEdit->setReadOnly(true);
timeLineEdit->setReadOnly(true);
temperatureLineEdit->setReadOnly(true);
humidityLineEdit->setReadOnly(true);
altitudeLineEdit->setReadOnly(true);
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(dateLabel, 0, 0);
mainLayout->addWidget(dateLineEdit, 0, 1);
mainLayout->addWidget(timeLabel, 1, 0);
mainLayout->addWidget(timeLineEdit, 1, 1);
mainLayout->addWidget(temperatureLabel, 2, 0);
mainLayout->addWidget(temperatureLineEdit, 2, 1);
mainLayout->addWidget(humidityLabel, 3, 0);
mainLayout->addWidget(humidityLineEdit, 3, 1);
mainLayout->addWidget(altitudeLabel, 4, 0);
mainLayout->addWidget(altitudeLineEdit, 4, 1);
setLayout(mainLayout);
setWindowTitle(tr("Weather Station"));
}
void WeatherStation::processPendingDatagrams()
{
QByteArray datagram;
do {
datagram.resize(udpSocket.pendingDatagramSize());
udpSocket.readDatagram(datagram.data(), datagram.size());
} while (udpSocket.hasPendingDatagrams());
QDateTime dateTime;
double temperature;
double humidity;
double altitude;
QDataStream in(&datagram, QIODevice::ReadOnly);
in.setVersion(QDataStream::Qt_4_3);
in >> dateTime >> temperature >> humidity >> altitude;
dateLineEdit->setText(dateTime.date().toString());
timeLineEdit->setText(dateTime.time().toString());
temperatureLineEdit->setText(tr("%1 C").arg(temperature));
humidityLineEdit->setText(tr("%1%").arg(humidity));
altitudeLineEdit->setText(tr("%1 m").arg(altitude));
}Jetzt ist meine Frage ob das so Sinn mach oder ob ich das am besten ganz anders aufbaue. Vielleicht hat ja auch jemand einen Tipp für mich wie ich das in ein MainWindow reinbastel. Ich tuhe mich noch ein bisschen schehr mit dem Objektorientierten Programmieren.
Schon einmal vielen Dank.
Fabian