Search code examples
c++qtleap-motion

C++ accessing object, QT


I'd appreciate it, when someone could help me. I am not used to C++. I am new to it.

My problem: I want to make an object "player" usable for another class. I don't get how to archive this.

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

return a.exec();
}

mainwindow.h

#include <QMainWindow>
#include <QMediaPlayer>
#include <QDebug>
#include "Leap.h"
#include <leapmotionlistener.h>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QMediaPlayer* player;
private slots:
....
private:
    Ui::MainWindow *ui;
    void initialize();

    Controller controller;
    LeapMotionListener leapMotionListener;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSound>
#include <iostream>
#include <QfileDialog>
using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);

    player = new QMediaPlayer(this);

    connect(player, &QMediaPlayer::positionChanged, this,   &MainWindow::on_positionChanged);
    connect(player, &QMediaPlayer::durationChanged, this, &MainWindow::on_durationChanged);
    initialize();
}

QString path;

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

void MainWindow::initialize(){
    controller.addListener(leapMotionListener);
    leapMotionListener.setPlayer(player);
    controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);

}
...

leapmotionlistener.h

#ifndef LEAPMOTIONLISTENER
#define LEAPMOTIONLISTENER
#include <iostream>
#include <cstring>
#include "Leap.h"
#include <QFile>
#include <QAudioOutput>
#include <QMediaPlayer>

using namespace Leap;

class LeapMotionListener : public Listener {
public:
    LeapMotionListener();
    void setPlayer(QMediaPlayer&);
    //... some more methods

private:
QMediaPlayer player;

};
#endif // LEAPMOTIONLISTENER

leapmotionlistener.cpp

//calls the player object in one method like the following
player.stop();

My main questions are: What am I doing wrong while referencing and instantiating? And how can leapmotionlistener access the same player object (I want to influence the audio)?

I get the Error:

MusicPlayer\mainwindow.cpp:32: Error:    C2664: 'void  LeapMotionListener::setPlayer(QMediaPlayer &)' : converting from argument  1 'QMediaPlayer *' in 'QMediaPlayer &' not possible

This little project is downloadable for a quick view with the following link: https://www.dropbox.com/s/igr7ywnvicdlxxd/MusicPlayer.zip?dl=0

Thanks in advance!


Solution

  • the following worked now:

    in MainWindow.h:

    public: 
    QMediaPlayer* player;
    

    in MainWindow.cpp:

    player = new QMediaPlayer(this);
    leapMotionListener.setPlayer(player);
    controller.addListener(leapMotionListener);
    

    in LeapMotionListener.h:

    private:
    QMediaPlayer *player;
    
    public: 
    void setPlayer(QMediaPlayer *mplayer);
    

    in LeapMotionListener.cpp:

    LeapMotionListener::LeapMotionListener()
    : player(0)
    {}
    
    
    void LeapMotionListener::setPlayer(QMediaPlayer *mplayer){
         player = mplayer;
    }