0

I am trying to upgrade my Seismic Graph Viewer to use tile graphics rendering. I am very unfamiliar with the process but my attempt to create a simple example is below. The reason why I need to use either QVector or QCache(I'm using QVector right now for simplicity) is to save ram and the tiles needed to be created on the fly. I'm not entirely sure if you're able to do what I'm trying to do below, but essentially it creates an array of bitmaps, makes them items, and then tries to add them to the scene. There are twelve errors when this program compiles, none of which directly refer the code that I made in the mainWindow.cpp.

the errors are either this

C:\Qt\Qt5.9.1\5.9.1\mingw53_32\include\QtCore\qvector.h:713: error: use of deleted function 'QGraphicsPixmapItem& QGraphicsPixmapItem::operator=(const QGraphicsPixmapItem&)' with the only thing changing being the location of the error (different header files)

or this

C:\Qt\Qt5.9.1\5.9.1\mingw53_32\include\QtWidgets\qgraphicsitem.h:861: error: 'QGraphicsPixmapItem::QGraphicsPixmapItem(const QGraphicsPixmapItem&)' is private Q_DISABLE_COPY(QGraphicsPixmapItem) which is in the qgraphicsitem.h header file

The code that produces doesn't compile due to these errors popping up in the header files

int count;
QVector<QBitmap> BitmapArrayTiles;
QVector<QGraphicsPixmapItem> PixmapItemsArray;
QGraphicsPixmapItem currentItem;
QBitmap currentBitmap;
QGraphicsScene *scene = new QGraphicsScene();

for(count = 0; count < 4; count++)
{
currentBitmap = QBitmap(150,150);
QPainter Painter(&currentBitmap);
QPen Pen(Qt::black); // just to be explicit
Painter.setPen(Pen);
drawStuff(Painter);
BitmapArrayTiles.insert(0, currentBitmap);
currentItem.setPixmap(BitmapArrayTiles[count]);
PixmapItemsArray.insert(count, currentItem);
scene->addItem(&currentItem);
currentItem.mapToScene((count*150)+150, (count*150)+150);
}
ui->TileView->setScene(scene);
            ^

I have not changed the header files manually so I'm not entirely sure why I would be getting these errors.

CrippledTable
  • 784
  • 5
  • 20
  • QObject is not copyable. The Qt4 way was to use pointers instead of objects. – drescherjm Aug 23 '17 at 21:20
  • https://stackoverflow.com/questions/19854371/repeating-q-disable-copy-in-qobject-derived-classes – drescherjm Aug 23 '17 at 21:23
  • https://stackoverflow.com/questions/2652504/how-do-i-copy-object-in-qt – drescherjm Aug 23 '17 at 21:23
  • https://stackoverflow.com/questions/19092513/copy-constructor-of-derived-qt-class – drescherjm Aug 23 '17 at 21:24
  • Do you mean pointers to the objects, that's what it says in the second link, just checking if that would be a viable option for what I am trying to do here – CrippledTable Aug 23 '17 at 21:42
  • I use pointers to Qt objects. Make sure you set the parent so that it get freed when the parent goes out of scope. – drescherjm Aug 23 '17 at 21:43
  • I've never used parents before, and I'm trying to read up on them right now. A little bit confused as to their implementation though. Is the parent the pointer I will be working with instead of copying, or is it just a pointer that is there to be deleted when I want all of it's children to be deleted as well. In which case I would need a different pointer to do the copying work around? – CrippledTable Aug 23 '17 at 22:04
  • This is a fundamental feature of Qt. When a parent QObject goes out of scope it frees all of its children. That is why all QObject constructors have a parent parameter. http://doc.qt.io/qt-5/qobject.html#QObject – drescherjm Aug 23 '17 at 22:14
  • Ya I read that as well, I'm just curious if there's anything else you do with the parent. edit nvm it makes sense now – CrippledTable Aug 23 '17 at 22:18
  • 1
    @drescherjm You are right that copying is disabled, but `QGraphicsItem` is not derived from `QObject`. However it has the same disable copy macros in the class definition. – thuga Aug 24 '17 at 09:26
  • I'm trying to implement pointers with the Qvectors now, but the image does not come out as intended. – CrippledTable Aug 24 '17 at 17:40

1 Answers1

0

Using pointers and tears

int count;
QGraphicsScene *scene = new QGraphicsScene(0, 0, 150*4, 150*4);
QVector<QBitmap*> BitmapArrayTiles;
QVector<QGraphicsPixmapItem*> BitmapItemsArray;
QGraphicsPixmapItem* CurrentItem;
QBitmap *CurrentBitmap;
const QBitmap* CurrentBitmapConstPointer = CurrentBitmap;

for(count = 0; count < 4; count++)
{
    CurrentBitmap = new QBitmap(150,150);
    QPainter Painter(CurrentBitmap);
    QPen Pen(Qt::black); // just to be explicit
    Painter.setPen(Pen);
    drawStuff(Painter);
    BitmapArrayTiles.insert(count, CurrentBitmap);
    CurrentItem = new QGraphicsPixmapItem(*BitmapArrayTiles[count]);
    BitmapItemsArray.insert(count, CurrentItem);
    //PixmapItemsArray.insert(count, currentItem);
    scene->addItem(BitmapItemsArray[count]);
    BitmapItemsArray[count]->setPos((count*150)+150, (count*150)+150);
}

ui->TileView->setScene(scene);
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
CrippledTable
  • 784
  • 5
  • 20
  • You don't have to store `QBitmap`s as pointers in your vector. Those can be automatic variables. – thuga Aug 28 '17 at 07:06