PDA

View Full Version : How to Load Image File into Desiger Created Qlabel?



Not Fast Enough
13th April 2010, 13:16
Hello all,

I'm in the process of learning c++ and read Thinking in c++, its lovely book. I ordered a book called C++ GUI Programming with Qt 4 (2nd Edition), but it wont arrive until next week. I use a mac and i wanted to develop a small program that would work on windows and mac and my professor recommended QT Framework for me. any way, please forgive me for my extremely newbie question: I have Created an interface in QT to load an image and display it in a Qlabel, and i was able to load the image in a label that pops up and not in the Qlabel that i already created in the designer view which already hold the same name:



void MainWindow::on_btnLoadImage_clicked()
{

QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty()) {
QImage image(fileName);
if (image.isNull()) {
QMessageBox::information(this, tr("Image Viewer"),
tr("Cannot load %1.").arg(fileName));
return;
}
imageLabel = new QLabel;
imageLabel->setPixmap(QPixmap::fromImage(image));
imageLabel->show();
}

}

I didn't get the idea of slots and signals and still understanding how to create a class and call its member functions, every book talks about apples, oranges, cars, wheels, engines, shapes, circles, squares and stuff like that and its still not making sense to me.

please advice me on how to update different Qlabels -that i already put in the designer view- with images and text.

thanks for the help in advance

JohannesMunk
13th April 2010, 22:36
Hi!

How to access the 'designed' widgets depends on what you set up in the form-creation wizard (Advanced .. Multiple Inheritance, vs private member, ...)

Probably its: ui->objectname. .. Where objectname is the name you provided in designer.

Look at these free books:

http://www.qtrac.eu/marksummerfield.html => http://www.qtrac.eu/C++-GUI-Programm...t-4-1st-ed.zip (http://www.qtrac.eu/C++-GUI-Programming-with-Qt-4-1st-ed.zip)
The DesignerIntegration in Creator makes the UI stuff easier, than this first edition describes.

http://cartan.cas.suffolk.edu/oopdocbook/opensource/index.html

Good fun with Qt! It's brilliant!

Johannes

Not Fast Enough
14th April 2010, 07:39
Thanks Johannes :cool:, Your reply was extremely helpful.
I used ui-> then i got the names of the objects in my designer view like that:


ui->imageLabel->setPixmap(QPixmap::fromImage(image));

Regards,