jamiroquai93
jamiroquai93

Reputation: 27

Why is my slot not being found in qt c++?

I've been stuck for a while trying to connect this pushButton to a function that plays the media already coded in. It gives the same error. Yes I included Q_Object in the classes. What's going on

In my main 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;
}

I have q_object in my classes but I get an error saying

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

I don't understand why I get this I have a function called playFile in the Player namespace class

#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

Here is the implementation.

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

Here is the main window header file. Is there something I'm missing maybe even the basics about 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

Lastly guys if you have an easier way I want to hear it. But just tell me what I did wrong here so Ill know for later..

Upvotes: 0

Views: 158

Answers (1)

eyllanesc
eyllanesc

Reputation: 244162

The parameters that are used in the slots are to receive parameters from the signals, and in your case it is not necessary.

Also another problem is that in the class inheriting from QMediaPlayer you have not called the parent constructor, so it works you must modify your code to the following:

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

Upvotes: 1

Related Questions