habe da ein kleines Problem.
Ich habe eine Klasse erstellt:
kommunikation.h
Code: Alles auswählen
#ifndef KOMMUNIKATION_H
#define KOMMUNIKATION_H
#include <QtCore/QObject>
class AbstractSerial;
class Kommunikation : public QObject
{
Q_OBJECT
public:
// Konstruktor und Destruktor anlegen
Kommunikation(QObject *parent = 0);
~Kommunikation();
// Methoden deklarieren
void Test ();
void SetBaudRate (long int baudrate);
private:
// verwendeter Zugriff
AbstractSerial *port;
};
#endif
Code: Alles auswählen
#include <QtCore/QTimer>
#include <QtCore/QDebug>
#include <QtCore/QTimer>
#include <abstractserial.h>
#include "kommunikation.h"
#include <iostream>
using namespace std;
Kommunikation::Kommunikation(QObject *parent) : QObject(parent)
{
port = new AbstractSerial(this);
port->setDeviceName("/dev/ttyUSB0");
port->open(AbstractSerial::WriteOnly | AbstractSerial::Unbuffered);
}
Kommunikation::~Kommunikation()
{
port->close();
}
void Kommunikation::SetBaudRate (long int baudrate)
{
switch (baudrate)
{
case 1200:
{
port->setBaudRate(AbstractSerial::BaudRate1200);
break;
}
case 2400:
{
port->setBaudRate(AbstractSerial::BaudRate2400);
break;
}
case 4800:
{
port->setBaudRate(AbstractSerial::BaudRate4800);
break;
}
case 9600:
{
port->setBaudRate(AbstractSerial::BaudRate9600);
break;
}
case 14400:
{
port->setBaudRate(AbstractSerial::BaudRate14400);
break;
}
case 19200:
{
port->setBaudRate(AbstractSerial::BaudRate19200);
break;
}
case 38400:
{
port->setBaudRate(AbstractSerial::BaudRate38400);
break;
}
case 56000:
{
port->setBaudRate(AbstractSerial::BaudRate56000);
break;
}
case 76800:
{
port->setBaudRate(AbstractSerial::BaudRate76800);
break;
}
case 115200:
{
port->setBaudRate(AbstractSerial::BaudRate115200);
break;
}
default:
{
port->setBaudRate(AbstractSerial::BaudRate9600);
qDebug () << "Falscher Wert für Baudrate -> schalte auf Standardwert 9600 um";
break;
}
}
}
void Kommunikation::Test ()
{
port->write("Hallo Steuerung\n");
}
Code: Alles auswählen
#include "test.h"
#include <QDesktopWidget>
#include <QApplication>
#include <QtCore/QCoreApplication>
#include <abstractserial.h>
#include "kommunikation.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Kommunikation Schnittstelle;
Schnittstelle.SetBaudRate (38400);
Schnittstelle.Test();
Test window;
window.setWindowTitle("Test");
window.setGeometry(10, 10, 1024, 768);
window.show();
return app.exec();
}
Code: Alles auswählen
#include "test.h"
#include <abstractserial.h>
#include <QMenu>
#include <QMenuBar>
#include <QApplication>
#include <QMessageBox>
#include <iostream>
#include <QInputDialog>
#include <QPalette>
#include <kommunikation.h>
Test::Test(QWidget *parent) : QMainWindow(parent)
{
// Schaltflächen erzeugen
//QPushButton *ende = new QPushButton("Ende", this);
//ende->setGeometry(50, 40, 90, 30);
start = new QPushButton("Start", this);
start->setGeometry(200, 40, 90, 30);
taste = new QPushButton("Stop", this);
taste->setGeometry(50, 40, 90, 30);
//connect(start, SIGNAL(pressed()), qApp, SLOT(quit()));
//connect(start, SIGNAL(pressed()), this, SLOT(senden()));
connect(start, SIGNAL(pressed()), this, SLOT(senden()));
connect(taste, SIGNAL(clicked()), this, SLOT(addItem()));
}
void Test::senden()
{
start->setStyleSheet( "background-color: green;" );
taste->setStyleSheet( "background-color: yellow;" );
Schnittstelle.Test(); -< funktioniert nicht
}
Code: Alles auswählen
#ifndef TEST_H
#define TEST_H
#include <QMainWindow>
#include <QPushButton>
#include <QRadioButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QListWidget>
#include <kommunikation.h>
class Kommunikation;
class Test : public QMainWindow
{
Q_OBJECT
public:
Test(QWidget *parent = 0);
//QPushButton *taste;
//QLabel* label;
//QVBoxLayout* layout;
QPalette* pal;
private:
QPushButton *taste;
QPushButton *start;
QListWidget *lw;
public slots:
void senden();
private slots:
void addItem();
};
#endif
Der Aufruf von Schnittstelle.Test(); in main funktioniert ohne Probleme.
Wenn ich jetzt aber die Methode in void Test::senden() aufrufen möchte,
bekomme ich folgende Fehlermeldung:
test.cpp: In member function 'void Test::senden()':
test.cpp:38: error: 'Schnittstelle' was not declared in this scope
Anscheinend findet er die Methode nicht.
Habe schon in Büchern und im Internet gewühlt, aber keine richtige Lösung gefunden.
Wie muß ich die Funktion richtig aufrufen ?
Danke für Eure Hilfe
Bang-Johansen