const QStaticText
-
Hi
You can use ctor init listclass MyWidget: public QWidget { public: MyWidget(QWidget *parent = 0) : QWidget(parent), m_staticText("This is static text") // init here protected: void paintEvent(QPaintEvent *) { QPainter painter(this); painter.drawStaticText(0, 0, m_staticText); } private: QStaticText m_staticText; // just member };
-
How can I draw a StaticText in the QGraphicsRectItem then? There I is no paintEvent(), right? There is a paint() function, but I don't really get, how to use it.
I'm creating Scrabble and I want to use StaticText to contain letters in "Tile" class, which derives from QGraphicsRectItem.
-
How can I draw a StaticText in the QGraphicsRectItem then? There I is no paintEvent(), right? There is a paint() function, but I don't really get, how to use it.
I'm creating Scrabble and I want to use StaticText to contain letters in "Tile" class, which derives from QGraphicsRectItem.
@MFrost
Since its just a static text, you can do it exact same way but its not paintEvent
you override but
http://doc.qt.io/qt-5/qgraphicsrectitem.html#paint
So its very much the same except the other paint have slightly different signature (parameters)
You might want to call QGraphicsRectItem::paint to let it draw as normally and then
draw your text over it/after. -
Ok
I've got movable square class like that, call "Cell"(unfortunately) and I want to add a text and score to it(best I think would be to add this together as a class object, because they are connected) and then drop its objects on the board.
In its constructor I'm adding Cell object to the main scene and setting its style. Besides that, I also have events to move them and drop on the defined board squares.
In MainWindow I create all Cell objects with a addCells(scene)
Where and how should I use the Cell::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = Q_NULLPTR) function and what should I send to it? What the painter I send needs to be like? When to use painter->drawStaticText?
Is there even a logic in my concept? Am I missing some pretty obvious structure solution?
-
Hi,
paint
will get called automatically for you when the scene needs to get painted. You just have to care to paint whatever yourCell
needs to paint.As for your Cell class, you shouldn't pass the scene to it. It shouldn't care about it at all. Add your cells to your scene in your MainWindow class.
-
Hi
Nope its fine here since it so related.
Well you just need to fill in body of paint.void cell::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget ) {
QGraphicsRectItem::paint( painter, option); //let QGraphicsRectItem draw its normal stuff ( if u want it to draw its rect)
// draw your stuff here
painter->drawStaticText(xxxx)
} -
First, I want to thank You Guys for your help!
Ok, so here I tried to do this, how you showed me:
How is it wrong?