简体   繁体   English

如何从Qt中的其他表单在QLabel上设置图像

[英]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. 在学习主要主题之前,请注意我是Qt的初学者。 I made a AddIm.cpp, and I want to set an image on QLabel in MainWindow. 我做了一个AddIm.cpp,我想在MainWindow的QLabel上设置一个图像。

here is my source in AddIm.cpp 这是我在AddIm.cpp中的来源

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

and here is MainWindow.cpp 这是MainWindow.cpp

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

and MainWindow.h 和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. 所以我在MainWindow中添加了一个按钮进行测试。 and when it clicked, setImage works. 单击时,setImage起作用。 but when I execute setImage in AddIm. 但是当我在AddIm中执行setImage时。 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++. 您的问题与您对Qt的了解无关,而与您对c ++的了解无关。

In AddIm::on_pushButton_clicked() , you create a new MainWindow object on the stack, create the image, and then exit the function. AddIm::on_pushButton_clicked() ,在堆栈上创建一个新的MainWindow对象,创建图像,然后退出该函数。

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: AddIm.cpp更改为以下内容:

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. 首先,您必须创建一个C ++类,而不是一个.cpp文件。 Then add a pointer to the window in your AddIm.h file: 然后在您的AddIm.h文件中添加一个指向窗口的指针:

private:
MainWindow* mainwindow;

Then in your AddIm.cpp file: 然后在您的AddIm.cpp文件中:

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 并记住在AddIm.h中包含MainWIndow

#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 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 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 main.cpp

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

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

    AddIm window;
    window.show();

    return a.exec();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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