Qt World Summit: Register Today!

Passing QPixmap Object Between Main and Sub Forms

  • I saw some samples but connect() mechanism looked weird a bit to me. I'm trying to understand it.

    Currently I have a QLabel in MainWindow, and it has a QPixmap (covered with image).

    Let's say there is a button and it has button_clicked() event. When button clicked, it should pass MainWindow's QLabel to sub form. So i need to pass this QLabel(as image) to another form and show it in another form's QLabel (as image).

    I think it's a good way to use connect(), SLOT & SIGNAL mechanism, like i said i'm trying to understand it. Or maybe there's a better way to do pass QLabel to another form. I'm waiting for your helpful and explanatory answers.

  • Lifetime Qt Champion


    Something is not clear, do you want to move the QLabel around or the image it contains ? If the later, where does that image come from ?

  • @SGaist second one, the image comes from a database (blob data).

    At Main Form:

    //strQuery contains how i get blob data from a local Database
    QByteArray outByteArray = strQuery.value(0).toByteArray();
    QPixmap outPixmap = QPixmap();
    outPixmap.loadFromData( outByteArray );
    QPixmap pixmap = outPixmap;
    QImage image;
    image = image.scaledToWidth(ui->QLabel1->width(), Qt::SmoothTransformation);
    //until here, i loaded blob data to a QLabel

    The QLabel shows blob data, it's OK. Now i want to show same blob data in other form. Yes i can do with the same way, get from db again but that's not way i want.

    I just need to show QLabel's image in sub form. Did i explain well?

  • Lifetime Qt Champion

    So the sub form should also have a label to hold image (pixmap) as
    moving mainwindows actual label will be odd.
    Lets call it subforms label for sublabel.
    so you add a function to subform for setting it
    ( as the UI is private from outside)
    void SubFormName::setImage(QPixmap pix) {

    and then when in Main form, where/when u show sub form
    SubForm * mysub=new SubForm(this);
    // we call our function to transfer image to sub form
    mysub->setImage( ui->QLabel1->pixmap() );

    note you need to delete subform again at some point!
    using a QDialog as subForm would make this easier if
    its ok, that mainwindow is blocked while sub form is open.

  • @mrjj when i do

    mysub->setImage( ui->QLabel1->pixmap() );

    ui keyword is underlined and this error:

    no viable conversion from const QPixmap * to QPixmap

    I solved it with create a new const QPixmap *;

    const QPixmap *pixMap = ui->QLabel1->pixmap();
    mysub->setImage( ui->QLabel1->pixmap() );

    It's ok now. Many thanks @Lifetime-Qt-Champion

  • Lifetime Qt Champion

    Ah sorry, my bad.
    I thought it would return a copy.
    but its
    const QPixmap * pixmap() const
    Should be fine as a copy is made so even if original image should
    be removed, the subForm has its own.

Log in to reply