[SOLVED] Custom QPainter widget not drawing
-
I am trying to make a simple drawing widget that draws a line. I've been at this for a hour or so and can't understand what i'm dong wrong. Here's the .h/.cpp
Header
@#ifndef NILEEXPANDER_H
#define NILEEXPANDER_H#include <QWidget>
class NileExpander : public QWidget
{
Q_OBJECT
public:
NileExpander(QWidget *parent = 0);protected:
void paintEvent(QPaintEvent *);signals:
public slots:
};
#endif // NILEEXPANDER_H@
CPP
@#include "nileexpander.h"
#include <QPainter>
#include <QDebug>NileExpander::NileExpander(QWidget *parent) :
QWidget(parent)
{
qDebug() << "Init Nile Expander.";
resize(300,25);
}void NileExpander::paintEvent(QPaintEvent *)
{
qDebug() << "Paint Nile Expander.";
QPainter painter(this);
painter.drawLine(0,0, 300,25);
}
@I have a have a QVBoxLayout I am trying to parent this to. Here's how I've implemented this custom widget:
@ NileExpander* expander = new NileExpander(this);
ui->NodeView->addWidget(expander);
ui->NodeView->addWidget(m_nodeCommon);
ui->NodeView->addWidget(m_nodeDrawable);ui->NodeView->addSpacerItem(spacer);@
I've seen nothing coming through. The painEvent() function isn't getting called. The constructor is getting called, however.
Any suggestions?
-
I figured it out. I assumed the resize(300,25) would automatically set the size of the widget. Turns out I needed this to get it to work:
@ NileExpander* expander2 = new NileExpander();
expander2->setFixedWidth(300);
expander2->setFixedHeight(25);
ui->NodeView->addWidget(expander2);@ -
Hi,
Rather than hardcode sizes like that, why not just use width() and height() in your paint event ?
-
Yea, thats good advice. Now that I've got something coming through, I'll get more fancy. ;)
-
If this solves your problem, please update the thread title prepending [solved] so other forum users may know a solution has been found :)