QPainter drawText() in QGLWidget draw long Chinese text with English upside down

1.6k views Asked by At

I use codes below to draw text in QGLWidget, but when the text is long and mixed Chinese(or Japanese) with English, then the text becomes upside down. Anyone knows why?

PS1: When I change QGLWidget to QWidget, everything is OK.

PS2: My Qt version is 4.8.0, OS is Ubuntu 12.04 64bit

PS3: When I firstly draw the text into an image, then draw that image, everything is ok.

Normal when using QWidget: normal text

Upside-down when using QGLWidget: upside down text

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QGLWidget>

class MainWindow : public QGLWidget
{
Q_OBJECT

public:
    explicit MainWindow(QGLWidget *parent = 0);

protected:
    void paintEvent(QPaintEvent* event);
};

#endif // MAINWINDOW_H

mainwindow.cpp

MainWindow::MainWindow(QGLWidget *parent) : QGLWidget(parent)
{
    setGeometry(0, 0, 1000, 200);
}

void MainWindow::paintEvent(QPaintEvent* event) {
    QPainter painter;
    painter.begin(this);

    QString text = QString::fromLocal8Bit(
                "THIS IS一段A很长的LONG示例文本TEXT。在非常长的时候会出现上下  颠倒的问题。你那里是否出现了?" \
                "一段很长的示例文本。在非常长的时候会出现上下颠倒的问题。你那里是否出现了?" \
                "一段很长的示例文本。在非常长的时候会出现上下颠倒的问题。你那里是否出现了?"
                );

    painter.setFont(QFont("default", 46, -1, false));
    painter.drawText(QRect(0, 0, 3000, 200), Qt::TextSingleLine, text);
    painter.end();
}

**

Edit:

**

I change font family name "default" to "YaHei Consolas Hybrid", it becomes normal when only drawText once. But When I use codes below to change text at intervals, the Chinese Characters become upside down again...

MainWindow.cpp

#include <QtGui>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QGLWidget *parent) : QGLWidget(parent)
{
    setGeometry(0, 0, 2000, 100);

    paintTimer = new QTimer(this);
    connect(paintTimer, SIGNAL(timeout()), this, SLOT(repaint()));
    paintTimer->start(16);

    text1 ="【中文1024测试】ABCDEFGHIJKLMNOPQRSTUVWXYZ中文ABCDEFGHIJKLMNOPQRSTUVWXYZ"\
            "中文ABCDEFGHIJKLMNOPQRSTUVWXYZ中文ABCDEFGHIJKLMN";

    text2 = "中文English中文English中文English中文English中文Englis中文";

    text = text2;

    offset = 0;
}

void MainWindow::paintEvent(QPaintEvent* event) {
    QPainter painter;
    painter.begin(this);

    painter.setFont(QFont("YaHei Consolas Hybrid", 40, -1, false));
    painter.drawText(QRect(0, 0, 3000, 100), Qt::TextSingleLine, text);
    painter.end();

    offset= (offset + 8) %400;

    if (offset > 200) {
        text = text1;
    } else {
        text = text2;
    }
}

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QGLWidget>
#include <QTimer>
#include <QFont>

class MainWindow : public QGLWidget
{
    Q_OBJECT

public:
    explicit MainWindow(QGLWidget *parent = 0);

protected:
    void paintEvent(QPaintEvent* event);
private:
    QTimer* paintTimer;
    QString text1;
    QString text2;
    QString text;
    int offset;
};

#endif // MAINWINDOW_H
1

There are 1 answers

1
Loong On BEST ANSWER

The codes below can solve the upside down problem:

void MainWindow::paintEvent(QPaintEvent* event) {
    qglClearColor(QColor(0,0,0));
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QPainter painter(this);
    painter.beginNativePainting();
    renderText(-offset, 60, 0, text, QFont("YaHei Consolas Hybrid", 40, 1, false));
    painter.endNativePainting();
}