Anzeige QImage mit QtGui
Frage
Ich bin neu in Qt, und ich versuche, eine einfache GUI-Anwendung, die zeigt ein Bild einmal eine Schaltfläche angeklickt wurde, zu erstellen.
ich das Bild in einem QImage
Objekt lesen kann, aber gibt es eine einfache Möglichkeit, eine Qt-Funktion aufrufen, die die QImage
als Eingabe verwendet, und zeigt es?
Lösung 4
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);
}
Andere Tipps
Einfach, aber vollständiges Beispiel zeigt, wie QImage angezeigt werden könnte wie folgt aussehen:
#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();
}
ein Bild Zeichnen eines QLabel
mit scheint ein bisschen ein Flickschusterei zu mir. Bei neueren Versionen von Qt können Sie einen QGraphicsView
Widget verwenden. In Qt Creator, ziehen Sie ein Graphics View
Widget auf Ihre UI und nennen Sie es etwas (es unter mainImage
im Code genannt wird). In mainwindow.h
, so etwas wie die folgenden als private
Variablen zu Ihrer MainWindow
Klasse hinzufügen:
QGraphicsScene *scene;
QPixmap image;
Dann einfach bearbeiten mainwindow.cpp
und machen den Konstruktor so etwas wie folgt aus:
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. 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();
}
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
.