QLabels and high speed updating causes crash

On a project I'm working on, I need to update many QLabels very quickly. Each label needs to be updated at about 20 to 50 hertz and there can be over 50 labels at any given time. The problem I'm having is after running the program for about a minute, the labels freeze and the whole program crashes about 10 seconds later. If I comment out the setText() and setNum() calls and reroute the outputs to the console, it runs fine and never crashes. Why does calling SetText() and setNum() so quickly cause the program to crash and how can I prevent this?
Need to see some code.

The following works fine on my machine (Qt 4.8.0):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <QtGui>
#include <limits>

class mainWindow : public QMainWindow {
    Q_OBJECT
    QList<QLabel *> list;
    int x;
public:
    mainWindow() : x(0) {
        QWidget *widget = new QWidget;
        QGridLayout *lo = new QGridLayout(widget);
        for(int i=0; i<60; ++i){
            QLabel *label = new QLabel;
            lo->addWidget(label, i/2, i % 2);
            list.append(label);
        }
        widget->setLayout(lo);
        setCentralWidget(widget);
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(update()));
        timer->start(16);  // ~60 Hertz
     }
public slots:
    void update() {
        if(x>std::numeric_limits<int>::max()-1){
            x=0;
        }
        for(int i=0; i< list.size(); i+=2){
            list[i]->setText(QString("setText %1").arg(++x));
            list[i+1]->setNum(++x);
        }
    }
};

#include "main.moc"

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    mainWindow w;
    w.show();
    app.exec();
}


Edit: added Qt version
Last edited on
When it freezes, if I move the window it continues like normal.
I can't seem to find the problem but here is the code that does the work with the labels.

This is my function that goes through all the labels:

1
2
3
4
5
6
7
void ItemUpdater::doPeriodic()
{
	for (std::map<std::string, DisplayItem*>::iterator it=subscribers.begin(); it!=subscribers.end(); ++it)
	{
		it->second->updateDisplayItem();
	}
}


this function gets called at 60 hertz.

Next is the function that updates the label:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
void Parameter::updateDisplayItem()//Parameter Inherits QLabel
{
	if(information != NULL)
	{
		switch(information->getType())
		{
		case(UdpValueTableParameter::TYPE_STRING):
			{
				setText(information->get<std::string>().c_str());
			}break;
		case(UdpValueTableParameter::TYPE_INT16):
			{
				setNum(information->get<short>());
			}break;
		case(UdpValueTableParameter::TYPE_INT32):
			{
				setNum(information->get<int>());
			}break;
		case(UdpValueTableParameter::TYPE_INT8):
			{
				setNum(information->get<signed char>());
			}break;
		case(UdpValueTableParameter::TYPE_BOOL):
			{
				if(information->get<bool>() == true)
				{
					setText("True");
				}
				else
				{
					setText("False");
				}
			}break;
		case(UdpValueTableParameter::TYPE_FLOAT32):
			{
				setNum(information->get<float>());
			}break;
		default:
			{
				printf("Not supported!\n");
			}break;
		}
	}
}


Also, when the whole window freezes, the console continues to print out data like it did when every thing was still working fine.
Last edited on
TexanMonkey wrote:
if I move the window it continues like normal.
Try calling update() on your main display widget.
Last edited on
that doesn't seem to help
One more guess: Try calling setFocus() on your display widget in doPeriodic() after you iterate the map.

If you post your code somewhere so I can download it, I'll be happy to take a look. Kind of hard to get an Idea of what's going on from the snippets that you have posted.
Registered users can post here. Sign in or register to post.