修改
我在 dialog.cpp 中启动程序时最初创建所有对象,并将所有QPixmaps存储在一个数组中,然后从中挑选一个随机的。那个随机的QPixmap我想传递给我的maintargets类并在场景中绘制(也是在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));
}
绘制后,它向x方向移动。
答案 0 :(得分:2)
遗憾的是,您的问题非常广泛,具体解决方案取决于您的使用案例。我将为您的问题提一些不同的解决方案,然后您可以看看,但请阅读有关如何在Stack Overflow上提问的文档,因为您的问题目前质量非常低。
1)如果你的操作应该构建另一个类,你可以将它作为构造函数参数传递,如果它不是你的设计构造这个类。
2)如果可以通过这种方式扩展您的类,您可以使用void setPixmap(QPixmap);
setter,并且您可以访问该方法中的对象实例。
3)你可以使用代理类处理所有这些,如果这是你在操作中获得随机索引的所有权限。
4)如果另一个类需要在同一个源文件中,则可以在同一个源文件中设置静态变量。这一般不是一个好主意,但我也看到了这种情况。
5)您可以设置其他类方法正在使用的全局变量。再次,这是一个非常糟糕的做法。
6)您可以直接将此QPixmap作为参数传递给另一个类中的绘图函数。
7)您可以将此传递给代理类对象,该代理类对象将其传递给另一个类的绘图方法。
8)如果另一个班级处于不同的过程中,你至少可以通过另外两种方式来传递它。
正如我所说,这实际上取决于您的情况,并且有很多方法可以做到这一点。话虽这么说,我稍后会删除这个答案,因为这个问题太宽泛了,但我希望告诉你你的要求有多宽。
答案 1 :(得分:0)
简单的参考传递是我迷失的。这解释了在maintargets类中获取QPixmap变量所需的过程。
// 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
在构造函数中的QPixmap变量中引用并设置为等于新创建的指针
#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);