Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对齐到QLabel和QLineEdit中的右侧文本_C++_Qt_Qlayout - Fatal编程技术网

C++ 对齐到QLabel和QLineEdit中的右侧文本

C++ 对齐到QLabel和QLineEdit中的右侧文本,c++,qt,qlayout,C++,Qt,Qlayout,我有一个QLabel,就在QLineEdit的下方,具有相同的大小和对齐属性: QLineEdit *lineEdit = new QLineEdit("999"); lineEdit->setFixedWidth(100); lineEdit->setAlignment(Qt::AlignRight); // QLabel *label = new QLabel("999"); label->setFixedWidth(100); label->setAlignment

我有一个
QLabel
,就在
QLineEdit
的下方,具有相同的大小和对齐属性:

QLineEdit *lineEdit = new QLineEdit("999");
lineEdit->setFixedWidth(100);
lineEdit->setAlignment(Qt::AlignRight);
//
QLabel *label = new QLabel("999");
label->setFixedWidth(100);
label->setAlignment(Qt::AlignRight);
//
QLayout *layout = new QVBoxLayout;
layout->addWidget(lineEdit);
layout->addWidget(label);
这是如何呈现的:

如何使底部
标签的文本与
行编辑的文本对齐?


全额奖励如果您找到了一种在所有平台上都适用的解决方案,并且在
行编辑
标签
中字体大小不同时也适用,那么不幸的是,这可能不可能,至少不是现成的,右边距将不起作用,因为它始终为0,即使文本明显向左偏移。原因是这个偏移量不是由边距决定的,而是取决于平台GUI样式和正在使用的特定字体度量的组合,并且它的值“方便地”在类公共接口中不可用,无法获得

您可以轻松获取字体度量,但无法获取
QStyleOptionFrame
,因为所需的方法受到保护,访问它需要子类
QLineEdit
。但是,如果幸运的话,该值很可能为零,因此您可以使用以下简单的方法:

  QVBoxLayout *layout = new QVBoxLayout;
  QLineEdit *lineEdit = new QLineEdit("999");
  lineEdit->setAlignment(Qt::AlignRight);
  QLabel *label = new QLabel("999");
  label->setAlignment(Qt::AlignRight);

  int offsetValue = lineEdit->fontMetrics().averageCharWidth();
  label->setIndent(offsetValue);

  setLayout(layout);
  layout->addWidget(lineEdit);
  layout->addWidget(label);
如果这样做不正确,您将别无选择,只能子类化
QLineEdit
,仔细检查其绘制事件,确定偏移量的计算位置,并将该值存储在公共成员中,以便从外部访问该值以用于偏移标签

我个人很幸运能得到这个代码:


您是否能够代替使用
QLineEdit
QLabel
来使用两个QLineEdits

考虑以下几点:

QWidget* widget = new QWidget();
// Original line edit
QLineEdit *lineEdit1 = new QLineEdit("999");
lineEdit1->setFixedWidth(100);
lineEdit1->setAlignment(Qt::AlignRight);
lineEdit1->setStyleSheet("border-width: 2px;");
// A suggestion if you want a label
QLabel *label = new QLabel("999");
label->setFixedWidth(100);
label->setAlignment(Qt::AlignRight);
label->setStyleSheet("border: 2px solid rgba(255, 0, 0, 0%)");
// Alternatively if you can use another QLineEdit
QLineEdit *lineEdit2 = new QLineEdit("999");
lineEdit2->setFixedWidth(100);
lineEdit2->setAlignment(Qt::AlignRight);
lineEdit2->setReadOnly(true);
lineEdit2->setStyleSheet("background: rgba(0, 0, 0, 0%);  "
                         "border-width: 2px;              "
                         "border-style: solid;            "
                         "border-color: rgba(0, 0, 0, 0%);");
// Bring it all together
QLayout *layout = new QVBoxLayout(widget);
layout->addWidget(lineEdit1);
layout->addWidget(label);
layout->addWidget(lineEdit2);
widget->show();
它强制所有边界为2px,因此在不同的平台上应该是相同的。第二个
QLineEdit
的外观不应与
QLabel
不同(不过文本颜色看起来比标签颜色稍暗,这可能是一件好事,因为它与原始编辑相匹配)

使用
QLineEdit
而不是
QLabel
的另一个好处是该值现在是可选择的

免责声明:我只在Linux上测试过,没有做过像素级的比较


编辑:我看到不同字体大小的对齐失败。

如您所见,
QLineEdit
使用从边框到文本的分隔符空间(很像
QLayout
中的边距。你可以在
标签周围添加一个布局来模拟这种行为。如果你需要计算精确的填充,你可以检查并考虑边框宽度。这是一个相当简单的代码!因为当qtexted fontsize大于QLabel中的值时,它不起作用这个问题没有答案。