1

Please note: I am a Qt newbie.

Purpose: I am attempting to display a PNG on a GraphicsView object on a UI form

Tried: I have been following a simple solution, from this post, for displaying a PNG, but have had no luck.

My PNG does not display onto a GraphicsView object on my UI. My GraphicsView size is (w 121,h 111) and my png size is (w 100,h 100).

Headers/mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtCore>
#include <QtGui>
#include <QtWidgets>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QGraphicsView *view;
    QGraphicsScene *scene;
};

#endif // MAINWINDOW_H

Sources/main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

Sources/mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    view = ui->graphicsView;
    scene = new QGraphicsScene(view);
    view->setScene(scene);
    QPixmap pixmap("back.png");
    scene->addPixmap(pixmap);
    view->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}

Forms/mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>283</width>
    <height>415</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>130</y>
      <width>141</width>
      <height>22</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_2">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>170</y>
      <width>141</width>
      <height>22</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>250</y>
      <width>91</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>Login</string>
    </property>
   </widget>
   <widget class="QCheckBox" name="checkBox">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>220</y>
      <width>101</width>
      <height>20</height>
     </rect>
    </property>
    <property name="text">
     <string>Remeber Me</string>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>130</y>
      <width>59</width>
      <height>14</height>
     </rect>
    </property>
    <property name="text">
     <string>Email</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>170</y>
      <width>59</width>
      <height>14</height>
     </rect>
    </property>
    <property name="text">
     <string>Password</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>96</x>
      <y>310</y>
      <width>80</width>
      <height>22</height>
     </rect>
    </property>
    <property name="text">
     <string>Register</string>
    </property>
   </widget>
   <widget class="QGraphicsView" name="graphicsView">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>0</y>
      <width>121</width>
      <height>111</height>
     </rect>
    </property>
    <property name="autoFillBackground">
     <bool>false</bool>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>283</width>
     <height>19</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

PNG location, root directory

What am I missing?

UPDATE

note 1: fixed png location error, but that did not solve the issue. Must be something else.

to clarify root directory

Project is in VPN directory

$ ll /home/cx/qt-projects/VPN/

total 48
drwxrwxr-x 1 cx cx   162 Oct  5 15:45 ./
drwxr-xr-x 1 cx cx    90 Oct  5 12:21 ../
-rw-rw-r-- 1 cx cx  1514 Oct  5 15:33 back.png
drwxrwxr-x 1 cx cx   108 Oct  5 12:12 .git/
-rw-rw-r-- 1 cx cx   172 Oct  5 10:41 main.cpp
-rw-rw-r-- 1 cx cx   393 Oct  5 15:45 mainwindow.cpp
-rw-rw-r-- 1 cx cx   399 Oct  5 15:25 mainwindow.h
-rw-rw-r-- 1 cx cx  3134 Oct  5 12:21 mainwindow.ui
drwxrwxr-x 1 cx cx     8 Oct  5 12:11 res/
-rw-rw-r-- 1 cx cx   400 Oct  5 12:12 VPN.pro
-rw-rw-r-- 1 cx cx 24064 Oct  5 12:35 VPN.pro.user

Thus refering to image location below works:

QPixmap pixmap("/home/cx/qt-projects/VPN/back.png");

but this does not, why?

QPixmap pixmap("back.png");
Community
  • 1
  • 1
CybeX
  • 2,060
  • 3
  • 48
  • 115
  • Does "back.png" exist in the file system? You state "PNG location, root directory" but the path being used is relative (and I'm assuming you're not running from the root directory). – G.M. Oct 05 '16 at 13:46
  • @G.M. THanks, I just noticed this now, implemented the change, but did not fix this issue. So there must be an error somewhere else too... – CybeX Oct 05 '16 at 13:49
  • I don't think you fix file location. Try **full** path to file to be sure that no error here. – Konstantin T. Oct 05 '16 at 13:50
  • How do you build your project? PNG are loaded through plugins: are they installed where you're executing your code? – rocambille Oct 05 '16 at 13:51
  • @KonstantinT. I will try full path, see if this helps, although it is cause for concern in later projects if only full path works – CybeX Oct 05 '16 at 13:52
  • Do you really mean "root" directory (as in "/") -- or are you referring to your project's root directory? – G.M. Oct 05 '16 at 13:54
  • @wasthishelpful please clarify, what do you mean by "how do you build your project" and where can I check for said PNG plugins? – CybeX Oct 05 '16 at 13:54
  • @G.M. I mean project root, see update – CybeX Oct 05 '16 at 13:55
  • @KonstantinT. I jsut attempted full path, it works, am I refering to project root incorrectly? – CybeX Oct 05 '16 at 13:56
  • 1
    If you want use `QPixmap pixmap("back.png");` you need put `back.png` to your **working directory** look in `Projects=>Run=>Working directory:` it can differ from *Project root* directory. – Konstantin T. Oct 05 '16 at 14:08
  • @KonstantinT. oh, my workign directory is /home/cx/qt-projects/build-VPN-Desktop_Qt_5_7_0_GCC_64bit-Debug. Interesting, thanks for the help! I am looking into using the resource system to store my png rather – CybeX Oct 05 '16 at 14:18
  • Using resources is safer as long as you do not have to change back.png often because in that case you would have to recompile binary – Marco Oct 05 '16 at 14:37

0 Answers0