問題描述
我是 Qt 的新手,我正在嘗試創(chuàng)建一個簡單的 GUI 應(yīng)用程序,該應(yīng)用程序在單擊按鈕后顯示圖像.
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
對象中讀取圖像,但是有什么簡單的方法可以調(diào)用 Qt 函數(shù),將 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 創(chuàng)建并創(chuàng)建標(biāo)簽(例如myLabel")
In the main GUI window create using the drag drop GUI and create label (e.g. "myLabel")
在按鈕(單擊)的回調(diào)中,使用指向用戶界面窗口的 (*ui) 指針執(zhí)行以下操作:
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);
}
這篇關(guān)于使用 QtGui 顯示 QImage的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!