Text drawn onto custom widget with QPainter is garbled after scrolling the widget



  • Hello all,

    I've searched the forums and the Internet for someone with a similar problem, but haven't found anything.

    I've been using QPainter for some rendering code in a program I've been working on. In the current design, a string needs to be painted onto a custom widget. This widget is then inserted into a QScrollArea and displayed in the main window. Below is a minimal compilable example:

    @
    #include <QtGui>
    #include <QScrollArea>
    #include <QApplication>
    #include <QMainWindow>
    #include <QWidget>
    #include <QScrollArea>
    #include <QLayout>

    class PaintArea : public QWidget
    {
    public:
    PaintArea(QWidget *parent);

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

    PaintArea::PaintArea(QWidget *parent) : QWidget(parent)
    {
    }

    void PaintArea::paintEvent(QPaintEvent *event)
    {
    Q_UNUSED(event);

    QPainter *qPainter = new QPainter(this);
    QRect boundingRect;
    QString filler = QString::fromStdString(
                "Hello world! This is a really long string that I'm typing to demonstrate the issues that I'm having with scrolling in Qt. They're getting extremely annoying, so I'd like to be able to resolve them soon.");
    qPainter->drawText(this->geometry(), Qt::TextWordWrap, filler, &boundingRect);
    this->setMinimumHeight(boundingRect.height());
    
    delete qPainter;
    

    }

    class MainWindow : public QMainWindow
    {
    public:
    MainWindow();

    private:
    PaintArea *paintArea;
    QScrollArea *scrollArea;
    QWidget *centralLayout;
    };

    MainWindow::MainWindow()
    {
    paintArea = new PaintArea(this);
    scrollArea = new QScrollArea(this);
    scrollArea->setWidget(paintArea);
    scrollArea->setWidgetResizable(true);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(scrollArea);
    
    centralLayout = new QWidget(this);
    centralLayout->setLayout(layout);
    
    setCentralWidget(centralLayout);
    

    }

    int main(int argc, char* argv[])
    {
    QApplication app(argc, argv);
    MainWindow window;
    window.show();

    return app.exec&#40;&#41;;
    

    }
    @

    When this code is run, if you resize the window to the point where the scrollbar appears, and then scroll up and down, the problem becomes apparent. It looks like this:

    !https://dl-web.dropbox.com/get/Photos/Compressed.png?w=AAAMTMAV1fRm6jd5cEaO43iFqGT6BYAdwjtgj_164XpdVw(Compressed text)!

    However, as soon as the window is resized, brought to an active state, or has its paint event called in any way, the text is corrected, so it looks like this:

    !https://dl-web.dropbox.com/get/Photos/Uncompressed.png?w=AACt91XiL6BAIYbLHEejrHyxVIxGUlDXATn0LHkT8lsR2w(Correctly displayed text)!

    My guess is that this is a problem with the PaintEvent being called during scrolling, but I haven't been able to confirm this.

    I've tested this problem on Mac OS X 10.8/10.9 and an up to date Arch Linux, both using Qt 5.1.1. If anyone has any ideas about how to either correct this or display the string in a way that this can be corrected, I'd be interested in hearing them. Thanks!



  • Hi, your problem has been solved? I need your help. Thanks



  • Q_UNUSED(event);
    QPainter qPainter(this);
    qPainter.setFont(font());
    QRect boundingRect;
    qDebug()<<boundingRect.height();
    QString filler = QString::fromStdString(
    "Hello world! This is a really long string that I'm typing to demonstrate the issues that I'm having with scrolling in Qt. They're getting extremely annoying, so I'd like to be able to resolve them soon.");
    qPainter.drawText(rect(), Qt::TextWordWrap, filler, &boundingRect);
    setMinimumHeight(boundingRect.height());
    }



  • Hello.

    I've run into the same problem (QT 5.4.1, Windows 8).
    Has anyone found a solution to this?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.