Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLabel does not change visible Text after SetText
Forum Updated to NodeBB v4.3 + New Features

QLabel does not change visible Text after SetText

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 6.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Murtagh
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ckakman
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Murtagh
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ckakman
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Murtagh
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ckakman
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Murtagh
                  wrote on last edited by
                  #8

                  So how can I return to the eventloop?

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved