Search code examples
qttransparentqlabelmplayer

QRubberBand and MPlayer in QLabel


I have already tried many solution that are provided on stackoverflow to make it transparent. I want to make QRubberBand transparent and i am also facing problem regarding that green color which is due to mplayer.

   #include "physician.h"
   #include "ui_physician.h"

    Physician::Physician(QWidget *parent) :
         QMainWindow(parent),
         ui(new Ui::Physician)
    {
         ui->setupUi(this);
         ui->sendROIButton->setStyleSheet(
                    "background-color: #d9d9d9;"
                    "border-radius: 10px;"
                    "color: Black; "
                    "font-size: 15px;"
         );
     }

     Physician::~Physician()
     {
         delete ui;
     }

     void Physician::mouseMoveEvent(QMouseEvent *e)
     {
         rubberBand->hide();
         bottomRight = e->pos();
         QRect rect = QRect(topLeft, bottomRight).normalized();
         rubberBand->setGeometry(rect);//Area Bounding
         QToolTip::showText(e->globalPos(), QString("%1,%2")
        .arg(rubberBand->size().width())
        .arg(rubberBand->size().height()), this);
      }
      void Physician::mousePressEvent(QMouseEvent *e)
      {
        wWidth=ui->videoShowLabel->width();
        wHeight = ui->videoShowLabel->height();
        rubberBand->setGeometry(QRect(0, 0, 0, 0).normalized());
        rubberBand->hide();
        topLeft = e->pos();
       }
       void Physician::mouseReleaseEvent(QMouseEvent *e){
        rubberBand->show();
       }

       void Physician::on_manualROIRadioButton_clicked()
       {
         rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
       }

       void Physician::on_autoROIRadioButton_clicked()
       {
         QString winNuber= QString::number((int)(ui->videoShowLabel->winId()));
         QStringList argsList ;
         argsList << "-slave" << "-quiet" << "-wid" << winNuber << "zoom" << "-
         vo" << "gl" << "C:/../../../Physician21/PhotoshopQML.mkv";
         mplayer_proc = new QProcess;
         mplayer_proc-
         >start("C:/../../../PhysicianTest/mplayer/mplayer.exe",argsList);
        }

Solution

  • Firstly, regarding "QRubberBand should work only on that QLabel". You need to make the QLabel the parent of the QRubberBand to achieve that.

    Secondly, regarding transparency I assume you mean that the output from mplayer should be visible through the rectangle painted by the QRubberBand? I'm not sure you will be able to do that. Doing so would required the rubber band painting logic to act as a compositor but in order to do that it needs to know both the source and destination(mplayer) images. Since mplayer draws directly on the underlying native window Qt has know knowledge of the current destination image and so can not merge the source image with it. I think your best bet would be to find/generate a style that causes QRubberBand to draw the rectangle outline only -- not sure if that's possible. You could subclass it and do your own painting with something like...

    class rubber_band: public QRubberBand {
      using super = QRubberBand;
    public:
      template<typename... Types>
      explicit rubber_band (const Types &... args)
        : super(args...)
        {}
    protected:
      virtual void paintEvent (QPaintEvent *event) override
        {
          QPainter painter(this);
          painter.setPen(Qt::red);
          painter.setBrush(Qt::NoBrush);
          painter.drawRect(rect().adjusted(0, 0, -1, -1));
        }
    };
    

    The above could still leave visual artifacts on the widget though.