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?


  • Moderators

    @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.


  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

    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. 😭😭😭


  • Moderators

    @mosleim

    May be a stupid question or comment. But did you rerun qmake in creator too?


  • Lifetime Qt Champion

    Where's your QLabel located ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.