2017-08-04 43 views
0

我一直試圖將此pushButton連接到播放已編碼媒體的函數一段時間。它給出了相同的錯誤。是的,我在類中包含了Q_Object。這是怎麼回事爲什麼我的插槽沒有在qt C++中找到?

在我的主要window.cpp:

#include "mainwindow.h" 
#include "ui_mainwindow.h" 
MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    mediafile = new Player; 
    connect(ui->pushButton, SIGNAL(pressed()),mediafile,SLOT(playFile(mediafile))); 
} 
MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

我有Q_OBJECT在我的課,但我得到一個錯誤說

QObject::connect: No such slot Player::playFile(mediafile) in  ../musicplayer/mainwindow.cpp:12 
QObject::connect: (sender name: 'pushButton') 

我不明白爲什麼我得到這個我在Player命名空間類中有一個名爲playFile的函數

#ifndef PLAYER_H 
#define PLAYER_H 
#include <QMediaPlayer> 
#include <QDebug> 

class Player : public QMediaPlayer 
{ 
    Q_OBJECT 

public: 
    Player(); 
    ~Player(); 


public slots: 
    void playFile(Player *); 

private: 
    //Player file; 

}; 

#endif // PLAYER_H 

這裏是實現。

#include "player.h" 
#include "mainwindow.h" 
Player::Player() { 
} 
Player::~Player(){ 
    //delete file; 
} 
void Player::playFile(Player* file){ 
    file->setMedia(QUrl::fromLocalFile("Average White Band - Overture.mp3")); 
    file->setVolume(50); 
    file->play(); 
} 

這是主窗口的頭文件。有什麼我失蹤,甚至可能是關於qt的基礎知識

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 
#include <QMainWindow> 
#include <player.h> 
namespace Ui { 
    class MainWindow; 
} 

class MainWindow : public QMainWindow { 
    Q_OBJECT 
public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 
private: 

    Player *mediafile; 
    Ui::MainWindow *ui; 
}; 

#endif // MAINWINDOW_H 

最後,如果你有更簡單的方法,我想聽到它的傢伙。 但是,只是告訴我我在這裏做了什麼錯誤,所以我知道以後..

+0

是什麼'file'? – eyllanesc

+0

它是我用於該功能的參數的名稱 – jamiroquai93

+0

當您在插槽中使用參數是從信號接收數據時,如果您意識到該參數不合適。 – eyllanesc

回答

1

插槽中使用的參數是從信號接收參數,在你的情況下,它是沒有必要的。

而且另一個問題是,在從類繼承QMediaPlayer您尚未調用父構造,所以它的工作原理,您必須修改代碼以下列:

mainwindow.h

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include "player.h" 

#include <QMainWindow> 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 

private: 
    Ui::MainWindow *ui; 
    Player *mediafile; 
}; 

#endif // MAINWINDOW_H 

mainwindow.cpp

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

#include <QPushButton> 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    mediafile = new Player; 
    connect(ui->pushButton, SIGNAL(pressed()), mediafile, SLOT(playFile())); 
} 

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

player.h

#ifndef PLAYER_H 
#define PLAYER_H 

#include <QMediaPlayer> 

class Player : public QMediaPlayer 
{ 
    Q_OBJECT 
public: 
    Player(QObject *parent = Q_NULLPTR, Flags flags = Flags()); 
    ~Player(); 
public slots: 
    void playFile(); 
}; 

#endif // PLAYER_H 

player.cpp

#include "player.h" 

Player::Player(QObject *parent, Flags flags):QMediaPlayer(parent, flags) 
{ 

} 

Player::~Player() 
{ 

} 

void Player::playFile() 
{ 
    setMedia(QUrl::fromLocalFile("/home/eyllanesc/Music/Coldplay/A Rush of Blood to the Head/Track 8.mp3")); 
    setVolume(50); 
    play(); 
} 
+0

它的工作原理!誠實的人,我真的非常感謝你一大堆,真的很感激。被困在這個好2天大聲笑。我有一個問題,雖然是因爲我有不必要的指針playFile裏面的函數也 – jamiroquai93

+0

閱讀此:http://doc.qt.io/qt-4.8/signalsandslots.html – eyllanesc

+0

並閱讀有點OOP和C++ ,繼承很重要。 – eyllanesc