2013-07-19 67 views
0

您好,我正在使用QT將圖像加載到GUI。 我可以輕鬆地將一個文件加載到一個標籤中。 當我想將新圖像加載到同一個標籤時出現問題。本質上我想更新那個標籤。 我想通過按下一個使用SetPixmap作爲插槽功能的按鈕來實現它。 但是,使用SetPixmap的直接調用可以工作,但是當它位於某個插槽中時,它不適用於我。QT標籤:使用圖像更新標籤

#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; 
} 

回答

1

信號的簽名必須與接收時隙的簽名相匹配。 因此,在這段代碼中,這些是錯誤的。

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(); 
}