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. QTabWidget: QLabel sometimes paints what's on the last tab

QTabWidget: QLabel sometimes paints what's on the last tab

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 3.9k 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.
  • C Offline
    C Offline
    charles_Tester
    wrote on last edited by
    #1

    I have an application in which the central widget is a tab widget. I use many labels throughout the application, on different tabs. I frequently (once per second) update the labels with new text by calling setText().

    When I switch tabs, however, the labels occasionally get painted with what's displayed on another tab -- it's almost as if the label is momentarily transparent.

    Any ideas on what the problem could be?

    Update: this happens on two different Linux machines: one running Fedora, the other Ubuntu. Also I've added a small piece of sample code below which demonstrates the problem.

    @Andre: In the example I've provided below, the updating occurs in the GUI thread.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kunashir
      wrote on last edited by
      #2

      I may suggest: You updating labels on the timer? If it right, try to use signal/slot, that is updating when the data do change.

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

        That updating, is that done from the UI thread or from another thread?

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

          From the UI thread, as far as I can tell.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            charles_Tester
            wrote on last edited by
            #5

            Kunashir: I think you're on the right track, in that something funky might be happening with a timer/thread.

            I tried sending a signal everytime the data changed, but the problem is the data changes too rapidly (300 times / second) and the GUI becomes unresponsive.

            So I receive the data in a separate thread and store the values there; then once per second the GUI (using a QTimer, yes) polls the values and performs all the setText()'s for the labels.

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

              Also, just to give more information:

              I use currentWidget() to make sure I only setText() on labels that are currently displayed, i.e. that are on the currently displayed tab. What happens, however, is that sometimes the labels seem to paint what was on a previously displayed tab.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                charles_Tester
                wrote on last edited by
                #7

                Here is an example that demonstrates part of the problem, if you switch between Tab1 and Tab2 frequently, you will see that the labels on Tab2 sometimes paint what's on Tab1:

                tabwidget-bug.pro
                @
                QT += core gui

                TARGET = tabwidget-bug
                TEMPLATE = app

                SOURCES += main.cpp
                tabtest.cpp

                HEADERS += tabtest.h
                @
                tabtest.h:
                @
                #ifndef TABTEST_H
                #define TABTEST_H

                #include <QtGui/QWidget>
                #include <QTabWidget>

                class QLabel;
                class QTimer;
                class QTextEdit;

                class TabTest : public QTabWidget
                {
                Q_OBJECT

                public:
                TabTest(QWidget *parent = 0);
                ~TabTest();

                private:

                QWidget * widget1;
                QWidget * widget2;
                
                QLabel * testlabel;
                QLabel * testlabel2;
                QLabel * testlabel3;
                QLabel * testlabel4;
                QLabel * testlabel5;
                
                QTextEdit * edit;
                
                QTimer * timer;
                

                private slots:
                void update_test_label();

                };

                #endif // TABTEST_H
                @
                main.cpp:
                @
                #include <QtGui/QApplication>
                #include "tabtest.h"

                int main(int argc, char *argv[])
                {
                QApplication a(argc, argv);
                TabTest w;
                w.show();

                return a.exec&#40;&#41;;
                

                }
                @
                tabtest.cpp:
                @
                #include "tabtest.h"

                #include <QTextEdit>
                #include <QVBoxLayout>
                #include <QTimer>
                #include <QLabel>

                TabTest::TabTest(QWidget *parent)
                : QTabWidget(parent)
                {

                srand ( time(NULL) );
                
                widget1 = new QWidget;
                widget2 = new QWidget;
                
                testlabel = new QLabel( "  0");
                testlabel2 = new QLabel( "  0");
                testlabel3 = new QLabel( "  0");
                testlabel4 = new QLabel( "  0");
                testlabel5 = new QLabel( "  0");
                
                edit = new QTextEdit( "Test" );
                
                QVBoxLayout * layout1 = new QVBoxLayout;
                QVBoxLayout * layout2 = new QVBoxLayout;
                
                layout1->addWidget( edit );
                layout2->addWidget( testlabel );
                layout2->addWidget( testlabel2 );
                layout2->addWidget( testlabel3 );
                layout2->addWidget( testlabel4 );
                layout2->addWidget( testlabel5 );
                
                widget1->setLayout( layout1 );
                widget2->setLayout( layout2 );
                
                addTab( widget1, "Tab1" );
                addTab( widget2, "Tab2" );
                
                timer = new QTimer();
                connect( timer, SIGNAL( timeout() ), this, SLOT( update_test_label() ) );
                timer->start( 1000 );
                

                }

                TabTest::~TabTest()
                {

                }
                @

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  charles_Tester
                  wrote on last edited by
                  #8

                  Oops, missing part of tabtest.cpp:

                  @
                  void TabTest::update_test_label() {

                  int i = rand() % 100 + 1;
                  
                  QString s = QString("%1").arg(i,3);
                  
                  testlabel->setText( s );
                  
                  i = rand() % 100 + 1;
                  
                  s = QString("%1").arg(i,3);
                  
                  testlabel2->setText( s );
                  
                  i = rand() % 100 + 1;
                  
                  s = QString("%1").arg(i,3);
                  
                  testlabel3->setText( s );
                  
                  i = rand() % 100 + 1;
                  
                  s = QString("%1").arg(i,3);
                  
                  testlabel4->setText( s );
                  
                  i = rand() % 100 + 1;
                  
                  s = QString("%1").arg(i,3);
                  
                  testlabel5->setText( s );
                  

                  }
                  @

                  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