你能代替使用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'的邊距。你可以在'label'周圍添加一個佈局來模擬這種行爲 – Zaiborg
有一個看 http://doc.qt.io/qt-4.8/qlabel.html#indent-prop – Ankur
如果你需要計算一個精確的填充,你可以檢查[QLineEdit :: textMargins()](http:// doc .qt.io/qt-5/qlineedit.html#textMargins)加上考慮邊框寬度。 – ymoreau