Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image QT标签:使用图像更新标签_Image_Qt_Labels - Fatal编程技术网

Image QT标签:使用图像更新标签

Image QT标签:使用图像更新标签,image,qt,labels,Image,Qt,Labels,您好,我正在使用QT将图像加载到GUI。 我可以轻松地将一个文件加载到一个标签中。 当我想将新图像加载到同一个标签中时,就会出现问题。 本质上,我想更新这个标签。 我试图通过使用SetPixmap作为插槽函数按下一个按钮来实现这一点。 然而,使用SetPixmap的直接调用是有效的,但是当它在插槽中时,它对我不起作用 #include "mainwindow.h" #include "ui_mainwindow.h" #include <iostream> #include <

您好,我正在使用QT将图像加载到GUI。 我可以轻松地将一个文件加载到一个标签中。 当我想将新图像加载到同一个标签中时,就会出现问题。 本质上,我想更新这个标签。 我试图通过使用SetPixmap作为插槽函数按下一个按钮来实现这一点。 然而,使用SetPixmap的直接调用是有效的,但是当它在插槽中时,它对我不起作用

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <qdebug>
#include <QLabel>
#include <QPixmap>
#include <QTcore>

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QImage Hellkite_Tyrant;
    Hellkite_Tyrant.load(":/MtGimages/HT.jpg");

    QImage Island;
    Island.load(":/MtGimages/Island.jpg");

    if(Hellkite_Tyrant.isNull())
    {
        ui->textlabel->setText("Did not work");
    }
    //ui->myLabel->setPixmap(QPixmap::fromImage(Hellkite_Tyrant));
    ui->myLabel->hide();



    connect(ui->pushButton,SIGNAL(clicked()),
            ui->myLabel,SLOT(setPixmap(QPixmap::fromImage(Hellkite_Tyrant))));

    connect(ui->pushButton,SIGNAL(clicked()),
            ui->myLabel,SLOT(show()));

   // connect(ui->pushButton_2,SIGNAL(clicked()),
    //        ui->myLabel,SLOT(setPixmap(QPixmap::fromImage(Island))));

   // connect(ui->pushButton_2,SIGNAL(clicked()),
   //         ui->myLabel,SLOT(repaint()));

   // connect(ui->pushButton_2,SIGNAL(clicked()),
   //         ui->myLabel,SLOT(show()));
}

MainWindow::~MainWindow()
{
    delete ui;
}
#包括“mainwindow.h”
#包括“ui_main window.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
他是一个暴君;
Hellkite_Tyrant.load(“:/MtGimages/HT.jpg”);
齐马格岛;
Island.load(“:/MtGimages/Island.jpg”);
if(Hellkite_Tyrant.isNull())
{
用户界面->文本标签->设置文本(“不工作”);
}
//ui->myLabel->setPixmap(QPixmap::fromImage(Hellkite_Tyrant));
ui->myLabel->hide();
连接(用户界面->按钮,信号(点击()),
ui->myLabel,SLOT(setPixmap(QPixmap::frommage(Hellkite_Tyrant));
连接(用户界面->按钮,信号(点击()),
ui->myLabel,SLOT(show());
//连接(用户界面->按钮2,信号(点击()),
//ui->myLabel,SLOT(setPixmap(QPixmap::frommage(Island));
//连接(用户界面->按钮2,信号(点击()),
//ui->myLabel,SLOT(repaint());
//连接(用户界面->按钮2,信号(点击()),
//ui->myLabel,SLOT(show());
}
MainWindow::~MainWindow()
{
删除用户界面;
}

信号的签名必须与接收插槽的签名匹配。 因此,这段代码中存在一些错误

connect(ui->pushButton,SIGNAL(clicked()),
        ui->myLabel,SLOT(setPixmap(QPixmap::fromImage(Hellkite_Tyrant))));
你应该这样做

// mainwindow.h

class MainWindow: public QMainWindow
{
...

protected slots:
    void on_pushButton_clicked();

...
};

// mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
...

    connect(ui->pushButton,SIGNAL(clicked()),
            this, SLOT(on_pushButton_clicked()));

}

void MainWindow::on_pushButton_clicked()
{
    ui->myLabel->setPixmap(QPixmap::fromImage(Hellkite_Tyrant));
    ui->myLabel->show();
}