使用 QtGui 显示 QImage
我是 Qt 的新手,我正在尝试创建一个简单的 GUI 应用程序,该应用程序在单击按钮后显示图像.
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.
我可以在 QImage
对象中读取图像,但是有什么简单的方法可以调用 Qt 函数,将 QImage
作为输入并显示它?
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?
推荐答案
谢谢大家,我找到了方法,和 Dave 和 Sergey 一样:
Thanks All, I found how to do it, which is the same as Dave and Sergey:
我正在使用 QT Creator:
I am using QT Creator:
在主 GUI 窗口中使用拖放 GUI 创建并创建标签(例如myLabel")
In the main GUI window create using the drag drop GUI and create label (e.g. "myLabel")
在按钮(单击)的回调中,使用指向用户界面窗口的 (*ui) 指针执行以下操作:
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);
}
相关文章