Qlable.setpixmap showing segmentation fault
-
if i use a qlable inside qtcuncurrent function showing segmentation fault
this is my code
void MainWindow::image_plot() { while (1) { QImage img(RxBuffer, 2048, 1, QImage::Format_RGB888); ui->label->setPixmap(QPixmap::fromImage(img.rgbSwapped())); } }
-
I assume your
ui->port
is aQLabel
[sic.], though it makes life easier if you say so.-
You must not directly access any UI element from any thread other than the main UI one. I guess you are using your method above in a QtConcurrently-run thread?
-
While you can use
QImage
in a non-UI thread, I believe you cannot useQPixmap
s either.
-
-
@swansorter
I don't know why you are using threads --- about half the time people use them they don't need to. But assuming you do, you can only communicate with the main UI thread by sending a signal, with whatever (non-UI-type) parameters are necessary which the UI thread has a slot on and acts upon receiving the signal.For example, since I believe secondary threads can use
QImage
but cannot useQPixmap
(and certainly not callQLabel::setPixmap()
) I think you couldemit imageCaptured(img)
fromimage_plot()
and have a slotMainWindow::onImageCaptured(const QImage &img)
which does theui->label->setPixmap(QPixmap::fromImage(img.rgbSwapped()));
in the main thread.