2012-03-07 126 views
3

如何在消息框中顯示圖像。我試過在QmessageBox中顯示圖像

about.setIcon(":/pics/goku3.jpg"); 

但它給了我錯誤。我知道我可以使用內置的關於盒子。以下是顯示有關框的完整代碼。

void MainWindow::on_actionUmer_s_Program_triggered() 
{ 
    QMessageBox about; 

    about.setText("Umer's Program"); 
    about.setInformativeText("Copyright ; 2012 Umer Software Inc.\nI wrote this program  for fun.\n); 
    about.setStandardButtons(QMessageBox::Ok); 
    about.setIcon(":/pics/goku3.jpg"); // here is the error 
    about.setDefaultButton(QMessageBox::Ok); 
    about.show(); 
    about.exec(); 
} 

請同時告訴我如何設置該圖像的大小。

回答

8

你不應該使用about.setIcon(":/pics/goku3.jpg");因爲QMessageBox::setIcon(Icon)方法只使用預定義的圖標是

QMessageBox::NoIcon 
QMessageBox::Question 
QMessageBox::Information 
QMessageBox::Warning 
QMessageBox::Critical 

工程加載你自己的圖片你應該使用:

void setIconPixmap (const QPixmap & pixmap) 

例如:

about.setIconPixmap(QPixMap(":/pics/goku3.jpg")); 

此外,如果你想使用這種格式":/pics/goku3.jpg"確保你的.qrc文件(這是一個資源文件)配置正確。

您可以從here獲取更多信息。

+0

我該如何調整圖標大小。 – 2012-03-07 13:12:01

+0

調整圖像本身大小的最佳方式 但是,如果您想從代碼執行此操作,請創建一個新的像素圖 QPixmap myPixmap(30,30); (大小) ,然後加載圖像: myPixmap.load(filename); 更多信息在這裏http://qt-project.org/doc/qt-4.8/qpixmap.html#pixmap-information – Stely 2012-03-07 15:54:43