簡體   English   中英

C ++訪問對象,QT

[英]C++ accessing object, QT

如果有人可以幫助我,我將不勝感激。 我不習慣C ++。 我是新來的。

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

main.cpp

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

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

return a.exec();
}

主窗口

#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

主窗口

#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);

}
...

jumpmotionlistener.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

jumpmotionlistener.cpp

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

我的主要問題是:引用和實例化時我在做什么錯? 並且jumpmotionlistener如何訪問同一播放器對象(我想影響音頻)?

我得到錯誤:

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

提前致謝!

您需要做的就是

leapMotionListener.setPlayer(*player);

左值引用綁定到對象,而不綁定到該對象的指針。 為了從指向它的指針獲取對象,您需要使用*取消引用指針。

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

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

QMediaPlayer player;

或者,您可以將setPlayer函數簽名更改為此:

void setPlayer(QMediaPlayer* mplayer);

如果更新功能簽名,則還需要修復其他功能簽名,並在引用播放器時將&符號保留在connect()語句之外。

現在工作如下:

在MainWindow.h中:

public: 
QMediaPlayer* player;

在MainWindow.cpp中:

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

在LeapMotionListener.h中:

private:
QMediaPlayer *player;

public: 
void setPlayer(QMediaPlayer *mplayer);

在LeapMotionListener.cpp中:

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


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM