QGraphicsVideoItem in QGraphicsView - qt

I am trying to draw video inside QGraphicsView in QT5.5. Here's the code
QString path = "video.mp4";
qDebug() << QFile::exists(path);
ui->graphicsView->setScene(new QGraphicsScene());
QMediaPlayer pl;
QGraphicsVideoItem vid;
pl.setVideoOutput(&vid);
qDebug() << pl.error();
vid.setSize( QSizeF(1920, 1080) );
pl.setMedia( QUrl::fromLocalFile( path ) );
qDebug() << pl.error();
ui->graphicsView->scene()->addItem(&vid);
ui->graphicsView->resize(1920, 1080);
pl.play();
qDebug() << pl.error();
Nothing is drawn. No errors. File exists. Maybe some issue with plugins? Or hardware acceleration, cause I am on notebook with discrete card. Thank you in advance.

Ok, I got it. My stupid mistake. QMediaPlayer and QGraphicsVideoItem was deleted after exiting scope.

In case someone else runs into this page. You may have to use a QVideoWidget instead of a QGraphicsVideoItem.
https://whynhow.info/30713/How-to-make-friends-QCamera-and-QGraphicsVideoItem
// Note: QGraphicsVideoItem doesn't work but QVideoWidget does
// https://whynhow.info/30713/How-to-make-friends-QCamera-and-QGraphicsVideoItem?
auto *vidWidget = new QVideoWidget;
mpScene->addWidget(vidWidget);
mpCamera = new QCamera(acCamera, this);
mpCamera->setViewfinder(vidWidget);
mpCamera->setCaptureMode(QCamera::CaptureVideo);

Related

QScroll bar range not setting properly

I am trying to set the QScrollbar range using QScrollBar::setrange(min, max). But the values are not reflected when the app is run.
This is the code called from MainWindow constructor.
ui->setupUi(this);
QScrollArea* scroll = new QScrollArea(this);
scroll->setGeometry(ui->frame->geometry());
scroll->setWidget(ui->frame);
scroll->verticalScrollBar()->setRange(0, 1000);
qDebug() << "Max is " << scroll->verticalScrollBar()->maximum();
scroll->show();
PFA the images
Am I missing something. Any leads would be helpful. Thanks in advance.

Mouse click event in qt 3.0.3

Mouse button clicki am trying to create an automatic mouse click event at a particular co ordinate.
This source code moves the mouse pointer to the co ordinate region but it is not clicking.
please help me to solve this problem or suggest any new idea to automate mouse click event.
Note: i am using QT 3.0.3
void mMouseClickFunction()
{
QWidget *d = QApplication::desktop()->screen();
int w=d->width(); // returns desktop width
int h=d->height();
printf("w=%d\nh=%d\n",w,h);
int x,y;
printf("Enter the points...\n");
scanf("%d%d",&x,&y);
QApplication::desktop()->cursor().setPos(x,y);
QPoint pt(x,y);
std::cout << pt.x() << " " << pt.y() << std::endl;
QMouseEvent *e = new QMouseEvent(QEvent::MouseButtonPress, pt,Qt::LeftButton, 0);
QApplication::sendEvent(d, e);
std::cout << "in contentsMousePressEvent"<< e->x() << " " << e->y() << std::endl;
QMouseEvent *p = new QMouseEvent(QEvent::MouseButtonRelease, pt,Qt::LeftButton, 0);
QApplication::sendEvent(d, p);
std::cout << "in contentsMouseReleaseEvent"<< p->x() << " " << p->y() << std::endl;
}
QApplication::sendEvent sends an event internal to the QApplication, not a system wide event. You probably need to be system specific to send out an event like that. Here is the function call for windows:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
But even with that kind of call, you will be limited to certain windows, unless you have UIAccess to be true, and your program is a signed application located in the right part of the harddrive.
EDIT: Here is a page that has some examples for sending input in Linux:
http://www.linuxquestions.org/questions/programming-9/simulating-a-mouse-click-594576/
Hope that helps.

Segmentation fault when accessing the text of QLabel

I got a problem with the QLabel. I got a QtWidget with a QLabel inside. Now I want to change the text of the Label with following code:
QLabel* safetyLabel = this->findChild<QLabel *>("safety_bits");
safetyLabel->setText(QString("test"));
printf("%i", (safetyLabel->text()).length());
but I always get a "Segmentation fault". I think it's something quite simple, but I just can't see it...
Any ideas?
Your safetyLabel can be NULL if you use QtCreators' designer to build your UI and execute your code before you call ui->setupUi(this); in MainWindows constructor.
Here is the code.
QLabel *safetyLabel = NULL;
safetyLabel = (QLabel *) this->findChild("safety_bits");
if(!safetyLabel)
{
qDebug() << "Failed to find safety_bits label!";
return 1;
}
safetyLabel->setText(QString("safety_bits is here"));

Segmentation fault on QPixmap::load

This is the code:
void MainWindow::setPic(QString loc, int panel)
{
if(panel == 1)
{
loc1 = loc;
QPixmap pmap;
facedetect(loc1.toStdString(), 1, "CAND1.jpg");
qDebug() << "Loading : " << loc1;
pmap.load(loc1); // I'm getting the segfault error in this line
qDebug() << "Loaded : " << loc1;
ui->PicView1->setPixmap(pmap);
}
}
How could I resolve this error?
BTW, I'm using Qt 4.8 with the latest Qt Creator, on Ubuntu 12.04.
I tried it with wallpapers, it crashes. I tried it with my 1x1 id pic, crashed (how rude...), I tried it with a strip of my examination schedule (cropped from a different image), it said it loaded, but the QLabel PicView1 doesn't display anything.
I tried declaring the QPixmap as global variable, got segfault.
Don't fill you image with white before loading...
Use QPixmap hence :
QString loc;
// fill loc with a path to your image file.
QPixmap pmap;
pmap.load(loc);
Be carefull, all image type can't be read with QT. Look here QtImageReading to see the datatype supported by QT.

Transparent image in QT

i am new to QT, i got to know how to load a simple image on a window.
i want to know how to make transparent?.
please tell me the way to achieve it.
Thanks
This is how I did it:
canvas = new QImage(":/Zuerich.jpg");
city = new QImage(canvas->size(),QImage::Format_ARGB32);
QPainter p(city);
p.setOpacity(0.1);
p.drawImage(0,0,*canvas);
p.end();
// the proof:
QRgb pix = city->pixel(10,10);
qDebug() << "Alpha" << qAlpha(pix);

Resources