如何在QGroupBox标题中添加图标?

J C*_*lez 2 c++ qt

是否可以在QGroupBox标题中添加图标(以编程方式,通过QtCreator,有或没有样式表)?

我想在我的GUI中放置一些标记靠近一些QGroupBox的标题.最好的位置是标题的右侧,但任何其他合理的位置(标题左侧或小部件的右上角)也可能有效.

我能想到的唯一方法是创建一个带有图标的QLabel,并将其视觉放置在QGroupBox的顶部,位于所需位置.但我发现这是一个丑陋的解决方案.

编辑

我知道有一种方法可以使用样式表indicator为可检查的 QGroupBox 设置用作任何图像,包括已检查和未检查状态.但是QGroupBox必须是可检查的等等.我想将这个标记(一个小图像)独立地添加到QGroupBox的可检查性中.

sco*_*nov 6

背景

不幸的是,QGroupBox没有setIcon方法,也QPushButton没有.

一个可能的解决方案是QGroupBox使用QGroupBox::setCheckable如下样式表来使用并将自定义图像设置为指示器来进行检查:

QGroupBox::indicator:unchecked {
    image: url(:/images/checkbox_unchecked.png);
}
Run Code Online (Sandbox Code Playgroud)

但是,如果取消选中该复选框,则会禁用整个组框.

解

考虑到这个背景,我建议你一个相当复杂但也非常灵活的解决方案,包括以下步骤:

  1. 子类QStyleOptionGroupBox并将其他变量声明为公共类属性:

    QPixmap pixmap;
    int offset;
    int leftSpace;
    int rightSpace;
    
    Run Code Online (Sandbox Code Playgroud)

    这些将用于将装饰像素图以及放置设置传递给样式.

  2. 子类QCommonStyle和重新实现QCommonStyle::drawComplexControl如下:

    if (const StyleOptionDecoratedGroupBox *groupBox = qstyleoption_cast<const StyleOptionDecoratedGroupBox *>(opt)) {
        QRect textRect = proxy()->subControlRect(CC_GroupBox, opt, SC_GroupBoxLabel, widget);
        QRect checkBoxRect = proxy()->subControlRect(CC_GroupBox, opt, SC_GroupBoxCheckBox, widget);
        int decorationOffset = 0;
        int pixmapRectWidth = 0;
        int pixmapHeight = 0;
        int textOffset = 0;
    
        if (!groupBox->pixmap.isNull()) {
            decorationOffset = groupBox->offset;
            pixmapRectWidth = groupBox->leftSpace
                            + groupBox->pixmap.width()
                            + groupBox->rightSpace;
            pixmapHeight = groupBox->pixmap.height();
            textOffset = decorationOffset + pixmapRectWidth;
        }
    
        textRect.adjust(textOffset, 0, textOffset, 0);
    
        // Draw frame
        if (groupBox->subControls & QStyle::SC_GroupBoxFrame) {
            QStyleOptionFrame frame;
            frame.QStyleOption::operator=(*groupBox);
            frame.features = groupBox->features;
            frame.lineWidth = groupBox->lineWidth;
            frame.midLineWidth = groupBox->midLineWidth;
            frame.rect = proxy()->subControlRect(CC_GroupBox, opt, SC_GroupBoxFrame, widget);
            p->save();
            QRegion region(groupBox->rect);
            if (!groupBox->text.isEmpty()) {
                bool ltr = groupBox->direction == Qt::LeftToRight;
                QRect finalRect;
                if (groupBox->subControls & QStyle::SC_GroupBoxCheckBox) {
                    finalRect = checkBoxRect.united(textRect);
                    finalRect.adjust(ltr ? -4 : 0, 0, ltr ? 0 : 4, 0);
                } else {
                    finalRect = textRect;
                }
                region -= finalRect;
            }
            p->setClipRegion(region);
            proxy()->drawPrimitive(PE_FrameGroupBox, &frame, p, widget);
            p->restore();
        }
    
        // Draw icon
        if (!groupBox->pixmap.isNull()) {
            p->fillRect(decorationOffset, 0, pixmapRectWidth, pixmapHeight, opt->palette.window().color());
            proxy()->drawItemPixmap(p, QRect(decorationOffset, 0, pixmapRectWidth, pixmapHeight),
                                    Qt::AlignCenter, groupBox->pixmap);
        }
    
        // Draw title
        if ((groupBox->subControls & QStyle::SC_GroupBoxLabel) && !groupBox->text.isEmpty()) {
            QColor textColor = groupBox->textColor;
            if (textColor.isValid())
                p->setPen(textColor);
            int alignment = int(groupBox->textAlignment);
            if (!proxy()->styleHint(QStyle::SH_UnderlineShortcut, opt, widget))
                alignment |= Qt::TextHideMnemonic;
            proxy()->drawItemText(p, textRect,  Qt::TextShowMnemonic | Qt::AlignHCenter | alignment,
                                  groupBox->palette, groupBox->state & State_Enabled, groupBox->text,
                                  textColor.isValid() ? QPalette::NoRole : QPalette::WindowText);
            if (groupBox->state & State_HasFocus) {
                QStyleOptionFocusRect fropt;
                fropt.QStyleOption::operator=(*groupBox);
                fropt.rect = textRect;
                proxy()->drawPrimitive(PE_FrameFocusRect, &fropt, p, widget);
            }
        }
    } else {
        QCommonStyle::drawComplexControl(cc, opt, p, widget);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 子类QGroupBox和重新实现QGroupBox::paintEvent如下:

    CustomStyle style;
    QPainter painter(this);
    StyleOptionDecoratedGroupBox option;
    
    initStyleOption(&option);
    option.pixmap = m_pixmap;
    option.offset = m_offset;
    option.leftSpace = m_leftSpace;
    option.rightSpace = m_rightSpace;
    
    style.drawComplexControl(QStyle::CC_GroupBox, &option, &painter, this);
    
    Run Code Online (Sandbox Code Playgroud)

注意:当组框可检查时,此解决方案无法正常工作.

例

解决方案需要大量工作,但幸运的是我已经为您创建了必要的类,以及如何在您自己的项目中使用它们的完整示例.该代码可在GitHub上获得.

结果

提供的示例产生以下结果:

窗口与装饰组框和单选按钮

  • 从这样一个简单的起点出发,这是一个非常全面的答案 - 出色的工作! (3认同)
  • 你得到了我的赞成,但我仍然有一个小小的批评者:你没有编辑图片链接的替代文字。由于我公司的防火墙阻止了 imgur.com(我无法说服管理员解除阻止),我总是看到这些_在此处输入图片描述_链接。这有点烦人...... ;-)(当我几十年前学习 HTML 时,这是基本建议之一:始终为图像提供替代文本 - 现在似乎完全忘记了这一点。) (2认同)
  • @Scheff,我非常感谢您的批评者。事实上,[qt] 标签描述说:_我们在这个标签中追求高质量_。因此,卓越在于细节,没有任何细节可以不改进。在您发表评论后,我在大多数答案的图片中添加了说明。稍后我将编辑其余部分。谢谢你提醒我! (2认同)