[gelöst]Einfach nur zu blöd für C++ oder doch ein Qt problem
Verfasst: 28. Dezember 2008 22:57
Hi, bitte erschießt mich nicht, wenn es etwas total triviales ist, aber ich hab momentan total ein Brett vorm Kopf und hoffe ihr könnt es mir schnell wegnehmen.
Also ich habe keine Compilerfehler, sondern das Programm stürzt einfach ab, wenn es in die Methode wechselt.
Dabei läuft der Befehl doch auch im Konstruktor ohne Probleme durch.
Was ist an diesem tcpSocket anders?
Hier schnell der Code:
Und noch schnell die Headerdatei:
Ihr habt da bestimmt ein Auge für, oder?
Danke
Also ich habe keine Compilerfehler, sondern das Programm stürzt einfach ab, wenn es in die Methode
Code: Alles auswählen
void PowerControlQt::sendToSwitch(SwitchingState onoff, int value)Dabei läuft der Befehl doch auch im Konstruktor ohne Probleme durch.
Was ist an diesem tcpSocket anders?
Hier schnell der Code:
Code: Alles auswählen
#include <QtGui>
#include <QtNetwork>
#include "PowerControlQt.h"
#include "ControlWidget.h"
int read_ret = 0;
size_t read_syze = 0;
FILE * input = NULL;
size_t buf_in_syze = 512;
int flags = 0;
char buf[512];
char buf_in[512];
PowerControlQt::PowerControlQt(QWidget *parent) :
QDialog(parent)
{
ui.setupUi(this);
QHBoxLayout *mainLayout = new QHBoxLayout();
for (int i = 1; i <= 3; i++)
{
ControlWidget *poti = new ControlWidget(this, i);
QString portText = QString("Port");
portText.operator +=(i);
poti->ui.outletName->setText(QString("Port ") + QString::number(i));
poti->connect(poti->ui.checkBox, SIGNAL(stateChanged(int)), poti, SLOT(addressSwitch(int)));
mainLayout->addWidget(poti);
}
setLayout(mainLayout);
memset(buf, 0, 512);
memset(buf_in, 0, 512);
establishConnection();
}
void PowerControlQt::establishConnection()
{
tcpSocket = new QTcpSocket(this);
tcpSocket->connectToHost("192.168.0.90", 23);
if (tcpSocket->waitForConnected(5000) == false)
{
printf("Failed to connect\n");
}
tcpSocket->write(QByteArray("1 \r\n"));
tcpSocket->flush();
}
void PowerControlQt::destroyConnection()
{
tcpSocket->close();
}
void PowerControlQt::sendToSwitch(SwitchingState onoff, int value)
{
// if (tcpSocket->state() == QAbstractSocket::ConnectedState)
// establishConnection();
//// else
// printf("Verbindung ist noch da!");
// if (onoff == PowerControlQt::ON)
// ;
// tcpSocket->write(QByteArray("ON 5 \r\n"));
// else
tcpSocket->write(QByteArray("OFF 5 \r\n"));
tcpSocket->flush();
// tcpSocket->write(QByteArray("y \r\n"));
// tcpSocket->flush();
// }
}
PowerControlQt::~PowerControlQt()
{
destroyConnection();
}
Code: Alles auswählen
#ifndef POWERCONTROLQT_H
#define POWERCONTROLQT_H
#include <QtGui/QDialog>
#include <QTcpSocket>
#include "ui_powercontrolqt.h"
class PowerControlQt: public QDialog
{
Q_OBJECT
public:
PowerControlQt(QWidget *parent = 0);
~PowerControlQt();
enum SwitchingState
{
OFF,
ON
};
void establishConnection();
void destroyConnection();
void sendToSwitch(SwitchingState state, int value);
private:
Ui::PowerControlQtClass ui;
QTcpSocket *tcpSocket;
};
#endif // POWERCONTROLQT_HDanke