16,175
社区成员
发帖
与我相关
我的任务
分享
#ifndef SETTEXT_TEST_H
#define SETTEXT_TEST_H
#include <QtWidgets/QLabel>
class settext_test : public QWidget
{
Q_OBJECT
public:
settext_test();
public slots:
void rand_update();
private:
QLabel *label[20][10];
};
#endif // SETTEXT_TEST_H
// test settext
#include <stdlib.h>
#include <time.h>
#include <QtWidgets/QApplication>
#include <QtCore/QTimer>
#include "settext_test.h"
settext_test::settext_test()
{
this->resize (800, 480);
for (int y = 0; y < 20; y++)
{
for (int x = 0; x < 10; x++)
{
label[y][x] = new QLabel(this);
label[y][x]->resize (79, 19);
label[y][x]->move (80 * x, 24 * y);
}
}
}
void settext_test::rand_update()
{
char text[64] = "";
for (int y = 0; y < 20; y++)
{
for (int x = 0; x < 10; x++)
{
unsigned char r = rand () & 0xFF;
unsigned char g = rand () & 0xFF;
unsigned char b = rand () & 0xFF;
snprintf (text, 64, "<font color=#%02X%02X%02X>%02X%02X%02X</font>",
r, g, b, r, g, b);
label[y][x]->setText (text);
}
}
}
int main(int argc, char *argv[])
{
srand (time (0));
QApplication a(argc, argv);
settext_test widget;
#ifdef linux
widget.setWindowFlags(Qt::FramelessWindowHint); //无边框
#endif
widget.show ();
QTimer t;
t.connect (&t, SIGNAL(timeout()), &widget, SLOT(rand_update()));
t.start (1000);
return QApplication::exec();
}
// end of file
snprintf(text, 64, "<font color=#%02X%02X%02X>%02X%02X%02X</font>",
r, g, b, r, g, b);
label[y][x]->setTextFormat(Qt::PlainText);
label[y][x]->setTextFormat(Qt::RichText);
label[y][x]->setText(text);
你再长时间测试一下吧。我看效果还行