qRadioButton



#ifndef TESTRADIOBUTTON_H
#define TESTRADIOBUTTON_H

#include <QtWidgets/QMainWindow>
#include "ui_testradiobutton.h"

class testRadioButton : public QMainWindow
{
 Q_OBJECT

public:
 testRadioButton(QWidget *parent = 0);
 ~testRadioButton();

private:
 Ui::testRadioButtonClass ui;
 QButtonGroup * _btnGroup;

public slots:

 void setLabelText();
};

#endif // TESTRADIOBUTTON_H



#include "testradiobutton.h"

testRadioButton::testRadioButton(QWidget *parent)
 : QMainWindow(parent)
{
 ui.setupUi(this);

 _btnGroup = new QButtonGroup(this);
 _btnGroup->addButton(ui.radioButton, 0);
 _btnGroup->addButton(ui.radioButton_2, 1);
 _btnGroup->addButton(ui.radioButton_3, 2);
 ui.radioButton->setChecked(true);

 connect(ui.radioButton, SIGNAL(clicked()), this, SLOT(setLabelText()));
 connect(ui.radioButton_2, SIGNAL(clicked()), this, SLOT(setLabelText()));
 connect(ui.radioButton_3, SIGNAL(clicked()), this, SLOT(setLabelText()));

 
}

testRadioButton::~testRadioButton()
{

}

void testRadioButton::setLabelText()
{
 int selectedID = _btnGroup->checkedId();
 
 QString qText = "";
 switch (selectedID)
 {
 case 0 :
  qText = "选项1";
  break;

 case 1:
  qText = "选项2";
  break;

 case 2:
  qText = "选项3";
  break;

 default:
  break;
 }
 ui.label->setText(qText);
}

Qt QRadioButton自绘,点击时全黑

04-21

如题,QRadioButton要求单选框出现在ICON下边,于是自己继承了QCommonStyle,实现了subElementRect和sizeFromContents方法,实现单选框显示在ICON下边,代码如下:rnclass MediaStyle : public QCommonStylernrn Q_OBJECTrnpublic:rn MediaStyle() : QCommonStyle() rnrn QRect subElementRect(SubElement sr, const QStyleOption* opt, const QWidget* widget) constrn rn if (SE_RadioButtonIndicator == sr)rn rn QRect rect;rn int nWidth = proxy()->pixelMetric(PM_ExclusiveIndicatorWidth, opt, widget);rn int nHeight = proxy()->pixelMetric(PM_ExclusiveIndicatorHeight, opt, widget);rn rect.setRect(opt->rect.x() + ((opt->rect.width() - nWidth) / 2 + 1),rn opt->rect.y() + (opt->rect.height() - nHeight),rn nWidth, nHeight);rn return visualRect(opt->direction, opt->rect, rect);rn rn else if (SE_RadioButtonContents == sr)rn rn QRect rect;rn QRect rc = visualRect(opt->direction, opt->rect, subElementRect(SE_RadioButtonIndicator, opt, widget));rn int nSpacing = proxy()->pixelMetric(PM_RadioButtonLabelSpacing, opt, widget);rn rect.setRect(opt->rect.x() + ((opt->rect.height()- rc.height() - nSpacing) / 2), opt->rect.y(),rn opt->rect.width(), opt->rect.height() - rc.height() - nSpacing);rn return visualRect(opt->direction, opt->rect, rect);rn rn// else if (SE_RadioButtonClickRect == sr)rn// rn// QRect rect = subElementRect(SE_RadioButtonFocusRect, opt, widget);rn// rect |= subElementRect(SE_RadioButtonIndicator, opt, widget);rn// rnrn return QCommonStyle::subElementRect(sr, opt, widget);rn rnrn QSize sizeFromContents(ContentsType ct, const QStyleOption* opt, const QSize& csz, const QWidget* widget = 0) constrn rn if (CT_RadioButton == ct)rn rn QSize sz(csz);rn if (const QStyleOptionButton *btn = qstyleoption_cast(opt))rn rn int w = proxy()->pixelMetric(PM_ExclusiveIndicatorWidth, btn, widget);rn int h = proxy()->pixelMetric(PM_ExclusiveIndicatorHeight, btn, widget);rnrn int margins = 0;rn // we add 4 pixels for label marginsrn if (!btn->icon.isNull() || !btn->text.isEmpty())rn margins = 4 + proxy()->pixelMetric(PM_RadioButtonLabelSpacing, opt, widget);rn sz += QSize(4, h + margins);rn sz.setWidth(qMax(sz.width(), w));rn return sz;rn rn rnrn return QCommonStyle::sizeFromContents(ct, opt, csz, widget);rn rn;rnrnrnrnQRadioButton重写:rnclass AudioButton : public QRadioButtonrnrn Q_OBJECTrnpublic:rn AudioButton(const QString& path, QWidget* parent = 0) : QRadioButton(parent),rn m_pMediaStyle(new MediaStyle)rn rn this->setMinimumSize(52, 68);rn this->setIcon(QIcon(path));rn this->setIconSize(QSize(50, 50));rn this->setAttribute(Qt::WA_TranslucentBackground);rn this->setStyle(m_pMediaStyle);rn rn ~AudioButton()rn rn delete m_pMediaStyle;rn rnrnprivate:rn MediaStyle* m_pMediaStyle;rn;

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试