My environment is Qt5 32bit Windows 7 64bit MSVC 2010 32bit
My code is a simple music player
this is main code
slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 100);
player->setMedia(QUrl::fromLocalFile("D://test.mp3"));
player->setVolume(50);
...
connect(player, SIGNAL(bufferStatusChanged(int)), slider, SLOT(setValue(int)));
When I run this player, it could play music, but the value of the slider never changed.
then I add this:
connect(player, SIGNAL(bufferStatusChanged(int)), slider, SLOT(helloWorld(int)));
...
void player::helloWorld(int)
{
qDebug() << "hello, world";
}
I run it again, and found the string hello, world never be printed.
not printed anything.
Why the value of the slider not be changed?
————————-full code————————–
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
mainWidget = new QWidget();
HLayout = new QHBoxLayout();
VLayout = new QVBoxLayout();
playButton = new QPushButton("Okay");
exitButton = new QPushButton("Exit");
slider = new QSlider(Qt::Horizontal);
player = new QMediaPlayer();
connect(playButton, SIGNAL(toggled(bool)), this, SLOT(playOrPause(bool)));
connect(player, SIGNAL(bufferStatusChanged(int)), slider, SLOT(setValue(int)));
connect(player, SIGNAL(bufferStatusChanged(int)), this, SLOT(test(int)));
player->setMedia(QUrl::fromLocalFile("D://test.mp3"));
player->play();
player->setVolume(50);
slider->setRange(0, 100);
//UI
exitButton->setAutoDefault(true);
playButton->setAutoDefault(true);
playButton->setCheckable(true);
HLayout->addStretch();
HLayout->addWidget(playButton);
HLayout->addWidget(exitButton);
VLayout->addWidget(slider);
VLayout->addStretch();
VLayout->addLayout(HLayout);
mainWidget->setLayout(VLayout);
setCentralWidget(mainWidget);
setWindowIcon(QIcon(":/icons/icon.png"));
setWindowTitle("YUCOAT");
//connect(playButton, SIGNAL(toggled(bool)), this, SLOT(playOrPause(bool)));
//connect(player, SIGNAL(bufferStatusChanged(int)), slider, SLOT(setValue(int)));
}
MainWindow::~MainWindow()
{
}
void MainWindow::playOrPause(bool s)
{
if (s)
player->play();
else
player->pause();
}
void MainWindow::test(int s)
{
qDebug() << "hello, world!";
qDebug() << s;
qDebug() << player->mediaStatus();
}
According to the documentation :
You are making the connection after loading the media so probably the buffer is already in a
QMediaPlayer::BufferedMediastate so the signal will not get emited. You could check it by printing the media status before the connection: