1.widget窗口类构造函数
Widget::Widget(QWidget*parent) : QWidget(parent) , ui(newUi::Widget) { ui->setupUi(this); //创建显示类mMyDisplay=newDisplay(); //winId() 是 QWidget 类的一个方法,用于返回窗口的唯一标识符。//这个标识符可以用来与其他应用程序进行交互或者在一些特定情况下进行操作。//你可以通过调用该方法来获取当前窗口的标识符。mMyPlayer=newFPlayer(mMyDisplay->getVideo()->winId()); mThread=newQThread(); mMyPlayer->moveToThread(mThread); mThread->start(); mMyProgress=newProgress(); //添加显示窗口ui->gridLayout->addWidget(mMyDisplay); //添加进度条ui->verticalLayout_2->addWidget(mMyProgress); ui->uPlay->setEnabled(false); ui->uStop->setEnabled(false); ui->uSpeed->setText(QString("倍速x%1").arg(mMyPlayer->getPlaySpeed())); ui->uBackward->setEnabled(false); ui->uForward->setEnabled(false); connect(mMyPlayer, &FPlayer::signalProgress, this, &Widget::slotProgress); connect(mMyPlayer, &FPlayer::signalPLayStop, this, &Widget::slotPlayStop); connect(ui->uOpen, &QPushButton::clicked, this, &Widget::openClicked); connect(ui->uPlay, &QPushButton::clicked, this, &Widget::playClicked); connect(ui->uStop, &QPushButton::clicked, this, &Widget::stopClicked); connect(ui->uVolume, &QSlider::valueChanged, this, [=](intvalue) { mMyPlayer->setPlayVolume(value); }); connect(ui->uBackward, &QPushButton::clicked, this, [=]() { mMyPlayer->fastBackward(); }); connect(ui->uForward, &QPushButton::clicked, this, [=]() { mMyPlayer->fastForward(); }); connect(ui->uSpeed, &QPushButton::clicked, this, &Widget::speedClicked); connect(mMyProgress, &Progress::signalValue, this, [=](intvalue) { int64_th=value/60/60; int64_tmin=value/60; int64_ts=value-min*60; ui->uPlayProgress->setText(QString("%1:%2:%3").arg(h, 2, 10, QChar('0')).arg(min, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0'))); }); connect(mMyProgress, &Progress::signalSeekValue, this, [=](intvalue) { mMyPlayer->seek((longlong)value*1000); }); }
目标:初始化页面ui,绑定ui和播放控制类的信号和槽
2.其他页面ui元素
//进度voidWidget::slotProgress(longlongtime) { //按下if (!mMyProgress->getPressFlag()) { int64_ttempTime=time/1000; //小时int64_th=tempTime/60/60; //分int64_tmin=tempTime/60; //秒int64_ts=tempTime-min*60; ui->uPlayProgress->setText(QString("%1:%2:%3").arg(h, 2, 10, QChar('0')) .arg(min, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0'))); mMyProgress->setSliderValue(tempTime); } } //暂停播放voidWidget::slotPlayStop() { ui->uPlay->setText("播放"); ui->uOpen->setEnabled(true); ui->uStop->setEnabled(false); } voidWidget::openClicked() { QStringpath=QFileDialog::getOpenFileName(this, "", "", "*.mp4 *.avi *.flv"); if (mMyPlayer->open(path)) { intwidth=0; intheight=0; mMyPlayer->getVideoSize(width, height); mMyDisplay->setNewSize(width, height); int64_ttempTime=mMyPlayer->getLength() /1000; int64_th=tempTime/60/60; int64_tmin=tempTime/60; int64_ts=tempTime-min*60; ui->uPlayLength->setText(QString("%1:%2:%3").arg(h, 2, 10, QChar('0')).arg(min, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0'))); mMyProgress->setSliderLength(tempTime); ui->uPlay->setEnabled(true); ui->uBackward->setEnabled(true); ui->uForward->setEnabled(true); } } voidWidget::playClicked() { if (ui->uPlay->text() =="播放") { if (!mMyPlayer->play()) { return; } ui->uOpen->setEnabled(false); ui->uPlay->setText("暂停"); } else { mMyPlayer->pause(); ui->uPlay->setText("播放"); } ui->uStop->setEnabled(true); } voidWidget::stopClicked() { mMyPlayer->stop(); ui->uPlay->setText("播放"); ui->uStop->setEnabled(false); } voidWidget::speedClicked() { if (mMyPlayer->getPlaySpeed() ==PLAY_SLOW) { mMyPlayer->setPlaySpeed(PLAY_NORMAL); } elseif (mMyPlayer->getPlaySpeed() ==PLAY_NORMAL) { mMyPlayer->setPlaySpeed(PLAY_FAST2); } elseif (mMyPlayer->getPlaySpeed() ==PLAY_FAST2) { mMyPlayer->setPlaySpeed(PLAY_FAST4); } elseif (mMyPlayer->getPlaySpeed() ==PLAY_FAST4) { mMyPlayer->setPlaySpeed(PLAY_SLOW); } floatspeed= (mMyPlayer->getPlaySpeed() ==PLAY_SLOW) ?0.5 : mMyPlayer->getPlaySpeed(); ui->uSpeed->setText(QString("倍速x%1").arg(speed)); }