Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How can I pass QPixmap to different class
Creating a game that sends QPixmaps across the scene to shoot at. I have created an array of QPixmaps in my dialog class that I select randomly to draw.
Dialog::Dialog(QWidget *parent) :
// Create and configure scene scene = new Scene; scene->setBackgroundBrush(Qt::black); scene->setItemIndexMethod(QGraphicsScene::NoIndex); ui->graphicsView->setScene(scene); scene->setSceneRect(-200, -150, 400, 300); ui->graphicsView->setMouseTracking(true); QPixmap tankbase1(":/images/tankbase.jpg"); ui->tankbaseplay1->setPixmap(tankbase1); //Store targets in array and random generator index = 0; main_targets = QPixmap(":images/darkbluelogo.jpg)"); main_targets = QPixmap(":images/graylogo.jpg"); main_targets = QPixmap(":images/lightbluelogo.jpg"); main_targets = QPixmap(":images/limE.jpg"); main_targets = QPixmap(":images/pink.jpg"); main_targets = QPixmap(":images/purple.jpg"); main_targets = QPixmap(":images/redlogo.jpg"); main_targets = QPixmap(":images/yellow.jpg"); main_targets = QPixmap(":images/brown.jpg"); index = qrand((index % 9) + 1); //scene->addItem(main_targets[index]); //Timer for scene advancement QTimer *timer = new QTimer(); QObject::connect(timer, SIGNAL(timeout()), scene, SLOT(advance())); timer->start(100);
QGraphicsTextItem *FirstP; QString P1 = "Player1"; FirstP = scene->addText(P1); FirstP->setFont(QFont("Nimbus Mono L", 12,QFont::Bold)); FirstP->setDefaultTextColor(Qt::white); FirstP->setPos(-300, -220); QGraphicsTextItem *SecondP; QString P2 = "Player2"; SecondP = scene->addText(P2); SecondP->setFont(QFont("Nimbus Mono L", 12,QFont::Bold)); SecondP->setDefaultTextColor(Qt::white); SecondP->setPos(230, -220);
Not sure how to get the random QPixmap over to my maintargets class to draw.
class MainTargets : public QGraphicsScene
QRectF boundingRect() const;
QPainterPath shape() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void advance(int step);
qreal dx, dy;
qreal x, y;
qreal w, h;
#endif // MAINTARGETS_H
dx = -0.005;
dy = 0.0;
x = 1.5;
y = 0.0;
w = 100.0;
h = 70.0;
QRectF MainTargets::boundingRect() const
qreal shift = 1;
return QRectF(-w/2 -shift, - h/2
- shift, w + shift, h + shift);
QPainterPath MainTargets::shape() const
void MainTargets::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
//NEED TO ADD IT HERE
painter->drawPixmap(-w/2, -h/2, main_targets[index]);
void MainTargets::advance(int step)
if(step == 0) return;
x = x + dx;
y = y + dy;
Any help would be appreciated.
I would probably have it stored in a new object. That just stores and returns random QPixmap. This way you can pass the object (by pointer or reference). This way you are even more flexible.
If you want to keep it in your display class You can add them to a public variable. This can be accessed in the same way you access functions. But if you make code changes this could render you program useless.
Thanks that's exactly what I ended up doing. I passed the object by reference.
That is a nice way. But in my normal c++ experince it's easier to pass objects as pointers. But you have to keep track of it all in your deconstrutor. And you don't have the risk of deleting your object when a other object is deconstructed. You will just delete the pointer in that case.
Would passing by pointers have any effect, good or bad, for collision detection? I'm having a real struggle with that.
With a pointer you don't "share" the object but you share the memory the object is in. So you always know you have the right object and the newest object.
Pointers and there effect are hard. I also struggle with them. But there is a good c++ "book online.":http://cppannotations.sourceforge.net/annotations/html/ Chapter 16. But there is more about pointers in this book.
I don't really know what you meant about collision but it's all in the same memory. So if you always want this object that's the way to go.