QCheckbox / QRadioButton换行Qt4.6.0

问题描述 投票:8回答:4

我正在尝试使用标准QCheckbox / QRadioButton与Qt建立多行复选框/单选按钮。

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

  1. 我想从设计师那里做到这一点
  2. 不必重写小部件

除了将QRadioButton和QLabel放在一起之外,还有什么想法?

Thx,鲍里斯。

qt4 checkbox radio-button qcheckbox
4个回答
11
投票

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

使用QRadioButton按钮作为标题

Layout using the QRadioButton as a title

以这样的方式重写QRadioButton文本,使其只有该选项的简短描述。将QLabel置于其下方并添加更长的描述。我们使用较小的字体和小缩进使其看起来整洁。例如,这在Mac OS X中经常使用。

从单选按钮中删除文本

Layout using buddy labels

重新布局UI,以便每个单选按钮放在QLabel的左侧。将整个文本添加到QLabel并将标签设置为单选按钮的伙伴。这是功能最少的方法,因为单击标签不会检查单选按钮。此外,对齐不是很好。


3
投票

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


2
投票

我成功地为单选按钮添加了布局和标签作为子项,并将垂直大小策略更改为Preferred(而不是Fixed)。

不幸的是,这并没有自动使它对鼠标悬停和点击(如原生标签)作出反应,但是看一下黑客:我为单选按钮设置了setStyleSheet(“border:none”),它开始工作。也许这是幕后发生的一些特征;我很想有一定的确定性。

并且指标可以使用“QRadioButton :: indicator {subcontrol-position:top left}”对齐< - http://doc.trolltech.com/qq/qq20-qss.html


0
投票

我的解决方案

#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
}
© www.soinside.com 2019 - 2024. All rights reserved.