Qt QLabel 逐字显示的方法

来源:互联网 发布:smtp 端口 是多少 编辑:程序博客网 时间:2024/04/30 10:24

起因:嵌入式里QLabel显示一大堆中文时,效率相当的差。
由于中文的编码和解码的方式特殊,在显示中文文字时,需要一个个的去检索字体,造成CPU 负荷相当的重,致使绘制效率也变低。因此本人(Venus)采用了逐字显示的方法来提高效率,而且显示上也很特别,有点像事实播报一样,具体实现代码如下:

#include <QtCore>#include <QtGui>class Test: public QLabel{    Q_OBJECTpublic:    Test()        :m_inc(0)    {        connect(&m_time, SIGNAL(timeout()), SLOT(changed()));        m_time.start(30);        QLabel::setWordWrap(true);        setWordWrap(true);    }    void setText ( const char *text )    {       m_text = m_text.fromLocal8Bit(text);    }private slots:    void changed()    {        m_inc ++;        if (m_inc >= m_text.size())            m_time.stop();        else            QLabel::setText(m_text.left(m_inc));    }private:    int m_inc;    QTimer m_time;    QString m_text;};
#include "label.h"int main(int argc, char *argv[]){    QApplication app(argc, argv);    QWidget win;    win.setFixedSize(800,480);    QVBoxLayout *box = new QVBoxLayout;    win.setLayout(box);    Test *a = new Test;    box->addWidget(a);    a->setText("我现在有一Buffer里存放了16bit 的图像数据,我想让它在QT程序里显示出来该怎么办呢?在之前我用Qimage 类构造一个image图,但显示出来的图像颜色不对。实现的过程如下 :");    win.show();    return app.exec();}

原创粉丝点击