PDA

View Full Version : Opacity QPainter in thread Qt4.7



cupersuper
9th April 2015, 14:11
Hi,

Qt 4.7. Outside the thread of code works fine:


QImage IMG;
QPainter PAINTER;
QPen PEN;

IMG.load("img.jpg");
PAINTER.begin(&IMG);

PEN.setColor("#ff0000");
PAINTER.setOpacity(0.30);
PAINTER.setPen(PEN);
PAINTER.drawText( 20, 20, "Test");

But when put into the thread drawText crash:

QObject::killTimers: timers cannot be stopped from another thread

If remove the method:

PAINTER.setOpacity(0.30);
or send it to 0 or 1 , starts to work fine.

Tried to change it to:

QColor clr = QColor("#ff0000");
PEN.setColor( QColor( clr.red(), clr.green(), clr.blue(), 254 ) );
but not help

Any ideas?
Thanks in advance)

anda_skoa
9th April 2015, 16:03
Maybe you could show how you put that into a thread.

Cheers,
_

cupersuper
10th April 2015, 06:07
mythread.cpp

MyThread::MyThread(QObject *parent) :
QThread(parent)
{
}

void MyThread::run()
{
QImage IMG;
QPainter PAINTER;
QPen PEN;
IMG.load("img.jpg");
PAINTER.begin(&IMG);


PEN.setColor("#ff0000");
PAINTER.setOpacity(0.30); //This string
PAINTER.setPen(PEN);
PAINTER.drawText( 20, 20, "Test"); //crash this method
IMG.save("done.jpg");
}mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MThread = new MyThread();
MThread->start();
}This code don't work

_______________________________________
mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QImage IMG;
QPainter PAINTER;
QPen PEN;
IMG.load("img.jpg");
PAINTER.begin(&IMG);


PEN.setColor("#ff0000");
PAINTER.setOpacity(0.30);
PAINTER.setPen(PEN);
PAINTER.drawText( 20, 20, "Test");
IMG.save("done.jpg");
}This code works fine