Los Frijoles
Los Frijoles

Reputation: 4821

QWidget paint even not being called even with non-zero width and height?

I am trying to make a scroll area hold a widget which will serve as a drawing area for a drag-and-drop editor I am trying to build. However, I can't seem to get it to draw.

Here is a picture: https://i.sstatic.net/KbAaJ.png

On the right the black space is what is supposed to be my scrollarea

Here is the constructor for my the window class (I am using Qt-Creator):

ModelWindow::ModelWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ModelWindow)
{
    ui->setupUi(this);

    editor = new ModelEditorWidget(this);

    ui->scrollArea->setWidget(editor);
}

The model editor widget looks like so:

//header

class ModelEditorWidget : public QWidget
{
    Q_OBJECT
public:
    explicit ModelEditorWidget(QWidget *parent = 0);

signals:

public slots:

protected:
    virtual void paintEvent(QPaintEvent *e);

};

//.cpp file:

ModelEditorWidget::ModelEditorWidget(QWidget *parent) :
    QWidget(parent)
{
    this->setAcceptDrops(true);

    this->resize(1000, 1000);

    cout << this->rect().x() << " " << this->rect().width() << endl;

    this->update();
}

void ModelEditorWidget::paintEvent(QPaintEvent *e)
{
    cout << "painting";

    QWidget::paintEvent(e);

    QPainter painter(this);

    painter.setBrush(QBrush(Qt::green));
    painter.setPen(QPen(Qt::red));
    painter.drawRect(400, 400, 50, 50);
    painter.fillRect(e->rect(), Qt::SolidPattern);
}

I would think that this would set the modeleditorwidget's size to be 1000x1000 and then draw a green or red rectangle on the widget. However, the lack of a "painting" message in the command line from the cout at the beginning of the paintEvent shows that it isn't being executed. I first suspected this is because there was 0 width and 0 height on the widget. However, the cout in the constructor tells me that the widget is positioned at x = 0 and width = 1000 and so I assume that since that matches my resize statement that the height is also 1000 as was specified.

EDIT: By calling cout.flush() I got the "painting" output. However, that only deepens the mystery since the paint event doesn't look like it is actually painting. I am now calling show on both the scroll area and the widget as well.

Does anyone see what I could be doing wrong here? Perhaps I am not adding the ModelEditorWidget to the scrollarea properly?

Btw, I am very new to Qt and this is my first major GUI project using it. Most of my other GUI stuff was done using .NET stuff in C#, but since I want this to be cross platform I decided to stay away from C#.NET and mono and use Qt.

Upvotes: 0

Views: 1054

Answers (1)

Bojan
Bojan

Reputation: 736

Documentation of QScrollArea::setWidget() says:

If the scroll area is visible when the widget is added, you must show() it explicitly.

Note that You must add the layout of widget before you call this function; if you add it later, the widget will not be visible - regardless of when you show() the scroll area. In this case, you can also not show() the widget later.

Have you tried that?

Upvotes: 2

Related Questions