You cannot directly change something in the GUI thread from a different thread. The reason you are getting a segfault is probably because the GUI thread and your "Thread" are not synchronized.
The correct way to do it is to emit a signal from your Thread that is connected to a slot in MainWindow.
{
// ...
private slots:
void onChangeLabel( int mode );
private:
};
{
// ...
signals:
void changeLabel(int mode );
};
MainWindow
::MainWindow( QWidget * parent
) : Qwidget( parent )
{
// ...
connect( m_mainwindowThread, &Thread::changeLabel, this, &MainWindow::onChangeLabel );
}
void Thread::afficheLabelArret()
{
if (m_threadGainable.labelModeFroid == true) {
emit changeLabel( 0 );
else if (m_threadGainable.labelModeChauffage == true) {
emit changeLabel( 1 );
} else {
emit changeLabel( 2 );
}
void MainWindow::onChangeLabel( int mode )
{
switch( mode )
{
case 0:
m_label
->setPixmap
(QPixmap("/home/ludo/Qt/test2/build/images/froid.jpg"));
break;
case 1:
m_label
->setPixmap
(QPixmap("/home/ludo/Qt/test2/build/images/chauffage.jpg"));
break;
default:
m_label
->setPixmap
(QPixmap("/home/ludo/Qt/test2/build/images/abigael.jpg"));
break;
}
}
class MainWindow : public QMainWindow
{
// ...
private slots:
void onChangeLabel( int mode );
private:
QLabel * m_label;
};
class Thread : public QThread
{
// ...
signals:
void changeLabel(int mode );
};
MainWindow::MainWindow( QWidget * parent )
: Qwidget( parent )
{
// ...
connect( m_mainwindowThread, &Thread::changeLabel, this, &MainWindow::onChangeLabel );
}
void Thread::afficheLabelArret()
{
if (m_threadGainable.labelModeFroid == true) {
emit changeLabel( 0 );
else if (m_threadGainable.labelModeChauffage == true) {
emit changeLabel( 1 );
} else {
emit changeLabel( 2 );
}
void MainWindow::onChangeLabel( int mode )
{
switch( mode )
{
case 0:
m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/froid.jpg"));
break;
case 1:
m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/chauffage.jpg"));
break;
default:
m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/abigael.jpg"));
break;
}
}
To copy to clipboard, switch view to plain text mode
The signal / slot mechanism will make sure that the inter-thread call is synchronized correctly.
Bookmarks