Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. How can I pass QPixmap to different class

How can I pass QPixmap to different class

Scheduled Pinned Locked Moved Game Development
6 Posts 2 Posters 2.1k Views
  • 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.
  • D Offline
    D Offline
    DSlim
    wrote on last edited by
    #1

    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.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;
    }

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

    }

    void Dialog::on_instructionButton_clicked()
    {
    Instructions intDialog;
    intDialog.setModal(true);
    intDialog.exec();
    }

    void Dialog::on_settingsButton_clicked()
    {
    settings intDialog;
    intDialog.setModal(true);
    intDialog.exec();
    }

    void Dialog::on_highscoreButton_clicked()
    {
    highscore intDialog;
    intDialog.setModal(true);
    intDialog.exec();
    }@

    Not sure how to get the random QPixmap over to my maintargets class to draw.

    //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
    {
    //return QRectF(0,0,45,90);
    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)
    {
    //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;
    setPos(mapToParent(x, y));
    }
    @

    Any help would be appreciated.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DDH87
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DSlim
        wrote on last edited by
        #3

        Thanks that's exactly what I ended up doing. I passed the object by reference.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DDH87
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DSlim
            wrote on last edited by
            #5

            Would passing by pointers have any effect, good or bad, for collision detection? I'm having a real struggle with that.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DDH87
              wrote on last edited by
              #6

              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.

              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