QLabel does not change visible Text after SetText



  • Hi,
    I have a QT Project with a opengl-window. I save the coordinates of the cursor in my opengl window, called glwidget:
    @std::vector<double> actual_position;@

    At runtime I try to print the x,y and z value to a Label, called CoordLabel

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include "mouseinput.h"

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

    connect(this,SIGNAL(updateCoords(QString)),ui->CoordLabel,SLOT(setText(QString)));
    connect(this,SIGNAL(updateCoords(QString)),ui->CoordLabel,SLOT(repaint()));
    
    // include a update function so the window reloads every 20ms
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateWindow()));
    timer->start(30);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::updateWindow()
    {
    ui->openglwidget->update();
    // update position every frame
    emit updateCoords(QString(" X: %1\n Y: %2\n Z: %3").arg(ui->openglwidget->actual_position.at(0)).arg(ui->openglwidget->actual_position.at(1)).arg(ui->openglwidget->actual_position.at(2)));
    ui->CoordLabel->repaint();
    qApp->processEvents();
    QString temp = ui->CoordLabel->text();
    std::cout << temp.toStdString()<< std::endl;
    }@

    I recognized, that the label is only updated, if the label gets so big, that the glwidget size is changed and the complete window has to be redrawn.
    Otherwise the text of the Label is not changed in the GUI.
    I add the "cout" part to test, if the label-text changes. And yes it prints out the right values on my console.

    I already tried out update(),repaint(),...

    Thank you for help.



  • Hi,

    I don't have any experience with QGLWidget but I would like to make suggestion. Could try calling @qApp->processEvents(QEventLoop::ExcludeUserInputEvents);@
    with each update instead of (indirectly) calling the repaint() or update() slot?



  • I tried is out but it did't work.
    The QGLWidget works perfectly. But I think that it has some kind of higher priority, so the gui is never repainted as long as the QGLWidget works.

    Is there any setting that a label repaints every x seconds or something like that? Didn't find anything like that yet.



  • I don't think any particular widget has such a repaint timer.

    Have you tried with a reduced QTimer interval to see whether the Qt's event loop catches up?



  • I tried it out: with 2 seconds per frame the problem stays the same



  • If you change the text on a QLabel, it is updated ASAP without calling update/repaints etc. If it doesn't work even with 2 second timer interval then there is something fishy going on.

    I don't have any experience with Qt and OpenGL integration so I don't know whether somehow OpenGL messes up Qt's drawing machinery.



  • Sorry, but no. Updating a label does result in a call to update, which results in a paint event which results in the actual rendering. That's why calling setText() in a loop without returning to the eventloop doesn't work as expected.



  • So how can I return to the eventloop?



  • Make sure your functions don't run forever.

    Qt is an event-driven system. That is: things happen, and these things cause events. Events can be handled using your own functions. As long your code is running, new events are not processed. So, best make sure that your own code does not run for too long. That is especially true in the main (GUI) thread, that handles things like painting and handling mouse and keyboard events.

    If you do have code that needs to run for a longer time, there are multiple ways to deal with that. You could use threads, but you can also chop up your work in smaller pieces and use something like a timer with a 0 timeout to start processing the next chunk. There are more options, of course.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.