是否可以翻转RadioButton
?默认情况下,圆圈左对齐,文本右对齐。我想把文字放在左边,然后圈到右边。使用LayoutMirroring.childrenInherit: true
我将文字放在左侧,但圆圈仍位于左侧。
Column {
id: column1
x: -265
y: 219
width: 104
height: 45
spacing: 5
LayoutMirroring.enabled: true
LayoutMirroring.childrenInherit: true
ExclusiveGroup { id: diamTypes }
RadioButton { text: "one"; exclusiveGroup: diamTypes }
RadioButton { text: "two"; exclusiveGroup: diamTypes }
}
答案 0 :(得分:2)
在Text
项目中添加RadioButton
项(带有所需名称),并为其提供相对x
和y
位置。将text
的{{1}}属性保留为空白。
RadioButton
答案 1 :(得分:1)
RadioButtonStyle
也可用于此目的。应该以某种方式设置对齐:
RadioButton {
text: "bla-bla";
exclusiveGroup: diamTypes
style: RadioButtonStyle {
label: Label {
text: control.text
font.pointSize: 14
anchors.margins: 0
}
indicator: Rectangle {
implicitWidth: 16
implicitHeight: 16
radius: 9
border.color: control.activeFocus ? "darkblue" : "gray"
border.width: 1
Rectangle {
anchors.fill: parent
visible: control.checked
color: "#555"
radius: 9
anchors.margins: 4
}
}
}
}
答案 2 :(得分:1)
以下是使用小部件的方法:
继承QRadioButton
,为文字添加新的QLabel
,将原始QRadioButton::text
设为空,然后调整填充。像这样:
QFont myFont;
QFontMetrics fm(myFont);
int indicatorPadding = WINDOW_WIDTH - 25;
this->setStyleSheet(QString("QRadioButton { "
"text-align: left; "
"padding-left: " + QString::number(indicatorPadding ) + "px; "
"} ")
);
int paddingLeft = indicatorPadding - RADIO_BTN_WIDTH - fm.width(radioText->text());
radioText->setStyleSheet("padding-top: 7px; padding-left: " + QString::number(paddingLeft) + ";");
这是我的RTL语言RadioButtons的完整类(它也相应地对齐文本):
<强> my_radiobutton.h 强>:
#ifndef MYRADIOBUTTON_H
#define MYRADIOBUTTON_H
#include <QRadioButton>
#include <QEvent>
#include <QLabel>
#include "constants.h"
class MyRadioButton : public QRadioButton
{
private:
QLabel* radioText = NULL;
public:
explicit MyRadioButton(QWidget *parent=0);
explicit MyRadioButton(const QString &text, QWidget *parent=0);
void setText(const QString &text);
QString text() const;
protected:
void alignText();
void changeEvent(QEvent * event);
};
#endif // MYRADIOBUTTON_H
<强> my_radiobutton.cpp 强>:
#include "my_radiobutton.h"
bool isRTL(); //returns true if RTL language, and false otherwise
MyRadioButton::MyRadioButton(QWidget *parent)
: QRadioButton(parent)
{
if(isRTL()){
radioText = new QLabel(this);
}
}
MyRadioButton::MyRadioButton(const QString &text, QWidget *parent)
: QRadioButton(text,parent)
{
if(isRTL()){
radioText = new QLabel(this);
}
}
void MyRadioButton::setText(const QString &text)
{
if(isRTL()){
QRadioButton::setText("");
if(radioText){
radioText->setText(text);
}
}
else{
QRadioButton::setText(text);
}
alignText();
}
QString MyRadioButton::text() const
{
if(isRTL() && radioText){
return radioText->text();
}
else{
return QRadioButton::text();
}
}
void MyRadioButton::alignText()
{
if(isRTL() && radioText){
QFont myFont;
QFontMetrics fm(myFont);
int indicatorPadding = WINDOW_WIDTH - 25;
this->setStyleSheet(QString("QRadioButton { "
"text-align: left; "
"padding-left: " + QString::number(indicatorPadding ) + "px; "
"} ")
);
int paddingLeft = indicatorPadding - RADIO_BTN_WIDTH - fm.width(radioText->text());
radioText->setStyleSheet("padding-top: " + QString::number(CENTER_OF_LINE) + "px; padding-left: " + QString::number(paddingLeft) + ";");
}
else{
this->setStyleSheet(QString("QRadioButton { "
"text-align: left; "
"padding-left: " TEXT_PADDING "px; "
"} ")
);
}
}
void MyRadioButton::changeEvent(QEvent * event)
{
if (event->type() == QEvent::LanguageChange){
alignText();
}
QWidget::changeEvent(event);
}
<强> constants.h 强>:
#define WINDOW_WIDTH 220
#define RADIO_BTN_WIDTH 10
#define CENTER_OF_LINE ((LINE_HEIGHT/2) - (FONT_SIZE/2)) // ==7
#define TEXT_PADDING STRINGIFY(10)
结果:
答案 3 :(得分:1)
转到设计视图并将方向设置为“从左到右”。
答案 4 :(得分:0)
您可以轻松做到:
RadioButton {
id: offlineMapParentBox
text: qsTr("radio button")
anchors.left: parent.left
LayoutMirroring.enabled: true
}
或者,如果您使用的是从左到右和从右到左的多语言应用程序,则可以按照以下步骤操作:
RadioButton {
id: offlineMapParentBox
text: qsTr("Offline Map")
anchors.left: parent.left
LayoutMirroring.enabled: isLeftToRight ? false : true
}
谢谢