Qt 播放/暂停操作?

发布于 2024-08-02 16:13:46 字数 113 浏览 2 评论 0原文

在 Qt 中创建播放/暂停按钮的最佳方法是什么?我应该创建一个操作并在单击时更改其图标,还是应该创建两个操作然后以某种方式在单击时隐藏一个操作?如何使用一个快捷键来激活这两个操作? (播放时暂停,或暂停时播放)。

What's the best way to go about creating a play/pause button in Qt? Should I create one action and change it's icon when clicked, or should I create two actions and then somehow hide one when clicked? How do I use one shortcut key to activate both actions? (Pause when playing, or play when paused).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红焚 2024-08-09 16:13:46

保持简单。使用相同的按钮,但是在处理点击时,更改图标并根据当前状态(播放时暂停或暂停时播放)选择处理逻辑(播放或暂停)。

为了保持代码清晰,实现两个单独的方法,一种用于播放,一种用于暂停,并根据状态从按钮的插槽中调用它们。

Keep it simple. Use the same button, but when handling the clicking, change the icon and choose the handling logic (play or pause) based on current status (pause when playing or playing when paused).

In order to keep the code clear, implement two separate methods, one for play and one for pause and call them from the slot of the button, depeding on the status.

远山浅 2024-08-09 16:13:46

我认为这样的事情是最简单/最合适的:

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

我唯一不喜欢的是这些操作正在控制“其他”按钮的可见性状态。由于没有 setInvisible 函数,我无法将其连接起来,以便它们在单击时可以隐藏自己。

这似乎在隐藏按钮所在的位置产生了视觉间隙(至少在 Ubuntu 上)。

I think something like this is easiest/most appropriate:

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

The only thing I don't like is that the actions are controlling the OTHER button's visibility status. Since there is no setInvisible function I couldn't hook it up so that they could hide themselves when clicked.

That, and it seems to create a visual gap where the hidden button was (at least on Ubuntu).

可是我不能没有你 2024-08-09 16:13:46

您可以将播放和暂停操作添加到工具栏和主窗口的菜单中,并使暂停操作不可见。当您需要切换此操作时,您只需更改操作的可见性,它会同时影响菜单和工具栏。很方便,代码紧凑。

You can add both play and pause actions to the toolbar and to the menu of the main window and make the pause action invisible. When you need to switch this actions you will only have to change visibility of the actions and it takes affect on menu and on toolbar simultaneously. It's convenient, code is compact.

说谎友 2024-08-09 16:13:46

我不确定你的播放/暂停按钮在做什么,但我正在使用 Phonon 构建一个应用程序来传输音频,但我找不到获取媒体对象当前状态的好方法。

我能得到的最接近的是创建一个插槽并将其连接到 MediaObject 发出的 stateChanged() 信号。我最终这样做了:

    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's an error, do error handling.
        break;
    default:
        break;
    }
}

我不喜欢连接和重新连接,但我想这是 Qt 的方法。

I'm not sure what your play/pause buttons are doing, but I'm building an app with Phonon to stream audio and I couldn't find a good way to get the current state of the media object.

The closest I could get is to create a slot and connect it to the stateChanged() signal that MediaObject emits. I ended up doing 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's an error, do error handling.
        break;
    default:
        break;
    }
}

I'm not a fan of connecting and re-connecting, but I guess this is the Qt way to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文