1

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.

Community
  • 1
  • 1
user3353931
  • 47
  • 2
  • 8
  • Isn't it just main_targets[index]? – László Papp Mar 14 '14 at 02:43
  • Are you saying main_targets[index] for picking random QPixmap? – user3353931 Mar 14 '14 at 02:44
  • Yes, you wrote `I want to pass the random QPixmap to another class`, so I would assume you simply pass that to another class through a constructor, getter or other means. What is wrong about it? – László Papp Mar 14 '14 at 02:49
  • Can you show some code of passing arguemnts like what I'm trying to do? – user3353931 Mar 15 '14 at 02:30
  • It is not possible without providing more information. Do you have access to a maintargets object in dialog.cpp where you pick up a random image, for instance? – László Papp Mar 15 '14 at 07:10
  • I am voting for closure with the reason of too broad until further information is provided. I am happy to help once you explain what the responsibilities are between the files and objects in your design. It is not possible to narrow the list down without knowing those design decisions. – László Papp Mar 15 '14 at 20:05
  • Think we are on differet pages. The dialog.cpp file creates the array and generates the random QPixmap to push to the maintargets class. I am having the problem of how to store that object and have it available for the maintargets class to use to actually do anything to it. – user3353931 Mar 15 '14 at 20:16
  • That was understood the first time, too, but that does not answer the questions for being able to narrow the list down at all. If you do not want to answer those because of whatever reason, fine, take the list and your peek, but if you wish to get a more accurate answer, you have to answer those questions. – László Papp Mar 15 '14 at 20:19
  • I have access to the maintargets object in dialog.cpp because it is created there...The maintargets class is designed to draw the pixmap and have it move across the scene, but since the creation is in a different class, maintargets doesn't have access to it...hence the original question. What exactly am I not answering? – user3353931 Mar 15 '14 at 20:25
  • What is calling the paint method? Is that the dialog or some other files? Either way, in worst case scenario, you could write a void setPixmap(QPixmap pixmap); method in there, and call that from the dialog.cpp with the random stuff. – László Papp Mar 15 '14 at 20:30
  • The paint gets drawn in the local coordinates so it's called in the maintargets and advnaced by the dialog. Guess what is confusing originally the same code works with the QPixmap being created in the maintargets class and then them added in the dialog class. Now I'm just reversing the process and figured it would be just as simple. If I were to call the random pixmap from dialog it wouldn't be associated with the scenes local coordinates that are designed in the maintargets class. – user3353931 Mar 15 '14 at 20:48
  • Just add a pixmap parameter for the the paint or create a setPixmap mutator. – László Papp Mar 15 '14 at 22:16

2 Answers2

2

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.

László Papp
  • 51,870
  • 39
  • 111
  • 135
0

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);
user3353931
  • 47
  • 2
  • 8