C++ 如何将图像附加到使用 Qt Creator 创建的 gui
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17441239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to attach image to gui created by using Qt creator
提问by Medha Anand
How to put an image on the GUI created by Qt? The south west part of the screen is empty so I want a picture to be put there on a click of a button but I am unable to use QPixmap and setPixmap. Please help me with this !
如何在 Qt 创建的 GUI 上放置图像?屏幕的西南部分是空的,所以我想通过单击按钮将图片放在那里,但我无法使用 QPixmap 和 setPixmap。请在这件事上给予我帮助 !
回答by arslanekhan
Use QLabel in Qt Creator Then Go to Properties of QLabel Go to Pixmap Select the file
在 Qt Creator 中使用 QLabel 然后转到 QLabel 的属性 转到 Pixmap 选择文件
Its done.
完成。
回答by wrousseau
You can create a QWidget or a QFrame at the place you want to add an image. You can then use the style sheet to set a background-image on that picture. You might want to add your image to your ressources (.qrc).
您可以在要添加图像的位置创建 QWidget 或 QFrame。然后您可以使用样式表在该图片上设置背景图像。您可能希望将图像添加到资源 (.qrc) 中。
Using QtDesigner is a good idea for this kind of task.
对于此类任务,使用 QtDesigner 是一个好主意。
EDIT : Here is an easy way to do that without bothering with QPixMap.
编辑:这是一种简单的方法,无需打扰 QPixMap。
QWidget *frame = new QWidget(this);
frame->setGeometry(x, y, width, height);
frame->setStyleSheet("background-image: url(:/path/to/image.png)");
The :
specifies that you want to use the path in your Qt resources. The first two lines are not needed if you define this QWidget
using QtDesigner.
在:
指定要使用的路径在你的Qt资源。如果您QWidget
使用 QtDesigner定义它,则不需要前两行。
Also, don't forget to import your resources.qrc
(however you called it) file (including your resources) and add this to your .pro
:
另外,不要忘记导入您的resources.qrc
(无论您如何称呼它)文件(包括您的资源)并将其添加到您的.pro
:
RESOURCES = resources.qrc
回答by Pavel Strakhov
You should use a label (QLabel
). You can add a label to your form and edit its pixmap
property in Qt Designer (you will be able to choose one of images you've added to project resources). Also you can set an image on QLabel
programmatically using setPixmap()
.
您应该使用标签 ( QLabel
)。您可以向表单添加标签并pixmap
在 Qt Designer 中编辑其属性(您将能够选择已添加到项目资源的图像之一)。您也可以QLabel
使用setPixmap()
.