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");