Qt不在QGraphicsView上显示PNG

时间:2016-10-05 13:41:20

标签: c++ qt qgraphicsview qgraphicsscene qpixmap

请注意:我是Qt新手。

目的: 我试图在UI表单上的GraphicsView对象上显示PNG

尝试: 我一直在关注simple solution,在这篇文章中显示PNG,但没有运气。

我的PNG不会显示在我的UI上的GraphicsView对象上。我的GraphicsView大小是(w 121,h 111),我的png大小是(w 100,h 100)。

页眉/ 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

源/ main.cpp中

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

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

    return a.exec();
}

源/ 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;
}

表单/ 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位置,根目录

我错过了什么?

更新

注意1:修复了png位置错误,但没有解决问题。必须是别的东西。

澄清根目录

项目位于VPN目录

$ 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

因此,参考下面的图像位置工作:

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

但这不是,为什么?

QPixmap pixmap("back.png");

0 个答案:

没有答案