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. Help with QThread
Forum Updated to NodeBB v4.3 + New Features

Help with QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.2k Views 1 Watching
  • 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.
  • Z Offline
    Z Offline
    ziggx
    wrote on last edited by
    #1

    Hi There,

    I have a VideoPlayer and I want to put it into a separate thread, but I'm not sure how to do this. Here's my (shortened) code`

    VideoPlayer :: VideoPlayer(QWidget *parent)
        :QGraphicsScene(parent)
        ,mediaPlayer(0,QMediaPlayer::VideoSurface)
        ,videoItem(0)
    {
        videoItem=new VideoItem;
        mediaPlayer.setVideoOutput(videoItem);
        addItem(videoItem);
    }
    

    And here's the code for the VideoItem:

    VideoItem::VideoItem(QGraphicsItem *parent)
        : QGraphicsItem(parent)
        , imageFormat(QImage::Format_Invalid)
        , framePainted(false)
    {
    
    }
    
    VideoItem::~VideoItem()
    {
    }
    
    QRectF VideoItem::boundingRect() const
    {    
        return QRectF(QPointF(0,0), surfaceFormat().sizeHint());
    }
    
    void VideoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        Q_UNUSED(option);
        Q_UNUSED(widget);
    
        if (currentFrame.map(QAbstractVideoBuffer::ReadOnly)) {
            const QTransform oldTransform = painter->transform();
    
            if (surfaceFormat().scanLineDirection() == QVideoSurfaceFormat::BottomToTop) {
               painter->scale(1, -1);
               painter->translate(0, -boundingRect().height());
            }
    
            painter->drawImage(boundingRect(), QImage(
                    currentFrame.bits(),
                    imageSize.width(),
                    imageSize.height(),
                    imageFormat));
    
            painter->setTransform(oldTransform);
    
            framePainted = true;
    
            currentFrame.unmap();
        }
    }
    
    QList<QVideoFrame::PixelFormat> VideoItem::supportedPixelFormats(
            QAbstractVideoBuffer::HandleType handleType) const
    {
        if (handleType == QAbstractVideoBuffer::NoHandle) {
            return QList<QVideoFrame::PixelFormat>()
                    << QVideoFrame::Format_RGB32
                    << QVideoFrame::Format_ARGB32
                    << QVideoFrame::Format_ARGB32_Premultiplied
                    << QVideoFrame::Format_RGB565
                    << QVideoFrame::Format_RGB555;
        } else {
            return QList<QVideoFrame::PixelFormat>();
        }
    }
    
    bool VideoItem::start(const QVideoSurfaceFormat &format)
    {
        if (isFormatSupported(format)) {
            imageFormat = QVideoFrame::imageFormatFromPixelFormat(format.pixelFormat());
            imageSize = format.frameSize();
            framePainted = true;
    
            QAbstractVideoSurface::start(format);
    
            prepareGeometryChange();
            emit sizeChanged(boundingRect());
            return true;
        } else {
            return false;
        }
    }
    
    void VideoItem::stop()
    {
        currentFrame = QVideoFrame();
        framePainted = false;
    
        QAbstractVideoSurface::stop();
    }
    
    bool VideoItem::present(const QVideoFrame &frame)
    {
        if (!framePainted) {
            if (!QAbstractVideoSurface::isActive())
                setError(StoppedError);
    
            return false;
        } else {
            currentFrame = frame;
            framePainted = false;
            update();
            emit frameChanged();
            return true;
        }
    }
    
    

    I think I need to put the VideoItem's present function into a QThread but I am not sure how to do it. So my question is: is it the right place to use QThread and if so how do I go about it? Any help or suggestions would be gratefully received.

    Thanks in advance
    Best wishes
    Ziggx

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You cannot have any widgets (GUI) in another thread. That is not allowed at all.
      All GUI must stay in main thread.
      But why you want to run player in a thread ?

      1 Reply Last reply
      1
      • Z Offline
        Z Offline
        ziggx
        wrote on last edited by
        #3

        The app is a bit sluggish when running video - I use videoitem to emit a signal whenever there is a new frame (I need this to update timecode). I had hoped that using Thread would let it run faster. But from what you say, I simply can't do that.

        Hmm... I'll have to think again.

        Thanks for your reply

        Best wishes
        Ziggx

        jsulmJ 1 Reply Last reply
        0
        • Z ziggx

          The app is a bit sluggish when running video - I use videoitem to emit a signal whenever there is a new frame (I need this to update timecode). I had hoped that using Thread would let it run faster. But from what you say, I simply can't do that.

          Hmm... I'll have to think again.

          Thanks for your reply

          Best wishes
          Ziggx

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ziggx said in Help with QThread:

          I need this to update timecode

          How heavy is this updating?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • Z Offline
            Z Offline
            ziggx
            wrote on last edited by
            #5

            It needs to run on every new frame. :(

            jsulmJ 1 Reply Last reply
            0
            • Z ziggx

              It needs to run on every new frame. :(

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @ziggx It's not what I asked. The question is: how long does each calculation for this update take?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • Z Offline
                Z Offline
                ziggx
                wrote on last edited by
                #7

                Hmm... checked the elapsed time for that function and it is 1ms or less. The problem is obviously somewhere else. I'm on OSX and the activity monitor shows I have lots of idle wakeups - could the OS be slowing things down?

                Best wishes
                Ziggx

                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