I have a custom QGraphicsScene where I reimplement the drawbackground function and draw using opengl. I am rendering everything as expected in drawbackground. However when I add a QGraphicsItem to the scene I seem to be having difficulty getting the items to render appropriately. I am able to render a QGraphicsSimpleTextItem fine, however not so much with a QGraphicsPixmapItem. If I try to just add the QGraphicsPixmapItem nothing appears on the screen, however if I add both the QGraphicsSimpleTextItem and QGraphicsPixmapItem both render fine. I have no idea why this is happening, so I am hoping by taking a look at my main function anyone might be able to steer me straight. This is just a small program designed to teach myself how to properly add items to a QGraphicsScene.
#include "glgraphicsscene.h"
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsSimpleTextItem>
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QDebug>
int main(int argc, char *argv[])
{
GLGraphicsScene* glScene = new GLGraphicsScene;
textItem.setBrush(Qt::black);
qDebug() << pixmap.load("../Bounce/graphicsview-pixmapitem.png");
qDebug() << pixmap.isNull();
qDebug() << pixmap.width();
qDebug() << pixmap.height();
qDebug() << pixmap.hasAlpha();
gpixmap->setVisible(true);
// this works fine all the time
glScene->addItem(&textItem);
// this won't render unless line above is done first
glScene->addItem(gpixmap);
// just to find out if items are being added for my benefit
for(int i = 0; i < glScene->items().size(); i++)
{
qDebug() << glScene->items().at(i);
}
view.setScene(glScene);
view.show();
app.exec();
}
#include "glgraphicsscene.h"
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsSimpleTextItem>
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
GLGraphicsScene* glScene = new GLGraphicsScene;
QGraphicsSimpleTextItem textItem("Hello QGraphicsItem!");
textItem.setBrush(Qt::black);
QPixmap pixmap;
qDebug() << pixmap.load("../Bounce/graphicsview-pixmapitem.png");
qDebug() << pixmap.isNull();
qDebug() << pixmap.width();
qDebug() << pixmap.height();
qDebug() << pixmap.hasAlpha();
QGraphicsPixmapItem* gpixmap = new QGraphicsPixmapItem(pixmap);
gpixmap->setVisible(true);
// this works fine all the time
glScene->addItem(&textItem);
// this won't render unless line above is done first
glScene->addItem(gpixmap);
// just to find out if items are being added for my benefit
for(int i = 0; i < glScene->items().size(); i++)
{
qDebug() << glScene->items().at(i);
}
QGraphicsView view;
view.setViewport(new QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::Rgba)));
view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
view.setScene(glScene);
view.show();
app.exec();
}
To copy to clipboard, switch view to plain text mode
Any ideas, I haven't found anything on the internet to tell me why this won't work. Any help is greatly appreciated!
Bookmarks