PDA

View Full Version : Diff between QBrush and QPen



wagmare
16th February 2009, 14:03
hi friends,
i try to send the QPen value as argument like
in lineitem.h


class LineItem : public QObject, public QGraphicsLineItem
{
Q_OBJECT
public:
LineItem(const QLineF &rect, const QPen &pen);
private:
int xvalue, yvalue;
QPen pen;
}

in lineitem.cpp


LineItem::LineItem(const QLineF &line, const QPen &pen)
: QGraphicsLineItem(line),
pen(pen)
{
void LineItem ::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
painter->setPen(pen);
painter->drawLine(line());

}


in graphicsview


line1 = new LineItem(QLineF(2,4, 2 ,30), Qt::green);
line1->setPos(100,550);
scene->addItem(line1);


its giving error as
dualmon.cpp:93: error: no matching function for call to ‘LineItem::LineItem(QLineF, Qt::GlobalColor)’
lineitem.h:13: note: candidates are: LineItem::LineItem(const QLineF&, const QPen&)
lineitem.h:8: note: LineItem::LineItem(const LineItem&)
or as

but if the same QPen i replace with QBrush its running perfectly ...
i can send color as argument for QBrush but not for QPen why?

please help :)




}

talk2amulya
16th February 2009, 14:21
the color u gave is a Qt::GlobalColor , when u use QBrush instead of QPen, the following constructor is used(since the second argument is default and first is already given)

QBrush ( Qt::GlobalColor color, Qt::BrushStyle style = Qt::SolidPattern )

for QPen, there is no such constructor :)

wagmare
16th February 2009, 14:26
thanks .. so i have to send QColor instead of QPen ...

Lykurg
16th February 2009, 14:31
thanks .. so i have to send QColor instead of QPen ...

:confused::confused::confused: If you want to sent a QColor why you define a constructor expecting a QPen? What's about defining:


...
LineItem(const QLineF &rect, const QPen &pen);
LineItem(const QLineF &rect, const QColor &color);
LineItem(const QLineF &rect, const Qt::GlobalColor color);


with for example:


LineItem(const QLineF &rect, const Qt::GlobalColor color)
: QGraphicsLineItem(line)
{
pen = QPen(QColor(color));
}


then you will be save.

wagmare
16th February 2009, 15:39
no just i am telling i cant send GlobalColor to QPen .. just send the color value to the constructor .. from the value i will set it to QPen

Lykurg
16th February 2009, 15:47
Well, you can set up a QPen from a GlobalColor. The only thing is that Qt does this not for you. So just do it by yourself by transforming the GlobalColor to a QColor and set this to the QPen.