C++ Qt未在QGraphicsView上显示PNG
请注意:我是Qt新手 目的: 我试图在UI窗体上的GraphicsView对象上显示PNG 已尝试: 我一直在关注一个,从这篇文章,为显示PNG,但没有运气 我的PNG不会显示在我的UI上的GraphicsView对象上。我的图形视图大小是(w 121,h 111),png大小是(w 100,h 100) 标题/主窗口.hC++ Qt未在QGraphicsView上显示PNG,c++,qt,qgraphicsview,qgraphicsscene,qpixmap,C++,Qt,Qgraphicsview,Qgraphicsscene,Qpixmap,请注意:我是Qt新手 目的: 我试图在UI窗体上的GraphicsView对象上显示PNG 已尝试: 我一直在关注一个,从这篇文章,为显示PNG,但没有运气 我的PNG不会显示在我的UI上的GraphicsView对象上。我的图形视图大小是(w 121,h 111),png大小是(w 100,h 100) 标题/主窗口.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <Qt
#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
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>
因此,请参考以下图像位置:
QPixmap pixmap("/home/cx/qt-projects/VPN/back.png");
但事实并非如此,为什么
QPixmap pixmap("back.png");
文件系统中是否存在“back.png”?您声明“PNG位置,根目录”,但使用的路径是相对的(我假设您不是从根目录运行)。@G.m.谢谢,我刚刚注意到这一点,实施了更改,但没有解决此问题。所以在其他地方也一定有错误…我不认为您修复了文件位置。请尝试文件的完整路径,以确保此处没有错误。如何构建项目?PNG是通过插件加载的:它们安装在您执行代码的地方吗?如果您想使用
QPixmap pixmap(“back.PNG”)
您需要将back.png
放到您的工作目录中查看项目=>运行=>工作目录:
它可以不同于项目根目录。文件系统中是否存在“back.png”?您声明“PNG位置,根目录”,但使用的路径是相对的(我假设您不是从根目录运行)。@G.m.谢谢,我刚刚注意到这一点,实施了更改,但没有解决此问题。所以在其他地方也一定有错误…我不认为您修复了文件位置。请尝试文件的完整路径,以确保此处没有错误。如何构建项目?PNG是通过插件加载的:它们安装在您执行代码的地方吗?如果您想使用QPixmap pixmap(“back.PNG”)
您需要将放回.png
到您的工作目录中,在Projects=>Run=>工作目录中查找:
它可以不同于项目根目录。
$ 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");