Question

EDIT

I am creating all objects initially when the program is started in my dialog.cpp and storing all QPixmaps in an array then picking a random one from them all. That random QPixmap I want to pass to my maintargets class and draw in the scene (which is also created in the dialog.cpp).

// dialog.cpp

#include "dialog.h"
#include "scene.h"
#include "ui_dialog.h"
#include "instructions.h"
#include "settings.h"
#include "highscore.h"
#include "maintargets.h"
#include <stdlib.h>
 
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
 
    // 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[0] = QPixmap(":images/darkbluelogo.jpg)");
     main_targets[1] = QPixmap(":images/graylogo.jpg");
     main_targets[2] = QPixmap(":images/lightbluelogo.jpg");
     main_targets[3] = QPixmap(":images/limE.jpg");
     main_targets[4] = QPixmap(":images/pink.jpg");
     main_targets[5] = QPixmap(":images/purple.jpg");
     main_targets[6] = QPixmap(":images/redlogo.jpg");
     main_targets[7] = QPixmap(":images/yellow.jpg");
     main_targets[8] = 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);
 
}
 
Dialog::~Dialog()
{
    delete ui;
}

//maintargets.h

#ifndef MAINTARGETS_H
#define MAINTARGETS_H
#include "dialog.h"
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QPainter>
#include <QRect>
 
class MainTargets : public QGraphicsScene
{
public:
    MainTargets();
    QRectF boundingRect() const;
    QPainterPath shape() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
 
protected:
    void advance(int step);
 
private:  
    qreal dx, dy;
    qreal x, y;
    qreal w, h;
     
};

 
#endif // MAINTARGETS_H

//maintargets.cpp

#include "maintargets.h"

MainTargets::MainTargets()
{
    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
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}

void MainTargets::paint(QPainter *painter,
                        const QStyleOptionGraphicsItem *option,
                        QWidget *widget)
{        
    painter->drawPixmap(-w/2, -h/2, main_targets[index]);
}

void MainTargets::advance(int step)
{
    if(step == 0) return;
    x = x + dx;
    y = y + dy;
    setPos(mapToParent(x, y));
}

After it is drawn it moves in x-direction.

Was it helpful?

Solution 2

Simple pass by reference was what I was lost on. This explains the process of what was needed to get the QPixmap variable in the maintargets class.

//dialog.h

private:
    Ui::Dialog *ui;
    //add a pointer
    MainTargets* pmap;

//dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include "maintargets.h"
#include <stdlib.h>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    // 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 = 1;
     main_targets[0] = QPixmap(":images/darkbluelogo.jpg)");
     main_targets[1] = QPixmap(":images/graylogo.jpg");
     main_targets[2] = QPixmap(":images/lightbluelogo.jpg");
     main_targets[3] = QPixmap(":images/lime.jpg");
     main_targets[4] = QPixmap(":images/pink.jpg");
     main_targets[5] = QPixmap(":images/purple.jpg");
     main_targets[6] = QPixmap(":images/redlogo.jpg");
     main_targets[7] = QPixmap(":images/yellow.jpg");
     main_targets[8] = QPixmap(":images/brown.jpg");



     //Timer for scene advancement
     QTimer *timer = new QTimer();
     QObject::connect(timer, SIGNAL(timeout()), scene, SLOT(advance()));
     timer->start(10);

}

Dialog::~Dialog()
{
    delete ui;
}


void Dialog::on_startButton_clicked()
{
    ui->settingsButton->hide();
    ui->titlescreen->hide();
    ui->highscoreButton->hide();
    ui->instructionButton->hide();
    ui->startButton->hide();

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

    //Initializes function
    setPixmaps();
}

void Dialog::setPixmaps()
{
    index = (qrand() % (9));
    //Add a new MainTarget and set equal to new pointer created in header file
    pmap = new MainTargets(main_targets[index]);
    pmap->setPos(355,0);
    scene->addItem(pmap);

}

//maintargets.h

private:   
    //Add a new QPixmap to header
    QPixmap p;

//maintargets.cpp

Reference in QPixmap variable in constructor and set equal to newly created pointer

#include "maintargets.h"

MainTargets::MainTargets(QPixmap& nomTargets)
{

    dx = -0.005;
    dy = 0.0;
    x = 0.0;
    y = 0.0;
    w = 100.0;
    h = 70.0;

    p = nomTargets;

}

QRectF MainTargets::boundingRect() const
{
    qreal shift = 1;
        return QRectF(-w/2 -shift, - h/2
                      - shift, w + shift, h + shift);
}

QPainterPath MainTargets::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}

void MainTargets::paint(QPainter *painter,
                        const QStyleOptionGraphicsItem *option,
                        QWidget *widget)
{
    //Set that pointer variable as the source for the 
    //given drawPixmap memeber

    painter->drawPixmap(-w/2, -h/2, p);

OTHER TIPS

Your question is very broad unfortunately, and the exact solution depends on your use case. I will mention a few different solutions for your issue, and then you can take your peek, but please read the documentation about how to ask questions on Stack Overflow because your question is very low-quality at the moment.

1) If your operation is supposed to build the other class, you can pass it as a constructor argument if it is not against your design for the construction of this class.

2) You can use a void setPixmap(QPixmap); setter if it is possible to extend your class this way, and you have access to an instance of the object in that method.

3) You can use a proxy class dealing with all this, if that is all you have access in your operation getting the random index.

4) You can set a static variable in the same source file if the other class needs this in the same source file. This is not a good idea in general, but I saw this happening, too.

5) You can set a global variable that the other class method is using. Again, this is a very bad practice.

6) You can just pass this QPixmap as an argument to the drawing function directly in the other class.

7) You can pass this to a proxy class object that will pass it to the drawing method of the other class.

8) If the other class is in a different process, you have get at least another many ways to pass it through.

As I said, it really depends on your scenario, and there is loads of ways to do it. That being said, I will remove this answer later because this question is too broad, but I wished to show you how broad what you are asking is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top