QGLWidget的QPainter drawText()将中文的中文文本上下颠倒。

时间:2022-09-10 17:57:04

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?

我使用下面的代码在QGLWidget中绘制文本,但是当文本是长和混合中文(或日语)和英语时,文本就会颠倒。有人知道为什么吗?

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

当我将QGLWidget更改为QWidget时,一切正常。

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

我的Qt版本是4.8.0,操作系统是Ubuntu 12.04 64位。

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

PS3:当我先把文本画成图像,然后画出图像,一切都没问题。

Normal when using QWidget: QGLWidget的QPainter drawText()将中文的中文文本上下颠倒。

正常使用时QWidget:

Upside-down when using QGLWidget: QGLWidget的QPainter drawText()将中文的中文文本上下颠倒。

倒在使用QGLWidget:

mainwindow.h

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.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...

我将“默认”字体改为“YaHei as Hybrid”,只在只写一次的时候就变得正常了。但是,当我用下面的代码不时地修改文本时,汉字又会倒过来……

MainWindow.cpp

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

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 个解决方案

#1


0  

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();
}

#1


0  

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();
}