Domanda

I've a problem with sending QImage to GUI-thread:

this is my code in child-thread:

QSize size = ui->label_2->size();
size=ui->label_2->size();
QImage pic(size.width(),size.height(),QImage::Format_ARGB32_Premultiplied);
pic.fill(Qt::transparent);
QPainter painter(&pic);
for (unsigned int i=0; i < wayVector.size(); i++){
    double *x = new double[wayVector[i].refs.size()];
    double *y = new double[wayVector[i].refs.size()];
    for (unsigned int j=0; j<wayVector[i].refs.size(); j++){
        x[j]=nodeHash[wayVector[i].refs[j]].lon;
        y[j]=nodeHash[wayVector[i].refs[j]].lat;
    }
    for (unsigned int j=0; j<wayVector[i].refs.size()-1;j++){
        painter.setPen(Qt::green);
        painter.drawLine(size.width()*x[j]/(maxlon-minlon),
                         size.height()*maxlat/(maxlat-minlat)-size.height()*y[j]/(maxlat-minlat),
                         size.width()*x[j+1]/(maxlon-minlon),
                         size.height()*maxlat/(maxlat-minlat)-size.height()*y[j+1]/(maxlat-minlat));
    }
}
emit sendPixmap(pic);

This is signal/slot connection:

 connect(this,SIGNAL(sendPixmap(QImage)),this,SLOT(setImage(QImage)));

And this is definition of slot:

void MainWindow::setImage(QImage img){
    ui->label_2->setPixmap(QPixmap::fromImage(img));
}

But nothing happened, label clears and no image appears. What I'm doing wrong? Waiting for your help :(

È stato utile?

Soluzione

From the documentation:

When using QPainter on a QImage, the painting can be performed in another thread than the current GUI thread

So what you intend to do should work. Are you sure you have set up your thread correctly? Your usage of ui->label_2 looks very suspicious, do you have ui elements in your thread or do you access GUI elements from your thread? Perhaps you should show us more of your code.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top