Displaying Multiple QLabels in QGraphicsScene
-
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.
And in fact, randering
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.
Why this happens and how to add a few QLables to QGraphicsScene through a loop
-
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.
-
@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
-
Then please check this forum post. It shows how to implement a custom item that will handle your animated gif.
1/4