C++ 使用 QtGui 显示 QImage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4474086/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:29:34  来源:igfitidea点击:

Display QImage with QtGui

c++qtqimageqtgui

提问by Karim

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 QImageobject, but is there any simple way to call a Qt function that takes the QImageas an input, and displays it?

我可以读取QImage对象中的图像,但是有什么简单的方法可以调用 Qt 函数,该函数以QImage为输入并显示它?

采纳答案by Karim

Thanks All, I found how to do it, which is the same as Dave and Sergey:

谢谢大家,我找到了如何去做,这与 Dave 和 Sergey 相同:

I am using QT Creator:

我正在使用 QT Creator:

In the main GUI window create using the drag drop GUI and create label (e.g. "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);
}

回答by Palmik

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();
}

回答by Scott Griffiths

Drawing an image using a QLabelseems like a bit of a kludge to me. With newer versions of Qt you can use a QGraphicsViewwidget. In Qt Creator, drag a Graphics Viewwidget onto your UI and name it something (it is named mainImagein the code below). In mainwindow.h, add something like the following as privatevariables to your MainWindowclass:

使用 a 绘制图像QLabel对我来说似乎有点麻烦。使用较新版本的 Qt,您可以使用QGraphicsView小部件。在 Qt Creator 中,将一个Graphics View小部件拖到您的 UI 上并为其命名(mainImage在下面的代码中命名)。在 中mainwindow.h,将类似以下内容作为private变量添加到您的MainWindow类中:

QGraphicsScene *scene;
QPixmap image;

Then just edit mainwindow.cppand 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);
}

回答by Dave Mateer

One common way is to add the image to a QLabelwidget using QLabel::setPixmap(), and then display the QLabelas you would any other widget. Example:

一种常见的方法是使用 将图像添加到QLabel小部件QLabel::setPixmap(),然后像显示QLabel任何其他小部件一样显示 。例子:

#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();
}

回答by Sergei Tachenov

As far as I know, QPixmapis used for displaying images and QImagefor reading them. There are QPixmap::convertFromImage()and QPixmap::fromImage()functions to convert from QImage.

据我所知,QPixmap用于显示图像和QImage阅读它们。有QPixmap::convertFromImage()QPixmap::fromImage()函数可以从QImage.