Qt4--创建自定义图标

1、通过Qt Assistant 查看 Qt resource system

原文:The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable. This is useful if your application always needs a certain set of files (icons, translation files, etc.) and you don't want to run the risk of losing the files.


意思是说,Qt resource system是和平台无关,他用来为应用程序存储二进制文件。如果在运行过程中不想要冒文件丢失的险,这样的文件集就是一个很好的选择。


原文:The resources associated with an application are specified in a .qrc file, an XML-based file format that lists files on the disk and optionally assigns them a resource name that the application must use to access the resource.

Here's an example .qrc file:

<!DOCTYPE RCC><RCC version="1.0">
 <qresource>
     <file>images/copy.png</file>
     <file>images/cut.png</file>
     <file>images/new.png</file>
     <file>images/open.png</file>
     <file>images/paste.png</file>
     <file>images/save.png</file>
 </qresource>
 </RCC>
这些资源被存放在.qrc文件下,这是一个基于XML格式的文件。

原文:For a resource to be compiled into the binary the .qrc file must be mentioned in the application's .pro file so that qmake knows about it. For example:

RESOURCES     = application.qrc
需要在.pro文件内加入.qrc文件

如下图是qmake以后生产的文件。也就是程序中可以用如“:/images/copy.png”



原文:

Using Resources in the Application

In the application, resource paths can be used in most places instead of ordinary file system paths. In particular, you can pass a resource path instead of a file name to the QIconQImage, or QPixmap constructor:

cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);


2、示例
1、在目录下建立images文件夹,在文件夹内放入图片:buttonico.png



2、建立一个Qt resource 文件,取名为images.qrc



3、修改images.qrc文件内容


修改好以后Qt Creator内images.qrc发生变化



4、创建main.cpp

#include<QApplication>
#include<QPushButton>

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    QPushButton* button = new QPushButton();
    button->setIcon(QIcon(":/images/buttonico.png"));
    QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit()));
    button->show();
    return app.exec();
}

5、生成结果


### 如何在 Qt 中为 QListWidget 创建自定义的列表项 widget 在 Qt 中,可以通过创建自定义的 `QWidget` 并将其设置为 `QListWidgetItem` 的关联 widget 来实现自定义的列表项。以下是具体方法: #### 使用 `QListWidget::setItemWidget()` 设置自定义 widget `QListWidget` 提供了一个名为 `void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget)` 的函数[^1],可以用来将一个自定义的 `QWidget` 关联到某个 `QListWidgetItem` 上。 下面是一个完整的代码示例,展示如何创建带有图标、标签和其他组件的自定义列表项: ```cpp #include <QApplication> #include <QListWidget> #include <QListWidgetItem> #include <QWidget> #include <QHBoxLayout> #include <QLabel> #include <QPushButton> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建主窗口中的 QListWidget 控件 QListWidget listWidget; // 定义一些数据用于填充列表项 const QString labels[] = {"Kits", "环境", "编辑器", "Fakevim", "帮助"}; const QString icons[] = {":/demo12_listwidget/resources/kits.png", ":/demo12_listwidget/resources/env.png", ":/demo12_listwidget/resources/editor.png", ":/demo12_listwidget/resources/vim.png", ":/demo12_listwidget/resources/help.png"}; for (int i = 0; i < 5; ++i) { // 创建一个新的 QListWidgetItem 对象 QListWidgetItem *item = new QListWidgetItem(&listWidget); // 创建一个自定义的 QWidget QWidget *customWidget = new QWidget(); QHBoxLayout *layout = new QHBoxLayout(customWidget); // 添加 QLabel 显示文字 QLabel *label = new QLabel(labels[i]); layout->addWidget(label); // 添加 QPushButton 显示按钮 QPushButton *button = new QPushButton("操作"); layout->addWidget(button); // 如果需要显示图标,则添加 QLabel 其他方式加载图片资源 if (!icons[i].isEmpty()) { QIcon icon(icons[i]); item->setIcon(icon); // 将图标设置给 QListWidgetItem } customWidget->setLayout(layout); // 将自定义的 widget 绑定到对应的 QListWidgetItem listWidget.setItemWidget(item, customWidget); } listWidget.show(); return app.exec(); } ``` #### 解析代码逻辑 1. **创建 QListWidgetItem**: 首先通过 `new QListWidgetItem()` 创建一个新的项目对象并添加到 `QListWidget` 中。 2. **设计自定义 widget**: 构建一个包含多个子控件(如 `QLabel`, `QPushButton` 等)的 `QWidget` 实例作为列表项的内容。 3. **绑定自定义 widget 到 QListWidgetItem**: 调用 `QListWidget::setItemWidget()` 函数,将自定义的 widget 和对应的 `QListWidgetItem` 进行绑定[^1]。 4. **可选功能扩展**: 可以为自定义 widget 增加更多交互功能,比如响应按钮点击事件等。 #### 注意事项 - 自定义的 widget 不会自动调整大小以适应 `QListWidget` 的宽度。如果希望 widget 占满整个列宽,可以在布局管理器中使用拉伸因子将父容器设为固定尺寸。 - 图标可以直接通过 `QListWidgetItem::setIcon()` 方法设置[^2],而无需额外处理。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值