Reputation: 139
I wanted to write an change blindness programm for my girl friends bachelor thesis. It shows for X milli seconds a picture, shows a short "flicker image" (in this case just grey) and then shows a slightly different image then the first one. Then again a flicker image.
Then everythings starts from the beginning. It working fine allready, there are just graphic artifacts. The canvas / QGraphicsView refreshes it not fast enough I think, so they are "lines" during refreshment.
Time for image to be displayed: 70 ms, flicker time: 30 ms. So its 100 ms = 10 FPS per second. I had though this would be possible without using the gpu. But it looks like it isn't.
Can I use double buffering or something to avoid GPU stuff? Or do I need to use QGL for it? Its around 1 Megabyte Images.
I load them once and just display them I think. Maybe I am doing wrong things with the scene.
I will attach my source code maybe you have some ideas.
Kind regards :)
#ifndef PICTURES_H
#define PICTURES_H
#include <QMainWindow>
#include <QImage>
#include <QPixmap>
#include <QGraphicsPixmapItem>
#include <iostream>
#include <QDir>
#include <QGraphicsScene>
#include <QDebug>
#include <QTimer>
namespace Ui {
class pictures;
}
class pictures : public QMainWindow
{
Q_OBJECT
public:
explicit pictures(QWidget *parent = 0);
~pictures();
void loadPics();
void showPics();
public slots:
void stopClicked();
void clearPictures();
void timePassed();
void changeImage();
void changeImageGrey();
private:
int counter;
int image_counter;
int stop_time;
Ui::pictures *ui;
QVector<int> times;
QTimer* timer;
QTimer* timerImageChange;
QTimer* timerGrey;
int imageChangeTime;
int greyTime;
QVector<QGraphicsPixmapItem*> images;
QGraphicsScene* scene;
QGraphicsScene* scene_grey;
QGraphicsPixmapItem* item1;
};
#endif // PICTURES_H
#include "pictures.h"
#include "ui_pictures.h"
pictures::pictures(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::pictures), times()
{
counter = 1;
stop_time = 0;
image_counter = 0;
timer = new QTimer(this);
timerGrey = new QTimer(this);
timerImageChange = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timePassed()));
connect(timerImageChange, SIGNAL(timeout()), this, SLOT(changeImageGrey()));
connect(timerGrey, SIGNAL(timeout()), this, SLOT(changeImage()));
greyTime = 200;
imageChangeTime = 500;
qDebug() << "Hello im here in pictures";
ui->setupUi(this);
this->loadPics();
connect(ui->pushButtonStop, SIGNAL(clicked()), this, SLOT(stopClicked()));
connect(ui->pushButtonNext, SIGNAL(clicked()), this, SLOT(clearPictures()));
timer->start(10);
timerImageChange->start(imageChangeTime);
}
pictures::~pictures()
{
delete ui;
}
void pictures::loadPics()
{
qDebug() << QDir::toNativeSeparators((QDir::current().absolutePath() + QDir::separator () + "debug" + QDir::separator () + "pics" + QDir::separator () + QString::number(counter) + QDir::separator()+"1.jpg"));
//QImage imageOne(QDir::toNativeSeparators(QDir::current().absolutePath() + QDir::separator () + "debug" + QDir::separator () + "pics" + QDir::separator () + QString::number(counter) + QDir::separator()+"1.jpg"));
//QImage imageTwo(QString("./pics/")+counter+"/2.png");
//QImage imageThree(QString("./pics/")+counter+"/3.jpg");
//QImage imageFour(QString("./pics/")+counter+"/2.png");
images.append(new QGraphicsPixmapItem(QDir::toNativeSeparators(QDir::current().absolutePath() + QDir::separator () + "debug" + QDir::separator () + "pics" + QDir::separator () + QString::number(counter) + QDir::separator()+"1.jpg")));
images.append(new QGraphicsPixmapItem(QDir::toNativeSeparators(QDir::current().absolutePath() + QDir::separator () + "debug" + QDir::separator () + "pics" + QDir::separator () + QString::number(counter) + QDir::separator()+"2.jpg")));
images.append(new QGraphicsPixmapItem(QDir::toNativeSeparators(QDir::current().absolutePath() + QDir::separator () + "debug" + QDir::separator () + "pics" + QDir::separator () + QString::number(counter) + QDir::separator()+"3.jpg")));
images.append(new QGraphicsPixmapItem(QDir::toNativeSeparators(QDir::current().absolutePath() + QDir::separator () + "debug" + QDir::separator () + "pics" + QDir::separator () + QString::number(counter) + QDir::separator()+"4.jpg")));
//QGraphicsPixmapItem* item1( QPixmap::fromImage(imageOne));
item1 = images[image_counter];
//QGraphicsPixmapItem item2( QPixmap::fromImage(imageTwo));
//QGraphicsPixmapItem item3( QPixmap::fromImage(imageThree));
//QGraphicsPixmapItem item4( QPixmap::fromImage(imageFour));
scene = new QGraphicsScene;
scene_grey = new QGraphicsScene;
int maximum = item1->boundingRect().width() > item1->boundingRect().height() ? item1->boundingRect().width() : item1->boundingRect().height();
int dimension_width = ui->graphicsView->rect().width();
int dimension_height = ui->graphicsView->rect().height();
//int biggest_canvas_dimension = (((float)item1->boundingRect().width())/dimension_width) > (((float)item1->boundingRect().height())/dimension_height) ? dimension_width : dimension_height;
int biggest_canvas_dimension = dimension_width > dimension_height ? dimension_width : dimension_height;
if(maximum > ui->graphicsView->rect().width() || maximum > ui->graphicsView->rect().height())
{
dimension_width = (int) (item1->boundingRect().width() * ((float)(biggest_canvas_dimension )/maximum));
dimension_height = (int) (item1->boundingRect().height() * ((float)(biggest_canvas_dimension)/maximum));
item1->setPixmap(item1->pixmap().scaled(dimension_width,dimension_height));
qDebug() << "Width: " << dimension_width << " Height " << dimension_height << " Pic High" << item1->boundingRect().height();
}
//item1->setPixmap(item1->pixmap().scaled(QSize(ui->graphicsView->rect().width(), ui->graphicsView->rect().height())));
scene->setSceneRect(ui->graphicsView->rect());
scene->addItem(item1);
scene_grey->setSceneRect(ui->graphicsView->rect());
scene_grey->addItem(images[1]);
ui->graphicsView->setScene(scene);
ui->graphicsView->adjustSize();
//ui->graphicsView->show();
}
void pictures::timePassed()
{
stop_time += 10;
}
void pictures::stopClicked()
{
timerImageChange->stop();
timerGrey->stop();
times.append(stop_time);
ui->pushButtonStop->setEnabled(false);
ui->pushButtonNext->setEnabled(true);
counter++;
qDebug() << "Time: " << stop_time;
stop_time = 0;
timer->stop();
}
void pictures::clearPictures()
{
scene->deleteLater();
images.clear();
loadPics();
ui->pushButtonStop->setEnabled(true);
ui->pushButtonNext->setEnabled(false);
timer->start(10);
timerImageChange->start(imageChangeTime);
}
void pictures::changeImageGrey()
{
timerGrey->start(greyTime);
timerImageChange->stop();
image_counter = (image_counter+1)%4;
//scene_grey->removeItem(scene_grey->items().at(0));
//scene_grey->addItem(images[image_counter]);
scene->removeItem(scene->items().at(0));
scene->addItem(images[image_counter]);
/*int maximum = item1->boundingRect().width() > item1->boundingRect().height() ? item1->boundingRect().width() : item1->boundingRect().height();
int dimension_width = ui->graphicsView->rect().width();
int dimension_height = ui->graphicsView->rect().height();
int biggest_canvas_dimension = dimension_width > dimension_height ? dimension_width : dimension_height;
if(maximum > ui->graphicsView->rect().width() || maximum > ui->graphicsView->rect().height())
{
dimension_width = (int) (item1->boundingRect().width() * ((float)(biggest_canvas_dimension )/maximum));
dimension_height = (int) (item1->boundingRect().height() * ((float)(biggest_canvas_dimension)/maximum));
item1->setPixmap(item1->pixmap().scaled(dimension_width,dimension_height));
}*/
//scene->setSceneRect(ui->graphicsView->rect());
//ui->graphicsView->setScene(scene_grey);
}
void pictures::changeImage()
{
timerImageChange->start(imageChangeTime);
timerGrey->stop();
image_counter = (image_counter+1)%4;
qDebug() << " item1 = images[" + QString::number(image_counter) + "]";
scene->removeItem(scene->items().at(0));
item1 = images[image_counter];
//qDebug() << QDir::toNativeSeparators((QDir::current().absolutePath() + QDir::separator () + "debug" + QDir::separator () + "pics" + QDir::separator () + QString::number(counter) + QDir::separator()+ QString::number(image_counter+1) +".jpg"));
int maximum = item1->boundingRect().width() > item1->boundingRect().height() ? item1->boundingRect().width() : item1->boundingRect().height();
int dimension_width = ui->graphicsView->rect().width();
int dimension_height = ui->graphicsView->rect().height();
int biggest_canvas_dimension = dimension_width > dimension_height ? dimension_width : dimension_height;
if(maximum > ui->graphicsView->rect().width() || maximum > ui->graphicsView->rect().height())
{
dimension_width = (int) (item1->boundingRect().width() * ((float)(biggest_canvas_dimension )/maximum));
dimension_height = (int) (item1->boundingRect().height() * ((float)(biggest_canvas_dimension)/maximum));
item1->setPixmap(item1->pixmap().scaled(dimension_width,dimension_height));
}
//item1->setPixmap(item1->pixmap().scaled(QSize(ui->graphicsView->rect().width(), ui->graphicsView->rect().height())));
//scene->setSceneRect(ui->graphicsView->rect());
scene->addItem(item1);
//ui->graphicsView->setScene(scene);
//ui->graphicsView->setScene(scene);
//ui->graphicsView->adjustSize();
//ui->graphicsView->show();
}
Upvotes: 1
Views: 2221
Reputation:
Remember that if your monitor refresh rate is 60 Hz, then each image is redrawn once in 16 ms. You can't draw with exact timing like 30 ms, only timing in 16.666 ms intervals is possible. And at least in Windows, you can't easily find out when image shown in the monitor is going to change. If your code shows an image for example for 10 ms, then you either see the image or you don't. If you show the image for 25 ms, you either see the image once (16.666 ms) or twice (33.333 ms).
If you really want exact timing, I would recommend using OpenGL where you can time the drawing using monitor's vertical sync information.
The lines you are seeing during drawing are probably tearing. You can get away from that also by using the monitor's vertical sync information.
You are also using the Qt's timer to count 10 ms intervals. That does not work. The timeout is at least 10 ms, but it is not guaranteed to be exactly 10 ms. In practice it is more, so the total time of 100 timeouts is probably around 1100 ms. And even more, depending on your operating system. In Windows, the default timer resolution is 16 ms. Then 100 timeouts add up to about 1700 ms. You can change the timer resolution using timeBeginPeriod.
For more accurate timing, start the timer for the period you want to wait. If you want do something after 70 ms, change the timer interval to 70 ms instead of making seven 10 ms timeouts.
Upvotes: 3