Qt为Qtreeview中的子项目设置不同的高度

发布于 2025-01-14 05:31:06 字数 303 浏览 4 评论 0原文

我目前正在通过样式表定义 QTreeView 中项目的高度,

QTreeView {
    background: palette(window);
    color: palette(text);
    border: none;
}

QTreeView::item {
    height: 40px;
    padding-top: 0.5ex;
    padding-bottom: 0.5ex;
    margin: 2px;
}

这样,树中的所有项目都将具有相同的高度。是否可以为孩子定义不同的身高?

I'm currently defining the heights of items in a QTreeView via a stylesheet

QTreeView {
    background: palette(window);
    color: palette(text);
    border: none;
}

QTreeView::item {
    height: 40px;
    padding-top: 0.5ex;
    padding-bottom: 0.5ex;
    margin: 2px;
}

With this, all items in the tree will have the same height. Is it possible to define a different height for the children?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舟遥客 2025-01-21 05:31:06

我不确定这是否可以通过样式表来完成。实现此目的的正常方法是覆盖 QAbstractItemModel::data< /code>并让它返回与 Qt::SizeHintRole 数据角色关联的特定值。

QVariant new_model::data (const QModelIndex &index, int role) const
{
    if (role == Qt::SizeHintRole) {

        /*
         * Calculate required size hint based on model data etc.
         */
        QSize size = ...;
        return size;
    }

    /*
     * Defer to base class implementation.
     */
    return base_class::data(index, role);
}

I'm not sure if this can be done via a stylesheet. The normal way to achieve this would be to override QAbstractItemModel::data and have it return specific values associated with the Qt::SizeHintRole data role.

QVariant new_model::data (const QModelIndex &index, int role) const
{
    if (role == Qt::SizeHintRole) {

        /*
         * Calculate required size hint based on model data etc.
         */
        QSize size = ...;
        return size;
    }

    /*
     * Defer to base class implementation.
     */
    return base_class::data(index, role);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文