Unsolved Cannot setText() QLabel with QMediaPlayer on the Mainwindow
-
My project.pro
QT += core gui multimedia multimediawidgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = DisplayAntrianSQ TEMPLATE = app DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ mainwindow_1366x768.cpp HEADERS += \ mainwindow_1366x768.h FORMS += \ mainwindow_1366x768.ui RESOURCES += \ resources.qrc
my main.cpp
#include "mainwindow_1366x768.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow_1366x768 * mainwindow; mainwindow = new MainWindow_1366x768; mainwindow->showFullScreen(); return a.exec(); }
my MainWindow
#ifndef MAINWINDOW_1366x768_H #define MAINWINDOW_1366x768_H #include <QMainWindow> #include <QTimer> #include <QDateTime> #include <QMediaPlayer> #include <QVideoWidget> #include <QVBoxLayout> #include <QKeyEvent> namespace Ui { class MainWindow_1366x768; } class MainWindow_1366x768 : public QMainWindow { Q_OBJECT public: explicit MainWindow_1366x768(QWidget *parent = 0); ~MainWindow_1366x768(); protected: void keyPressEvent(QKeyEvent *event); private: Ui::MainWindow_1366x768 *ui; QTimer * timer; QDateTime * dt; QMediaPlayer * player; QVideoWidget * videoWidget; QVBoxLayout * ly; void buatVideoPlayer(); bool first; public slots: void updateJamHariTanggal(); }; #endif // MAINWINDOW_H
And
#include "mainwindow_1366x768.h" #include "ui_mainwindow_1366x768.h" #include <QFile> #include <QDebug> #include <QDir> MainWindow_1366x768::MainWindow_1366x768(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow_1366x768), first(true) { ui->setupUi(this); timer = new QTimer(this); dt = new QDateTime(); timer->start(1000); connect(timer, SIGNAL(timeout()), this, SLOT(updateJamHariTanggal())); buatVideoPlayer(); } MainWindow_1366x768::~MainWindow_1366x768() { delete ui; delete dt; delete ly; } void MainWindow_1366x768::updateJamHariTanggal() { ui->lblJam->setText(dt->currentDateTime().time().toString(" HH:mm:ss")); ui->lblHariTanggal->setText(dt->currentDateTime().date().toString("dddd, dd MMMM yyyy")); qDebug()<<"Time must update, but not. why?"; } void MainWindow_1366x768::buatVideoPlayer() { player = new QMediaPlayer(this); videoWidget = new QVideoWidget(this); // when i comment this line below, i can update mu QLable ui->lblJam player->setVideoOutput(videoWidget); ly = new QVBoxLayout; ly->addWidget(videoWidget); videoWidget->setAspectRatioMode(Qt::IgnoreAspectRatio); ly->setMargin(0); ui->widVideo->setLayout(ly); player->setMedia(QUrl::fromLocalFile(QDir::currentPath()+"/media/tt.mp4")); player->play(); } void MainWindow_1366x768::keyPressEvent(QKeyEvent *event) { if(event->key()==Qt::Key_Escape) close(); }
this code is working in linux(Ubuntu 16.04). but, when i use in windows 7 and 10, my QLabel cannot updated.
i can update when i comment this line:// when i comment this line below, i can update mu QLable ui->lblJam player->setVideoOutput(videoWidget);
how to solve this my problem?
-
@mosleim said in Cannot setText() QLabel with QMediaPlayer on the Mainwindow:
this code is working in linux(Ubuntu 6.04). but, when i use in windows 7 and 10, my QLabel cannot updated.
Most likely you have a typing error in Ubuntu version. I guess you mean 16.04.
More important might be the Qt versions you are using on different platforms.
Unfortunately I have no experience with QMediapLayer. I could not see an obvious problem.
You can check also on JIRA for eventual bug reports.
-
Hi,
To add to @koahnig, what version of Qt are you using ?
Did you check the errors of video widget to see how thing are going around ?
-
My Qt version is Qt5.10. No warning or error triggerred.
-
Silly question but, what happens if you don't start the video playing rather than not set the video output on Windows ?
-
@SGaist the text still not updated.
I have clear and rebuild. But still not updated.
I create new project and copy paste the source, the label text can updated. ðŸ˜ðŸ˜ðŸ˜
-
May be a stupid question or comment. But did you rerun qmake in creator too?
-
Where's your QLabel located ?
-
-
It would be helpful if you post the content of the 'ui_mainwindow_1366x768.h' file.
-
Has anyone solved this problem? I encountered this problem too. If a QMediaPlayer is showup in a window, all control in that window won't show the updated UI. So if a QLabel has changed it's text content, you would 't see it. I suspect it has something to do with Media paint event. One (maybe) stupid way to workaround is use QMediaPlayer's unbind method to disconnect QVideoWidget with QMediaPlayer.