QCheckbox/QRadioButton线路包裹Qt4.6.0

QCheckbox/QRadioButton线路包裹Qt4.6.0,qt4,checkbox,radio-button,qcheckbox,Qt4,Checkbox,Radio Button,Qcheckbox,我正在尝试使用标准的QCheckbox/QRadioButton创建一个带有Qt的多行复选框/单选按钮 我没有找到直接的解决方案,因为QRadioButton{wrap:true;}没有效果。 唯一可能的是访问QRadioButton->label->setLineWrap(true)但是 我想从设计师那里得到 不必重写小部件 除了把一个QRadioButton和一个QLabel放在一起,还有什么想法吗 Thx,Boris。我知道的唯一方法(但它不是“可布局的”)是将符号\n放入字符串。这确实很

我正在尝试使用标准的QCheckbox/QRadioButton创建一个带有Qt的多行复选框/单选按钮

我没有找到直接的解决方案,因为
QRadioButton{wrap:true;}
没有效果。 唯一可能的是访问
QRadioButton->label->setLineWrap(true)
但是

  • 我想从设计师那里得到
  • 不必重写小部件
  • 除了把一个QRadioButton和一个QLabel放在一起,还有什么想法吗


    Thx,Boris。

    我知道的唯一方法(但它不是“可布局的”)是将符号\n放入字符串。

    这确实很烦人,不重新实现就无法解决:如果我没有弄错的话,标签使用
    Qt::PlainText
    。在我们的项目中,UI团队用两种方法解决了这个问题

    使用QRadioButton按钮作为标题

    重写QRadioButton文本,使其仅包含选项的简要说明。在下面放置一个QLabel,并添加更长的描述。我们使用较小的字体和一点缩进使它看起来整洁。例如,这在MacOSX中经常使用

    从单选按钮中删除文本


    重新启动UI,使每个单选按钮都放在QLabel的左侧。将整个文本添加到QLabel并将标签设置为单选按钮的好友。这是功能最少的方法,因为单击标签不会选中单选按钮。此外,对齐也不是很好。

    我成功地将布局和标签添加为单选按钮的子项,并将垂直大小策略更改为首选(而不是固定)

    不幸的是,这并没有像本机标签一样自动让它对鼠标悬停和点击做出反应,但观察单选按钮的hack:I setStyleSheet(“border:none”),它开始工作。也许这是一些发生在幕后的特征;我很想确定一下

    可以使用“QRadioButton::indicator{subcrol position:top left}”对齐指示器我的解决方案:

    #ifndef CHECKBOX_H
    #define CHECKBOX_H
    
    #include <QCheckBox>
    
    #include <QHBoxLayout>
    #include <QLabel>
    
    class CheckBox : public QCheckBox
    {
        Q_OBJECT
    public:
       explicit CheckBox(QWidget *parent = 0);
    
       void setText(const QString & text);
       QSize sizeHint() const;
       bool hitButton(const QPoint &pos) const;
    
    protected:
       void paintEvent(QPaintEvent *);
    
    private:
       QHBoxLayout* _layout;
       QLabel*      _label;
    };
    
    #endif // CHECKBOX_H
    
    
    
    
    #include "checkbox.h"
    
    #include <QStylePainter>
    #include <QStyleOption>
    
    #define MARGIN 4 // hardcoded spacing acording to QCommonStyle implementation
    
    CheckBox::CheckBox(QWidget *parent) : QCheckBox(parent)
    {
        setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred, QSizePolicy::CheckBox));
    
        QStyleOptionButton opt;
        initStyleOption(&opt);
    
        QRect label_rect = style()->subElementRect(QStyle::SE_CheckBoxContents, &opt, this);
    
        _label = new QLabel(this);
        _label->setWordWrap(true);
        _label->setMouseTracking(true);
        //_label->setMinimumHeight(label_rect.height());
    
        _layout = new QHBoxLayout(this);
        _layout->setContentsMargins(label_rect.left()+MARGIN, MARGIN/2, MARGIN/2, MARGIN/2);
        _layout->setSpacing(0);
        _layout->addWidget(_label);
    
        setLayout(_layout);
    }
    
    void CheckBox::setText(const QString & text)
    {
        _label->setText(text);
        QCheckBox::setText(text);
    }
    
    void CheckBox::paintEvent(QPaintEvent *)
    {
        QStylePainter p(this);
        QStyleOptionButton opt;
        initStyleOption(&opt);
    
        QStyleOptionButton subopt = opt;
        subopt.rect = style()->subElementRect(QStyle::SE_CheckBoxIndicator, &opt, this);
        subopt.rect.moveTop(opt.rect.top()+MARGIN/2); // align indicator to top
    
        style()->proxy()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &subopt, &p, this);
    
        if (opt.state & QStyle::State_HasFocus)
        {
            QStyleOptionFocusRect fropt;
            fropt.QStyleOption::operator=(opt);
            fropt.rect = style()->subElementRect(QStyle::SE_CheckBoxFocusRect, &opt, this);
            style()->proxy()->drawPrimitive(QStyle::PE_FrameFocusRect, &fropt, &p, this);
        }
    }
    
    QSize CheckBox::sizeHint() const
    {
        return QSize(); // will be calculated by layout
    }
    
    bool CheckBox::hitButton(const QPoint &pos) const
    {
        QStyleOptionButton opt;
        initStyleOption(&opt);
        return opt.rect.contains(pos); // hit all button
    }
    
    \ifndef复选框\u H
    #定义复选框
    #包括
    #包括
    #包括
    类复选框:公共QCheckBox
    {
    Q_对象
    公众:
    显式复选框(QWidget*parent=0);
    void setText(常量QString和text);
    QSize sizeHint()常量;
    布尔点击按钮(常量QPoint和pos)常量;
    受保护的:
    无效油漆事件(QPaintEvent*);
    私人:
    QHBoxLayout*\u布局;
    QLabel*_标签;
    };
    #endif//CHECKBOX\u H
    #包括“checkbox.h”
    #包括
    #包括
    #根据QCommonStyle实现定义边距4//硬编码间距
    复选框::复选框(QWidget*父项):QCheckBox(父项)
    {
    设置izepolicy(QSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred,QSizePolicy::CheckBox));
    QSTYLEOPTION按钮opt;
    initStyleOption(&opt);
    QRect label_rect=style()->subElementRect(QStyle::SE_CheckBoxContents,&opt,this);
    _标签=新的QLabel(本);
    _标签->setWordWrap(真);
    _标签->设置鼠标追踪(真);
    //_label->setMinimumHeight(label_rect.height());
    _布局=新的QHBoxLayout(本);
    _布局->设置内容边距(标签右左()+边距、边距/2、边距/2、边距/2);
    _布局->设置间距(0);
    _布局->添加小部件(_标签);
    setLayout(_布局);
    }
    void复选框::setText(常量QString&text)
    {
    _标签->设置文本(文本);
    QCheckBox::setText(文本);
    }
    无效复选框::paintEvent(QPaintEvent*)
    {
    QSP(本);
    QSTYLEOPTION按钮opt;
    initStyleOption(&opt);
    QStyleOptionButton subopt=opt;
    subopt.rect=style()->subElementRect(QStyle::SE_CheckBoxIndicator,&opt,this);
    subopt.rect.moveTop(opt.rect.top()+边距/2);//将指示器与顶部对齐
    style()->proxy()->drawPrimitive(QStyle::PE_IndicatorCheckBox,&subpt,&p,this);
    if(opt.state&QStyle::state\u HasFocus)
    {
    QStyleOptionFocusRect fropt;
    fropt.QStyleOption::operator=(opt);
    fropt.rect=style()->subElementRect(QStyle::SE_CheckBoxFocusRect,&opt,this);
    style()->proxy()->drawPrimitive(QStyle::PE_FrameFocusRect,&fropt,&p,this);
    }
    }
    QSize复选框::sizeHint()常量
    {
    return QSize();//将按布局计算
    }
    布尔复选框::点击按钮(常量QPoint和pos)常量
    {
    QSTYLEOPTION按钮opt;
    initStyleOption(&opt);
    返回opt.rect.contains(pos);//点击所有按钮
    }
    
    谢谢您的回答。我用你解释的第二个解决方案解决了我的问题。这不是很好的布局,但工程。。。我真的希望Qt能在不久的将来解决这个问题!无论如何,再次感谢您。解决方案的开头很好。但是,计算焦点矩形的计算算法应该改进(即“fropt.rect=style()->subElementRect(QStyle::SE_CheckBoxFocusRect,&opt,this);”),因为焦点矩形将与多行文本混在一起,而不是环绕它。为了确保系统为此类问题绘制出漂亮的焦点矩形,您需要为此类矩形设置适当的高度,因为多行文本具有不同的高度。