[英]Align QCheckbox text and a QLabel underneath it
我有一个带有一些文本的复选框,并且在此复选框下方有一个标签。 如何对齐此标签,使其与复选框上的文本对齐。
我想要的是:
[ ] insert_text
some_text
我有的:
[ ] insert_text
some_text
一个可能的解决方案是在QLabel的左侧添加一个合适宽度的填充物,计算宽度,我创建了一个自定义QCheckBox,它返回指标的宽度,但是要达到这个数量,您必须添加几个代表指标和文本之间的间隔:
#include <QtWidgets>
class CheckBox: public QCheckBox{
public:
using QCheckBox::QCheckBox;
int width_of_indicator(){
QStyleOptionButton opt;
initStyleOption(&opt);
return style()->subElementRect(QStyle::SE_CheckBoxIndicator, &opt,this).width();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
auto ch = new CheckBox("insert_text");
auto label = new QLabel("some_text: Stack Overflow");
label->setStyleSheet(QString("padding-left: %1px").arg(ch->width_of_indicator()+2));
auto lay = new QVBoxLayout(&w);
lay->addWidget(ch);
lay->addWidget(label);
w.show();
return a.exec();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.