Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Set Qlabel from another thread



  • I have an image in a thread that I would like to visualize it in a label I use the signal and slot but it did not work:
    code:
    QObject::connect(&Watcher, SIGNAL(p),ui->label_graphe, SLOT(setPixmap), Qt::QueuedConnection);

                Any idea please ?


  • @jagl
    Hi,
    your connect seems wrong.
    QObject::connect(&Watcher, SIGNAL(p(QPixmap)),ui->label_graphe, SLOT(setPixmap(QPixmap)), Qt::QueuedConnection);

    If you are using Qt5 or Qt6 it is better to use the new connect format



  • @CP71thanks you for your reply
    yes i'm using QT5
    even with
    :
    QObject::connect(&Watcher, SIGNAL(p(QPixmap)),ui->label_graphe, SLOT(setPixmap(QPixmap)), Qt::QueuedConnection);

    didn't work



  • @jagl
    try
    QObject::connect(&Watcher, &YourClassName::p, ui->label_graphe, &QLabel::setPixmap, Qt::QueuedConnection);

    The compiler should write a possible error



  • @jagl
    What exactly doesnt work? Any errors? Crashes?

    Try

    connect(&Watcher, &WatcherClass::p, ui->label_graphe, &QLabel::setPixmap);
    

    How and where is your watcher created? Any chance that it is going out of scope?



  • @CP71 @Pl45m4 thank you for your reply
    no theres no error or crashes but he didn't show me the pic
    there s the code :

    void graphe()
     {
    .
    .
    .  //code 
    .
    .
    
                                   QChart *chart = new QChart(); // Pointer sur charte
                                   chart->legend()->hide(); // Faire crée la lengend
                                   chart->addSeries(series); // Récuperer les points qui été définit
                                   chart->createDefaultAxes(); // Crée les axes avec échelle automatique
                                   chart->axes(Qt::Vertical).first()->setRange(-3, 3);
                                   chart->axes(Qt::Horizontal).first()->setRange(-10, 10);
                                   chart->setTitle("Lidar"); // définir un titre pour notre courbe // Verifier si on utilise le meme nom de titre du graph dans la meme seconde ca pourra causer des probleme ou pas ??
                                   chart->setBackgroundBrush(QBrush(Qt::black));
                                   QChartView *chartView = new QChartView(chart); // Pointer sur QchartWiew
                                   chartView->setRenderHint(QPainter::Antialiasing); //Appler à la bibliothéque QPainter pour qu'on pussient dessiner la courbe
                                   QMainWindow window; // La création dela fêntre
                                   window.setCentralWidget(chartView); // Intégrerla "charteview" dans la fêntre "window"
                                   window.resize(500, 500); // Définir la dimesion du la fênetre
                                   QString fileim = QDateTime::currentDateTime().toString(QString("'%1/graphe_'yyyy_MM_dd'_'hh_mm_ss'.png").arg(dossier));//creation du fichier csv
                                   QPixmap p = chartView->grab();
    
                                    p.save(fileim);
    }
    }
    void Rover::on_Lidar_clicked()
     {
    
                Watcher.setFuture(QtConcurrent::run(graphe, this->dossier));
    
    
     }
    


  • @jagl

    If watcher is a QFutureWatcher what do you expect from connecting to a signal p, which, I guess, is your QPixmap?

    // Instantiate the objects and connect to the finished signal.
    MyClass myObject;
    QFutureWatcher<int> watcher;
    connect(&watcher, &QFutureWatcher<int>::finished, &myObject, &MyClass::handleFinished);
    
    // Start the computation.
    QFuture<int> future = QtConcurrent::run(...);
    watcher.setFuture(future);
    

    from: https://doc.qt.io/qt-5/qfuturewatcher.html#details
    Read this to see how to use FutureWatcher



  • @Pl45m4 ok thanks
    I made the changes for " Start the computation."
    but I still have problems at the level of connect
    can you help me please



  • @jagl
    Aren't you asking just the same question/problem in https://forum.qt.io/topic/128547/qprogressbar ?

    As we asked you, if you would please take the time to change over to the New Signal Slot Syntax you would save us & yourself a lot of time on what looks like incorrect connect()s.



  • @JonB in the other forum I marked it solved and and for this thread is another topic



  • @jagl
    Fair enough. But from what I saw your issue still appeared to be an incorrect slot passed to connect() via old-style syntax. Which I think may be your issue here too. The answers above show new style syntax. Why don't you do yourself a favour and change over to these once and for all, it solves so many issues about incorrect signals/slots?


Log in to reply