QGraphicsPixmapItem not showing when added to scene
-
My understanding is as follows:
I create a QGraphicsView, QGraphicsScene, and QPixmap.
The resource file allows me to customize the string for the file location. In this case, it's set to ":/piece/b_bishop.png" according to the documentation for resource files.
I create a QGraphicsPixmapItem using the QPixmap I created earlier.
I set the QGraphicsPixmapItem to the QGraphicsScene.
I set the scene to the view.
I show the view, but nothing happens.
Just to make sure, I changed the background color.Below, I have my code.
#include <QApplication> #include <QGraphicsScene> #include <QGraphicsView> #include <QGraphicsPixmapItem> #include <QPixmap> int main(int argc, char **argv) { QApplication app (argc, argv); QGraphicsView view; QGraphicsScene scene; scene.setBackgroundBrush(Qt::green); QPixmap map (":/piece/b_bishop.png"); QGraphicsPixmapItem bishop(map); scene.addItem(&bishop); view.setScene(&scene); view.show(); return app.exec(); }
<RCC> <qresource prefix="/piece"> <file>chesspieces/b_bishop.png</file> </qresource> </RCC>
The image, as shown in the QT Creator:
I've currently tried: adjusting the syntax in multiple ways. By everything I have seen, I have done this correctly. I must be missing something. Thanks for your help.
-
Shouldn't it be
/piece/hesspieces/b_bishop.png
?
Please check with QFile::exists() if your path is really correct. -
I took your advice, and tried these:
QFile file1(":/piece/chesspieces/b_bishop.png"); QFile file2(":/piece/b_bishop.png"); QFile file3(":/chesspieces/b_bishop.png"); if(file1.exists()) qDebug() << "file 1 exists"; else qDebug() << "file 1 does not exist"; if(file2.exists()) qDebug() << "file 2 exists"; else qDebug() << "file 2 does not exist"; if(file3.exists()) qDebug() << "file 3 exists"; else qDebug() << "file 3 does not exist";
None of them exist, assuming I wrote that code correctly to check lol.
Here is my file location:
Ignore the other files, i'm just using main to test this.
I'm creating the resource file with the QT Creator, and adding files to it the same way.
-
Did you instantiate a Q(Core)Application before your test?
-
Honestly I have no idea what that is, so probably not.
I just tried.
"QWidget: Cannot create a QWidget without QApplication"
-
absolute path works.
I would much prefer not having to use absolute paths, though
-
Hi,
Maybe a silly question but did you list your .qrc file in your .pro file ?
-
I'm real dumb, I figured it out. Your solution worked, just had to keep digging. I had mixed up file names apparently.