2012-03-07 108 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