用Qt4的同学经常会读取图片的操作,比如某个图标要显示图片之类的。

下面我来介绍如何在Qt4中操作图形文件。

1.首先在项目的根目录下建立images文件夹,然后再里面放一个图形文件如:linuxredhat.png

2.在messgeboax.pro中添加一行:

RESOURCES += messagebox.qrc

3.然后创建一个资源文件messagebox.qrc:

<!DOCTYPE RCC><RCC version="1.0">
<qresource>
    <file>images/linuxredhat.png</file>
</qresource>
</RCC>

4.然后就可以使用这个资源了,在messagebox.cpp中添加:

 QMessageBox customMsgBox;
    customMsgBox.setWindowTitle("Custom message box");
    QPushButton *lockButton = customMsgBox.addButton(tr("Lock"),QMessageBox::ActionRole);
    QPushButton *unlockButton = customMsgBox.addButton(tr("Unlock"),QMessageBox::ActionRole);
    QPushButton *cancelButton = customMsgBox.addButton(QMessageBox::Cancel);
    customMsgBox.setIconPixmap(QPixmap(":/images/linuxredhat.png"));
    customMsgBox.setText(tr("This is a custom message box"));
    customMsgBox.exec();
    
    if(customMsgBox.clickedButton() == lockButton)
    	label->setText(" Custom MessageBox / Lock ");
    if(customMsgBox.clickedButton() == unlockButton)
    	label->setText(" Custom MessageBox / Unlock ");
    if(customMsgBox.clickedButton() == cancelButton)
    	label->setText(" Custom MessageBox / Cancel ");

注意:

  它的路径为: “:/images/linuxredhat.png"

  为相对路径。待续......

Logo

更多推荐