Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot setText() QLabel with QMediaPlayer on the Mainwindow

Cannot setText() QLabel with QMediaPlayer on the Mainwindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mosleim
    wrote on last edited by mosleim
    #1

    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?

    K 1 Reply Last reply
    0
    • M mosleim

      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?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by SGaist
        #3

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mosleim
          wrote on last edited by
          #4

          My Qt version is Qt5.10. No warning or error triggerred.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Silly question but, what happens if you don't start the video playing rather than not set the video output on Windows ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply
            0
            • SGaistS SGaist

              Silly question but, what happens if you don't start the video playing rather than not set the video output on Windows ?

              M Offline
              M Offline
              mosleim
              wrote on last edited by mosleim
              #6

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

              K 1 Reply Last reply
              0
              • M mosleim

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

                K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                @mosleim

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

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Where's your QLabel located ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  M 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Where's your QLabel located ?

                    M Offline
                    M Offline
                    mosleim
                    wrote on last edited by mosleim
                    #9

                    @SGaist in mainwindow form.

                    @koahnig i have rerun qmake. But, still problem. I have copypaste the project and cross compile from linux for windows. Same problem.

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      It would be helpful if you post the content of the 'ui_mainwindow_1366x768.h' file.

                      1 Reply Last reply
                      0
                      • atruinA Offline
                        atruinA Offline
                        atruin
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved