2

I'm new to Qt and I'm having some troubles.

I'm trying to draw a simple line on a video, like an overlay, when i click on a button. My video is from a camera, and it is displayed into a QGraphicsScene (or Qframe), it's a live video.

I tried to modify the paintEvent, but the line emerges and disapear when a new frame comes.

I also tried to create an Overlay class, with some attributes (WA_NoBackGround and WA_PaintOnScreen), but this time the line emerges on the video, but the video stops.

Here is my code for the overlay ( inspired by Draw Rectangular overlay on QWidget at click ):

Overlay.h

#include <QApplication>
#include <QWidget>
#include <QPainter>
#include <QPointer>

class Overlay : public QWidget
{
public:
    Overlay(QWidget * parent = 0) ;
protected:
    void paintEvent(QPaintEvent *e );
};

Overlay.cpp

#include "overlay.h"

Overlay::Overlay(QWidget *parent): QWidget (parent)
{
//setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_NoBackground);

setAttribute(Qt::WA_PaintOnScreen);
}

void Overlay::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    p.drawLine(0,0,100,100);
}

The clickDraw button slot :

QPointer<Overlay> m_overlay;
w= static_cast<QWidget*>(ui->r_frame);

if (!m_overlay)
    m_overlay =  new Overlay(w->parentWidget());
m_overlay->setGeometry(w->geometry());
m_overlay->show();
update();

I'm on linux embedded, with Qt 4.8.6

EDIT I tried to create a transparent Qwidget (setOpacity), with the QGraphicsView object as parent, but it did not work, the widget appears white. I think i need a composition manager for X11 (it is what i read from various websites).

Thanks for your help, Greetings

PierreOlivier
  • 1,387
  • 9
  • 23
  • If you're using `QGraphicsScene`, then overriding [drawForeground](http://doc.qt.io/qt-5/qgraphicsscene.html#drawForeground) may be a better approach. – TheDarkKnight Mar 29 '16 at 15:37
  • 1
    How about simply adding another graphics item on top of the video item, and use that to draw the line? – dtech Mar 29 '16 at 16:30
  • @TheDarkKnight I've tried this, the line appears, and disappear instantly under the video. – PierreOlivier Mar 30 '16 at 08:30

1 Answers1

0

For this to work properly you need to use the Qt-provided functionality:

  1. Use a VideoGraphicsItem for playing video, as explained in this fine tutorial: http://doc.qt.io/qt-5/qtmultimedia-multimediawidgets-videographicsitem-example.html
  2. Use a QGraphicsLineItem to add a line to your scene.

Note that Qt Multimedia comes with GStreamer backend, so it should be easy to incorporate your existing GStreamer input.

ypnos
  • 50,202
  • 14
  • 95
  • 141
  • The same as the method of TheDarkKnight, it appears briefly. – PierreOlivier Mar 30 '16 at 08:37
  • the video comes from a camera, the stream is created by gstreamer, and displayed by [videooverlay](https://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-plugins-base-libs/html/gst-plugins-base-libs-gstvideooverlay.html#gst-video-overlay-set-window-handle) I give to this fonction (gst_video_overlay_set_window_handle) th WinId of the QGraphicsView. – PierreOlivier Mar 31 '16 at 07:31
  • Well that obviously means that the widget is overpainted by the video without Qt being able to do anything about it. As you use X11 functionality for it. See my edited answer. – ypnos Mar 31 '16 at 09:59