Подскажи пожалуйста как отцентровать timeLabel текст внутри timeContainer? Вот код на…
- User
Подскажи пожалуйста как отцентровать timeLabel текст внутри timeContainer? Вот код на QML.
RowLayout { anchors.fill: parent anchors.margins: 2 spacing: 10
Item { Layout.preferredWidth: csLogo.width + csText.width Layout.alignment: Qt.AlignLeft Layout.leftMargin: 10 Image { id: csLogo anchors.verticalCenter: parent.verticalCenter source: "qrc:/assets/icons/chargeSpyLogo50.svg" sourceSize { width: 40 height: 40 } } Text { id: csText text: "Charge Spy" anchors { verticalCenter: parent.verticalCenter left: csLogo.right leftMargin: 8 } font { pointSize: 16 family: Theme.chargeSpyLogoFont bold: true capitalization: Font.AllUppercase } color: "#1F1F1F" } } Item { id: filler_1 Layout.fillWidth: true Layout.minimumWidth: 4 } Item { id: safetyButtonInd Layout.preferredWidth: warnImgLeft.width + safetyButtonText.contentWidth + internetStatusIcon.width Layout.preferredHeight: warnImgLeft.height Layout.alignment: Qt.AlignHCenter visible: false RowLayout { Image { id: warnImgLeft source: "qrc:/assets/icons/warning.svg" } Text { id: safetyButtonText font { pointSize: 14 } text: qsTr("Safety button pressed!") } Image { id: warnImgRight source: "qrc:/assets/icons/warning.svg" } Timer { running: Backend.safetyButtonPressed interval: 1000 repeat: true onTriggered: { safetyButtonInd.visible = !safetyButtonInd.visible } onRunningChanged: { safetyButtonInd.visible = running } } } } Item { id: filler_2 Layout.fillWidth: true Layout.minimumWidth: 4 } ToolButton { id: internetStatusIcon Layout.alignment: Qt.AlignRight Layout.rightMargin: 10 icon.source: "qrc:/assets/icons/no_internet.svg" } ColumnLayout { spacing: 2 Layout.alignment: Qt.AlignRight Layout.rightMargin: 10 Layout.fillHeight: true Item { id: timeContainer Layout.preferredWidth: timeLabel.contentWidth Layout.preferredHeight: timeLabel.contentHeight Text { id: timeLabel font.pointSize: 12 anchors { left: timeContainer.left right: timeContainer.right bottom: timeContainer.bottom top: timeContainer.top } horizontalAlignment: Text.AlignHCenter text: "21:48" } } Item { id: dateContainer Layout.preferredWidth: dateLabel.contentWidth Layout.preferredHeight: dateLabel.contentHeight Text { id: dateLabel anchors { left: dateContainer.left right: dateContainer.right } font.pointSize: 12 text: "15.07.2025" } } } } - Assistant
В вашем случае
timeLabelрастянут по всем четырём сторонам (anchors { left; right; top; bottom }), а самtimeContainerимеет ширину, равную ширине текста (Layout.preferredWidth: timeLabel.contentWidth).
По-этому «центрировать» уже просто нечего: ширина контейнера = ширина текста → текст и так заполняет весь контейнер.Сделайте наоборот:
- дайте контейнеру возможность занять доступную ширину,
- поместите сам текст ровно в центр контейнера.
Самый короткий вариант —
anchors.centerIn:Item { id: timeContainer Layout.fillWidth: true // контейнер тянется на всю ширину колонки height: timeLabel.implicitHeight // высота по тексту Text { id: timeLabel text: "21:48" font.pointSize: 12 anchors.centerIn: parent // горизонталь + вертикаль horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter } }Если контейнер вам вообще не нужен, можно обойтись без него и воспользоваться свойством
Layout.alignmentсамогоText(работает внутриColumnLayout):Text { id: timeLabel text: "21:48" font.pointSize: 12 Layout.alignment: Qt.AlignHCenter // центр по горизонтали }Любой из этих вариантов отцентрирует время так, как вы хотите.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.