Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. const QStaticText
Forum Updated to NodeBB v4.3 + New Features

const QStaticText

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MFrost
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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

      1 Reply Last reply
      4
      • M Offline
        M Offline
        MFrost
        wrote on last edited by
        #3

        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.

        mrjjM 1 Reply Last reply
        0
        • M MFrost

          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.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

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

          1 Reply Last reply
          3
          • M Offline
            M Offline
            MFrost
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MFrost
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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)
                  }

                  1 Reply Last reply
                  2
                  • M Offline
                    M Offline
                    MFrost
                    wrote on last edited by
                    #9

                    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?

                    jsulmJ 1 Reply Last reply
                    0
                    • M MFrost

                      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?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @MFrost Just add

                      #include <QStaticText>
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      3

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved