[英]Display QImage with QtGui
I am new to Qt, and I am trying to create a simple GUI Application that displays an image once a button has been clicked on. 我是Qt的新手,我正在尝试创建一个简单的GUI应用程序,一旦点击按钮就会显示图像。
I can read the image in a QImage
object, but is there any simple way to call a Qt function that takes the QImage
as an input, and displays it? 我可以在QImage
对象中读取图像,但是有没有简单的方法来调用Qt函数将QImage
作为输入并显示它?
Simple, but complete example showing how to display QImage might look like this: 简单但完整的示例显示如何显示QImage可能如下所示:
#include <QtGui/QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QImage myImage;
myImage.load("test.png");
QLabel myLabel;
myLabel.setPixmap(QPixmap::fromImage(myImage));
myLabel.show();
return a.exec();
}
Drawing an image using a QLabel
seems like a bit of a kludge to me. 使用QLabel
绘制图像对我来说似乎有些麻烦。 With newer versions of Qt you can use a QGraphicsView
widget. 使用较新版本的Qt,您可以使用QGraphicsView
小部件。 In Qt Creator, drag a Graphics View
widget onto your UI and name it something (it is named mainImage
in the code below). 在Qt Creator中,将Graphics View
小部件拖到UI上并将其命名为mainImage
在下面的代码mainImage
其命名为mainImage
)。 In mainwindow.h
, add something like the following as private
variables to your MainWindow
class: 在mainwindow.h
,将以下内容作为private
变量添加到MainWindow
类:
QGraphicsScene *scene;
QPixmap image;
Then just edit mainwindow.cpp
and make the constructor something like this: 然后只需编辑mainwindow.cpp
并使构造函数如下:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
image.load("myimage.png");
scene = new QGraphicsScene(this);
scene->addPixmap(image);
scene->setSceneRect(image.rect());
ui->mainImage->setScene(scene);
}
One common way is to add the image to a QLabel
widget using QLabel::setPixmap()
, and then display the QLabel
as you would any other widget. 一种常见的方法是使用QLabel::setPixmap()
将图像添加到QLabel
小部件,然后像显示任何其他小部件一样显示QLabel
。 Example: 例:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPixmap pm("your-image.jpg");
QLabel lbl;
lbl.setPixmap(pm);
lbl.show();
return app.exec();
}
Thanks All, I found how to do it, which is the same as Dave and Sergey: 谢谢大家,我找到了怎么做,这与戴夫和谢尔盖一样:
I am using QT Creator: 我正在使用QT Creator:
In the main GUI window create using the drag drop GUI and create label (eg "myLabel") 在主GUI窗口中使用拖放GUI创建并创建标签(例如“myLabel”)
In the callback of the button (clicked) do the following using the (*ui) pointer to the user interface window: 在按钮的回调中(单击)使用指向用户界面窗口的(* ui)指针执行以下操作:
void MainWindow::on_pushButton_clicked()
{
QImage imageObject;
imageObject.load(imagePath);
ui->myLabel->setPixmap(QPixmap::fromImage(imageObject));
//OR use the other way by setting the Pixmap directly
QPixmap pixmapObject(imagePath");
ui->myLabel2->setPixmap(pixmapObject);
}
As far as I know, QPixmap
is used for displaying images and QImage
for reading them. 据我所知, QPixmap
用于显示图像和QImage
用于读取它们。 There are QPixmap::convertFromImage()
and QPixmap::fromImage()
functions to convert from QImage
. 有QPixmap::convertFromImage()
和QPixmap::fromImage()
函数可以从QImage
转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.