QLabel 中的 QRubberBand 和 MPlayer

QRubberBand and MPlayer in QLabel

我已经尝试了Whosebug 上提供的许多解决方案以使其透明化。 我想让 QRubberBand 透明,我也面临着关于 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);
        }

首先,关于"QRubberBand should work only on that QLabel"。您需要使 QLabel 成为 QRubberBand 的父级才能实现。

其次,关于透明度,我假设您的意思是 mplayer 的输出应该通过 QRubberBand 绘制的矩形可见?我不确定您是否能够做到这一点。这样做需要橡皮筋绘画逻辑充当合成器,但为了做到这一点,它需要知道源图像和目标 (mplayer) 图像。由于 mplayer 直接在底层原生 window Qt 上绘制,因此知道当前目标图像,因此无法将源图像与其合并。我认为您最好的选择是 find/generate 一种导致 QRubberBand 仅绘制矩形轮廓的样式——不确定这是否可能。你可以将它子类化并用类似...

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));
    }
};

不过,以上内容仍可能会在小部件上留下视觉瑕疵。