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. Displaying Multiple QLabels in QGraphicsScene
Forum Updated to NodeBB v4.3 + New Features

Displaying Multiple QLabels in QGraphicsScene

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 350 Views 1 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.
  • T Offline
    T Offline
    TrofikdofiK
    wrote on last edited by
    #1

    I have a class A that inherits QLable.

    class_A.h

    class A : public QLabel
    {
        Q_OBJECT;
    
    public:
        A(const A& element);
        ~A() = default;
    
        void setPos();
    protected:
        short x, y;
    };
    

    class_A.cpp

    A::A(ElementType type)
    : QLabel(), type(type), dir(Stop)
    {
        setFixedSize(20, 20);
        x = pos().x();
        y = pos().y();
    };
    A::A(const A& element)
    {
        setFixedSize(20, 20);
        this->x = element.x;
        this->y = element.y;
    }
    
    void A::setPos()
    {
        x = pos().x();
        y = pos().y();
    }
    

    And then there's class B, which inherits QGraphicsScene. And one of the parameters is a class A vector.

    class_B.h

    class B : public QGraphicsScene
    {
    public:
        B(const char* puth, ushort h, ushort w);
       ~B() = default;
    private:
        std::vector<A> otherElement;
    };
    

    class_B.cpp

    B::B(const char* puth)
    : QGraphicsScene()
    {
        for (int i = 0; i < 10; ++i)
        {
            otherElement.emplace_back(GameElement::tWall);
            otherElement[i].move(0, i * 20);
            otherElement[i].setPos();
            otherElement[i].setPixmap(QPixmap("../Texture/wall.png"));
            addWidget(&otherElement[i]);
        }
    };
    

    And main.cpp

    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
        QGraphicsView view;
        B* scene = new B(puth);
        view.setScene(scene);
        view.show();
        return app.exec();
    }
    

    I expect something to be randering on the screen.

    enter image description here

    And in fact, randering

    enter image description here

    And if you change the class_B.cpp as follows

    B::B(const char* puth)
    : QGraphicsScene()
    {
        for (int i = 0; i < 10; ++i)
        {
            otherElement.emplace_back(GameElement::tWall);
            otherElement[i].move(0, i * 20);
            otherElement[i].setPos();
            otherElement[i].setPixmap(QPixmap("../Texture/wall.png"));
            addWidget(&otherElement[i]);
        }
        otherElement.pop_back();
    };
    

    Then we will stop withdrawing anything at all.

    enter image description here

    Why this happens and how to add a few QLables to QGraphicsScene through a loop

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

      Hi and welcome to devnet,

      Pardon my bluntness but why that convoluted architecture when the graphics view framework already has classes to show images as well as text ? Adding QLabels for that is rather a waste of ressources.

      Check the QGraphicsPixmapItem and QGraphicsTextItem.

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

      T 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Pardon my bluntness but why that convoluted architecture when the graphics view framework already has classes to show images as well as text ? Adding QLabels for that is rather a waste of ressources.

        Check the QGraphicsPixmapItem and QGraphicsTextItem.

        T Offline
        T Offline
        TrofikdofiK
        wrote on last edited by
        #3

        @SGaist said in Displaying Multiple QLabels in QGraphicsScene:

        ng QLabels for that is rather a waste of ressources.
        Check the QGraphicsPixmapItem and QGraphicsTextItem.

        The fact is that A is a class from which the classes that display gif animation are also inherited. And in QGraphicsPixmapItem I tested the animation does not go

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

          Then please check this forum post. It shows how to implement a custom item that will handle your animated gif.

          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
          0

          • Login

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