2015-11-05 49 views
0

我很感激它,當有人可以幫助我。我不習慣C++。我對它很陌生。C++訪問對象,QT

我的問題:我想使一個對象「玩家」可用於另一個類。我不知道如何歸檔。

的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(); 

我的主要問題是:我在做什麼錯,而引用和實例?那麼leapmotionlistener如何訪問同一個播放器對象(我想影響音頻)?

我得到的錯誤:

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

這個小項目是下載使用以下鏈接可快速查看: https://www.dropbox.com/s/igr7ywnvicdlxxd/MusicPlayer.zip?dl=0

提前感謝!

回答

0

以下已經摸索:

在MainWindow.h:

public: 
QMediaPlayer* player; 

在MainWindow.cpp。H:

private: 
QMediaPlayer *player; 

public: 
void setPlayer(QMediaPlayer *mplayer); 
在LeapMotionListener.cpp

LeapMotionListener::LeapMotionListener() 
: player(0) 
{} 


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

在其他問題中,您嘗試傳遞給setPlayer的QMediaPlayer是一個指針,而不是對象的引用。

您可以實例化您的播放器對象是這樣的:

QMediaPlayer player; 

也可以將您setPlayer函數簽名改成這樣:如果你更新你的函數簽名

void setPlayer(QMediaPlayer* mplayer); 

,您還可以需要修復其他函數簽名,並在引用播放器時將連接符(&)留在connect()語句之外。

+0

感謝。你的回答不夠詳細,因爲我對C++的理解很差。但現在它的工作。 – qecce

2

所有你需要做的是

leapMotionListener.setPlayer(*player); 

左值引用結合的對象,而不是指針的對象。爲了從指針中獲取對象,需要使用*對指針取消引用。在LeapMotionListener

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

+0

只是改變這沒有爲我工作。看到我的答案。但無論如何感謝。 – qecce