I need some help!
In qt app I have a central widget. In this central widget I have a QLabel
,and another QWidget
. Now with the help of gstreamer I can get the video from my webcam and show it via the second widget. Now the problem is that I want to take a screenshot of this widget and put it on the label. But the m开发者_如何学Goethods which I used only gave a blank screen.
I tried this :
QPixmap wpix = QPixmap::grabWidget(ui->videoWidget,0,0,640,480);
ui->label->setPixmap(wpix);
and this :
QPixmap wpix=QPixmap(ui->videoWidget->size());
wpix.fill(Qt::transparent);
ui->videoWidget->render(&wpix,QPoint(0,0),QRect(0,0,640,480),QWidget::DrawWindowBackground | QWidget::DrawChildren);
The only thing that works is to use grabWindow but here another thing:
The grabWindow() function grabs pixels from the screen, not from the window, i.e. if there >is another window partially or entirely over the one you grab, you get pixels from the >overlying window, too.
Now the grabWindow() is no use because of this effect, and the above 2 methods don't want to work!
Can someone please tell me what is the problem.Might it be the fact that I set the videoWidget to be a nativeWindow (if I don't set it my video stream would not be shown)?
Please help! If you need more info let me know! And please excuse my bad english!
#include "ksmile.h"
#include "ui_ksmile.h"
#include <gst/interfaces/xoverlay.h>
#include <iostream>
KSmile::KSmile(QWidget *parent) :QMainWindow(parent),ui(new Ui::KSmile)
{
QPalette p(palette());
ui->setupUi(this);
// Set background colour to black
p.setColor(QPalette::Background, Qt::black);
ui->videoWidget->setPalette(p);
ui->videoWidget->setGeometry(0,0,640,480);
ui->videoWidget->setAttribute(Qt::WA_NativeWindow,true);
ui->videoWidget->show();
ui->videoWidget->activateWindow();
QObject::connect(&iTimer, SIGNAL(timeout()), this, SLOT(timerDone()) );
iTimer.start(500);
}
void KSmile::startWebCam(int argc, char *argv[])
{
vs_WebCam = new VideoStream(argc,argv);
vs_WebCam->vs_initWebCam();
gst_x_overlay_set_xwindow_id(GST_X_OVERLAY(vs_WebCam->vs_getVideoSink()),ui->videoWidget->winId());
vs_WebCam->vs_playWebCam();
vs_WebCam->vs_setBrightness(0);
vs_WebCam->vs_setContrast(0);
vs_WebCam->vs_setHue(0);
vs_WebCam->vs_setSaturation(0);
}
void KSmile::timerDone()
{
std::cout << "In timer done " << std::endl;
QPixmap wpix=QPixmap(ui->videoWidget->size());
wpix.fill(Qt::transparent)
ui->videoWidget->render(&wpix,QPoint(0,0),QRect(0,0,640,480),QWidget::DrawWindowBackground | QWidget::DrawChildren);
ui->label->setPixmap(wpix);
/**
wpix = QPixmap::grabWindow(ui->videoWidget,0,0,640,480);
ui->label->setPixmap(wpix); */
}
KSmile::~KSmile()
{
delete ui;
}
精彩评论