简体   繁体   中英

How to set an Image on QLabel from other form in Qt

Before taking up the main subject, please mind that i`ma beginner of Qt. I made a AddIm.cpp, and I want to set an image on QLabel in MainWindow.

here is my source in AddIm.cpp

void AddIm::on_pushButton_clicked()
{
    MainWindow mainwindow;
    mainwindow.setImage();
}

and here is MainWindow.cpp

void MainWindow::setImage()
{
    QPixmap pix("./test.jpg");
    ui->label->setPixmap(pix);
}

and MainWindow.h

class MainWindow : public QMainWindow
{
    public:
    void setImage();

    ~ some source ~

    private:
    Ui::MainWindow *ui;
};

it doesn't work at all. so I added a button in MainWindow for testing. and when it clicked, setImage works. but when I execute setImage in AddIm. it doesn't work. please let me know why

Your problem has nothing to do with your knowledge with Qt but rather your knowledge of c++.

In AddIm::on_pushButton_clicked() , you create a new MainWindow object on the stack, create the image, and then exit the function.

When a function exits, all local stack objects are destroyed. This means that your image is indeed being loaded but the window is being destroyed before you get a chance to see it. Even if it survived to live longer than the function allowed, you never show the window so it remains hidden.

UPDATE:

Change AddIm.cpp to be the following:

void AddIm::on_pushButton_clicked()
{
    MainWindow *mainwindow = new MainWindow;
    mainwindow->setAttribute(Qt::WA_DeleteOnClose, true);
    mainwindow->setImage();
    mainwindow->show();
}

You did not shown the window.

First, you have to create a C++ Class not a single .cpp file. Then add a pointer to the window in your AddIm.h file:

private:
MainWindow* mainwindow;

Then in your AddIm.cpp file:

mainwindow = new MainWindow(this);
mainwindow->setAttribute(Qt::WA_DeleteOnClose, true); // prevent memory leak when closing window
mainwindow->setImage();
mainwindow->show();

And remember to include MainWIndow in AddIm.h

#include "mainwindow.h"

Compare this with with your code to see where you might have gone wrong. I tried as much as possible to code it just like you did. I hardly use the designer, i like to code everything. It works as expected.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QLabel>
#include <QPixmap>

class MainWindow : public QWidget {
    Q_OBJECT

public:
    void setImage();

private:
    QLabel *label;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

void MainWindow::setImage() {
    QPixmap pix(":/test.jpg");

    label = new QLabel;
    label->setPixmap(pix);

    label->show();
}

addim.h

#ifndef ADDIM_H
#define ADDIM_H

#include <QMainWindow>
#include <QPushButton>
#include <QHBoxLayout>

#include "mainwindow.h"

class AddIm : public QMainWindow {
    Q_OBJECT
public:
    AddIm(QWidget *parent = 0);
    ~AddIm();

private slots:
    void on_pushButton_clicked();

private:
    QPushButton *button;
};

#endif // ADDIM_H

addim.cpp

#include "addim.h"

AddIm::AddIm(QWidget *parent) : QMainWindow(parent) {

    button = new QPushButton("Show Image");

    setCentralWidget(button);

    connect(button, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
}

void AddIm::on_pushButton_clicked() {
    MainWindow mainwindow;
    mainwindow.setImage();
}

AddIm::~AddIm() {

}

main.cpp

#include "addim.h"
#include <QApplication>

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    AddIm window;
    window.show();

    return a.exec();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM