Working with Images in my Mainwindow



  • Hello everyone!

    I'm new to Qt and I want to add a image to my Mainwindow and I don't know how.

    Through the main of my project I can add a image to a new form, but I don't know how to add this image to my Mainwindow.

    To add the image to a new form i'm using the QLabel lib.

    main.cpp:
    @#include "mainwindow.h"
    #include <QApplication>
    #include <QLabel>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QLabel label("<img src='/home/gbrennon/Downloads/duto.jpg' />");
    label.show();
    MainWindow w;
    w.show();

    return a.exec(&#41;;
    

    }
    @

    How can I add the image using some kind of widget?

    Is there any possible RAD stuff to add the image?

    Thanks

    [Edit: Added @ tags around code -- mlong]



  • [quote author="gbrennon" date="1361208082"]
    How can I add the image using some kind of widget?

    Is there any possible RAD stuff to add the image?[/quote]

    The easiest way I can think of is by using the "Designer" feature of Qt Creator:

    you need to add (drag and drop) a label to you mainwindow then arrange it by Layout (if you need)

    right click into it then find "Edit Rich Text", IIRC

    from there, you can find an icon for adding image.

    *Note: The same steps can also be done with "Text Edit"

    The problem with this is that the image cannot be easily manipulated (size remains the same). I prefer using QPainter + QWidget + QImage as it gives me more control, but it can only be done programmatically.



  • The easiest solution may be:
    [code]
    #include "mainwindow.h"
    #include <QApplication>
    #include <QLabel>

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QLabel label;
        label.setPixmap(QPixmap("/home/gbrennon/Downloads/duto.jpg"));
        label.show();
        MainWindow w;
        w.show();
     
       
        return a.exec&#40;&#41;;
    }
    

    [/code]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.