我的环境是Qt5 32bit Windows 7 64bit MSVC 2010 32bit
我的代码是一个简单的音乐播放器

这是主要代码

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

当我运行此播放器时,它可以播放音乐,但slider的值从未更改。

然后我添加:
connect(player, SIGNAL(bufferStatusChanged(int)), slider, SLOT(helloWorld(int)));
...
void player::helloWorld(int)
{
    qDebug() << "hello, world";
}

我再次运行它,发现字符串hello, world从未被打印过。

没有印任何东西。

为什么不更改slider的值?

-------------------------完整代码------------------------- ---
#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();
}

最佳答案

根据documentation:



在加载媒体后,您正在建立连接,因此缓冲区可能已经处于QMediaPlayer::BufferedMedia状态,因此不会发出信号。您可以通过在连接之前打印media status来检查它:

qDebug() << player->mediaStatus();

10-07 22:20