Qt Play / Pause action?

What is the best way to create a play / pause button in Qt? Do I have to create one action and change its icon when I click, or do I need to create two actions and then somehow hide it when I click? How to use one key combination to activate both actions? (Pause playback or pause playback).

+5
source share
4 answers

Keep it simple . Use the same button, but when processing a click, change the icon and select the processing logic (play or pause) depending on the current state (pause during playback or playback during pause).

, : , .

+6

, - / :

playAct = new QAction(QIcon(":/icons/elementary/media-playback-start.png"), tr("&Run"), controlActGroup);
playAct->setShortcut(Qt::Key_Space);
playAct->setCheckable(true);
playAct->setStatusTip(tr("Run physics"));
connect(playAct, SIGNAL(triggered()), editorView, SLOT(runPhysics()));

pauseAct = new QAction(QIcon(":/icons/elementary/media-playback-pause.png"), tr("&Pause"), controlActGroup);
pauseAct->setShortcut(Qt::Key_Space);
pauseAct->setCheckable(true);
pauseAct->setStatusTip(tr("Pause physics"));
connect(pauseAct, SIGNAL(triggered()), editorView, SLOT(pausePhysics()));

connect(playAct, SIGNAL(toggled(bool)), pauseAct, SLOT(setVisible(bool)));
connect(pauseAct, SIGNAL(toggled(bool)), playAct, SLOT(setVisible(bool)));
pauseAct->setChecked(true);
pauseAct->setVisible(false);

, , , OTHER. setInvisible, , .

, , , , ( , Ubuntu).

+2

, . , , . , .

+1

I'm not sure what your play / pause buttons do, but I am building a Phonon application for streaming audio, and I could not find a good way to get the current state of the media object.

The closest I can get is to create a slot and connect it to the signal stateChanged()that it emits MediaObject. I ended up with this:

    MyMediaPlayer::MyMediaPlayer(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MyMediaPlayer)
    {
        ...

        connect(mediaObj, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
                this, SLOT(handleMediaState(Phonon::State,Phonon::State)));
    }

...

void MyMediaPlayer::handleMediaState(Phonon::State state, Phonon::State)
{
    switch (state)
    {
    case Phonon::PlayingState:
    case Phonon::LoadingState:
    case Phonon::BufferingState:
        //If we're playing, the button should pause
        ui->playPauseButton->setIcon(QIcon(":/assets/stock_media-pause.svg"));
        connect(ui->playPauseButton, SIGNAL(clicked()),
                mediaObj, SLOT(pause()));
        break;
    case Phonon::PausedState:
    case Phonon::StoppedState:
        //if we're paused, the button should play
        ui->playPauseButton->setIcon(QIcon(":/assets/stock_media-play.svg"));
        connect(ui->playPauseButton, SIGNAL(clicked()),
                mediaObj, SLOT(play()));
    case Phonon::ErrorState:
        //additionally, if there an error, do error handling.
        break;
    default:
        break;
    }
}

I'm not a fan of connecting and reconnecting, but I think this is Qt's way of doing this.

+1
source

All Articles