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.


  • Qt Champions 2017

    Hi
    You can use ctor init list

    class 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
    };
    

    http://doc.qt.io/qt-5/qstatictext.html



  • 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.


  • Qt Champions 2017

    @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.
    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

    Hi,

    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.


  • Qt Champions 2017

    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:

    alt text
    alt text

    How is it wrong?


  • Moderators

    @MFrost Just add

    #include <QStaticText>
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.