我试图让QRadioButton
的大小随窗口大小而变化。我需要的是,当我调整窗口的大小变得更大(或更小)时,QRadioButton
也应该调整为变大(或更小)。
我尝试使用我用于QPushButton
的方法,但我发现它不起作用(检查底部的pic1和pic2)。 QRadioButton
的大小保持不变。
此外,我还为setStyleSheet
尝试QRadioButton
,但是我点击图片无效,这不是我想要的结果(点击图片应该有效在我的情况下)。
我想知道如何修改我的代码。谢谢!
以下是一些相关代码:
.h文件
QRadioButton *funcSourceOrientedButton;
QPushButton *funcLockButton;
.cpp文件
QPixmap pixmap(":/images/Function/SourceOriented.png");
QIcon ButtonIcon(pixmap);
funcSourceOrientedButton->setIcon(ButtonIcon);
funcSourceOrientedButton->setIconSize(function_oriented_button_size);
funcSourceOrientedButton->setMinimumSize(function_oriented_button_size);
funcSourceOrientedButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
funcLockButton->setStyleSheet("border-image: url(:/images/Function/WLock.png);");
funcLockButton->setMinimumSize(function_button_size);
funcLockButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
以下是图片:
PIC1
PIC2
答案 0 :(得分:1)
您需要创建一个布局并将这些按钮添加到其中
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
class QRadioButton;
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
~MyWidget();
bool eventFilter(QObject *obj, QEvent *event);
private:
QSize __CalculateIndicatorSize(const QSize & radioBtnSize);
private:
QRadioButton * m_radioBtn1;
QRadioButton * m_radioBtn2;
QString styleForButton_;
QString styleForIndicator_;
};
#endif // MYWIDGET_H
mywidget.cpp
#include "mywidget.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QRadioButton>
#include <QEvent>
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
styleForButton_ = "QRadioButton{border-width: 10 10 10 10; border-image: url(C:/Users/phamat/Desktop/bg.png) 10 10 10 10;}";
styleForIndicator_ = "QRadioButton::indicator{width: %1px; height: %2px;}";
QHBoxLayout * layout = new QHBoxLayout();
layout->setContentsMargins(20, 20, 20, 20);
layout->setSpacing(20);
setLayout(layout);
m_radioBtn1 = new QRadioButton(this);
m_radioBtn1->setText("Radio button 1");
m_radioBtn1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_radioBtn1->setStyleSheet(styleForButton_);
m_radioBtn1->installEventFilter(this);
layout->addWidget(m_radioBtn1);
m_radioBtn2 = new QRadioButton(this);
m_radioBtn2->setText("Radio button 2");
m_radioBtn2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_radioBtn2->setStyleSheet(styleForButton_);
m_radioBtn2->installEventFilter(this);
layout->addWidget(m_radioBtn2);
}
MyWidget::~MyWidget()
{
}
bool MyWidget::eventFilter(QObject *obj, QEvent *event)
{
if (obj == m_radioBtn1)
{
if (event->type() == QEvent::Resize)
{
QSize size = __CalculateIndicatorSize(m_radioBtn1->size());
m_radioBtn1->setStyleSheet(styleForButton_ + styleForIndicator_.arg(size.width()).arg(size.height()));
}
}
else if (obj == m_radioBtn2)
{
if (event->type() == QEvent::Resize)
{
QSize size = __CalculateIndicatorSize(m_radioBtn2->size());
m_radioBtn2->setStyleSheet(styleForButton_ + styleForIndicator_.arg(size.width()).arg(size.height()));
}
}
return QWidget::eventFilter(obj, event);
}
QSize MyWidget::__CalculateIndicatorSize(const QSize & radioBtnSize)
{
if (radioBtnSize.width() > 300)
{
return QSize(20, 20);
}
else if (radioBtnSize.width() > 200)
{
return QSize(15, 15);
}
else
{
return QSize(10, 10);
}
}
pic 1
pic 2