ich bin neu in der C++ programmierung und mit dem Umgang von Qt und auch hier im Forum.
An der Uni habe ich ein Projekt bei dem wir mit einem Raspberry Pi einen mobilen Roboter programmieren.
Ich versuche gerade mittels einem GUI dem Roboter zu sagen, dass er vorwärts fahren soll.
Aber die Connect Funktion von Qt in meiner mobilePlatform.cpp Datei funktioniert nicht.
Zum Test habe ich ein cout erstellt welches mir den Text SLOT aktiv ausgeben soll. Aber das klappt nicht. Warum warum warum...
Ich verstehe nicht was ich da falsch gemacht habe. Ob ich da etwas mit den Klassen und Zeiger evtl. nicht richtig verstehe und deshalb das ganze nicht Funktioniert.
Vielleicht kann mir ja einer von euch helfen. Würde mich ewig freuen. Habe den ganzen Tag schon daran rum probiert.
Grüße Manu
Code: Alles auswählen
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QObject>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void sgn_btnBackwardClicked();
void sgn_btnForwardClicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Code: Alles auswählen
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QObject>
#include <iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui -> forwardButton,SIGNAL(clicked()),this, SIGNAL(sgn_btnForwardClicked()));
connect(ui -> backwardButton, SIGNAL(clicked()),this, SIGNAL(sgn_btnBackwardClicked()));
//Diese Verbindung funktioniert
}
MainWindow::~MainWindow()
{
delete ui;
}
Code: Alles auswählen
#ifndef CONTROLSYSTEM_H
#define CONTROLSYSTEM_H
#include <mobileplatform.h>
#include <mainwindow.h>
#include <QObject>
class ControlSystem : public QObject
{
Q_OBJECT
public:
explicit ControlSystem(QObject *parent = 0);
~ControlSystem();
void connectSignalSlots();
private:
MobilePlatform *m_pMobilePlatform;
MainWindow *m_pMainWindow;
public slots:
void slot_moveForward();
signals:
void sgn_btnForwardClicked();
void sgn_btnBackwardClicked();
};
#endif // CONTROLSYSTEM_H
Code: Alles auswählen
#include "controlsystem.h"
#include "mainwindow.h"
#include "mobileplatform.h"
#include <QObject>
#include <iostream>
using namespace std;
ControlSystem::ControlSystem(QObject *parent) :
QObject(parent)
{
this->m_pMainWindow = new MainWindow();
m_pMobilePlatform = new MobilePlatform();
connectSignalSlots();
}
void ControlSystem::connectSignalSlots()
{
cout<<"CONNECT SignalSlots aufgerufen"<< endl;
connect(this->m_pMainWindow, SIGNAL(sgn_btnForwardClicked()),this,SLOT(slot_moveForward()));
connect(m_pMainWindow, SIGNAL(sgn_btnBackwardClicked()), m_pMobilePlatform, SLOT(slot_moveBackward()));
//Diese Verbindung nach slot_moveForward funktioniert NICHT!!! Warum
}
void ControlSystem::slot_moveForward()
{
cout<<"SLOT aktiv"<<endl;
}
ControlSystem::~ControlSystem()
{
delete this -> m_pMainWindow;
delete this -> m_pMobilePlatform;
}