使用 QtGui 显示 QImage

发布于 2024-10-08 04:00:25 字数 116 浏览 0 评论 0原文

我是 Qt 新手,我正在尝试创建一个简单的 GUI 应用程序,单击按钮后会显示图像。

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月蹉跎了容颜 2024-10-15 04:00:25

显示如何显示 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();
}

Simple, but complete example showing how to display QImage might look like this:

#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();
}
与他有关 2024-10-15 04:00:25

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

QGraphicsScene *scene;
QPixmap image;

然后只需编辑 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);
}

Drawing an image using a QLabel seems like a bit of a kludge to me. With newer versions of Qt you can use a QGraphicsView widget. In Qt Creator, drag a Graphics View widget onto your UI and name it something (it is named mainImage in the code below). In mainwindow.h, add something like the following as private variables to your MainWindow class:

QGraphicsScene *scene;
QPixmap image;

Then just edit mainwindow.cpp and make the constructor something like this:

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);
}
2024-10-15 04:00:25

一种常见的方法是使用 QLabel::setPixmap() 将图像添加到 QLabel 小部件,然后像您希望的那样显示 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();
}

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. 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();
}
明媚如初 2024-10-15 04:00:25

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

我正在使用 QT Creator:

在主 GUI 窗口中使用拖放 GUI 创建并创建标签(例如“myLabel”)

在回调中按钮(单击)使用指向用户界面窗口的 (*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);
}

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

I am using QT Creator:

In the main GUI window create using the drag drop GUI and create label (e.g. "myLabel")

In the callback of the button (clicked) do the following using the (*ui) pointer to the user interface window:

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);
}
满身野味 2024-10-15 04:00:25

据我所知,QPixmap 用于显示图像,QImage 用于读取图像。有 QPixmap::convertFromImage()QPixmap::fromImage() 函数可以从 QImage 进行转换。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文