⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

const QStaticText

  • const QStaticText tt("AA");

    How should I correctly initialize it?

    I feel kind of silly asking that, but I cannot come up with a solution.

  • Lifetime Qt Champion

    You can use ctor init list

    class MyWidget: public QWidget
        MyWidget(QWidget *parent = 0) : QWidget(parent), m_staticText("This is static text") // init here
        void paintEvent(QPaintEvent *)
            QPainter painter(this);
            painter.drawStaticText(0, 0, m_staticText);
        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.

  • Lifetime Qt Champion

    Since its just a static text, you can do it exact same way but its not paintEvent
    you override but
    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.
    alt text

    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.
    alt text

    In MainWindow I create all Cell objects with a addCells(scene)
    alt text

    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?

  • Should I end this topic and start another with my latest post?

  • Lifetime Qt Champion


    paint will get called automatically for you when the scene needs to get painted. You just have to care to paint whatever your Cell 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.

  • Lifetime Qt Champion

    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

  • First, I want to thank You Guys for your help!

    Ok, so here I tried to do this, how you showed me:

    alt text
    alt text

    How is it wrong?

  • Lifetime Qt Champion

    @MFrost Just add

    #include <QStaticText>

Log in to reply