Custom QWidget wont repaint properly



  • Hello there,

    So I seem to be having some issue with repainting my custom widget.

    This image has file1 loaded at start up, everything is fine
    This image shows file2 loaded up at start up, everything is fine.
    This image shows file2 being loaded after file1, the QString is updated and other variables of the class are updated but as you can see in the photo the end of each line is chopped off and the last few lines are chopped off.

    Any help will be appreciated, thank you!

    UPDATE:

    Here is a little program I made that re-produces the problem.

    main.cpp

    #include "widget.h"
    #include <QApplication>
    #include <QtWidgets>
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        
        Widget *w = new Widget;
        QScrollArea area;
       
        area.setWidget(w);
        area.show();
    
        //change string and other data variables
        w->lines = "1234567890\nQWERTYU123\nLETTERS123\nWORDSSS123\n"
                   "TESTING123\nFUNCTIO123\nLETTERS123\nWORDSSS123\n";
        w->lineSize = 10;   // each line is 10 characters long
        w->rowSize = 7;     // there are 8 rows
    
        // update everything
        w->updateGeometry();
        w->update();
        area.updateGeometry();
        area.update();
    
        return a.exec();
    }
    
    

    Widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QString>
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Widget(QWidget *parent = 0);
    
        QString lines;
        int lineSize = 7, rowSize = 4;
    
        QSize sizeHint() const override;
    protected:
        void paintEvent(QPaintEvent *event) override;
    
    };
    
    #endif // WIDGET_H
    
    

    Widget.cpp

    #include "widget.h"
    #include <QtWidgets>
    
    Widget::Widget(QWidget *parent) : QWidget(parent)
    {
        lines = "TESTING\nFUNCTIO\n1234567\nWORDS77\n";
        update();
    }
    
    
    void Widget::paintEvent(QPaintEvent * /*event*/)
    {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
        
        QFont defaultFont = painter.font();
        QPen defaultPen = painter.pen();
        defaultFont.setLetterSpacing(QFont::AbsoluteSpacing, lineSize / 2 - 3);
        // set pen and font
        painter.setFont(defaultFont);
        painter.setPen(defaultPen);
        // starting point
        QPoint prntPoint(5, 15);
    
        for (QString::size_type ind = 0, row = 1; ind < lines.size(); ++ind)
        {
            if (lines[ind] != '\n') // if it's a letter
            {
                // print the letter and move the point to the right
                painter.drawText(prntPoint, QString(lines[ind]));
                prntPoint.setX(prntPoint.x() + 20);
            }
            else if (lines[ind] == '\n') // if the character is a newline
            {
                prntPoint.setX(5); // move point back to the very left
                prntPoint.setY(row * 20 + 15); // move down
                ++row;
            }
        }
    }
    
    QSize Widget::sizeHint() const
    {
        return QSize(lineSize * 20, rowSize * 20);
    }
    
    

    This is the resulting program.
    As you can see the end of each line and the last few lines are cut off. Which were set in main.cpp. The digits 890 are cut off the first line for example.

    Removing the QScrollArea fixes the issue but of course the scroll bars go which is why I'm using a QScrollArea.

    Here is the main.cpp file with the QScrollArea removed:

    #include "widget.h"
    #include <QApplication>
    #include <QtWidgets>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        Widget *w = new Widget;
        w->show();
    
        //change string and other data variables
        w->lines = "1234567890\nQWERTYU123\nLETTERS123\nWORDSSS123\n"
                   "TESTING123\nFUNCTIO123\nLETTERS123\nWORDSSS123\n";
        w->lineSize = 10;   // each line is 10 characters long
        w->rowSize = 7;     // there are 8 rows
    
        // update everything
        w->updateGeometry();
        w->update();
    
        return a.exec();
    }
    

    Solution

    "I think that you have to resize it manually when the data chages, because the sizeHint it's only a recommended size that is used e.g when the item is into a layout or when the widget is shown the first time as top level widget. You can use resize(sizeHint()) or setFixedSize(sizeHint()) – Fabio"

    From stakoverflow



  • This post is deleted!

Log in to reply
 

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