1 changed files with 30 additions and 2 deletions
@ -1,14 +1,42 @@ |
|||||||
import QtQuick.Controls 2.0 |
import QtQuick 2.15 |
||||||
|
import QtQuick.Controls 2.15 |
||||||
|
|
||||||
ComboBox { |
ComboBox { |
||||||
id: cb |
id: cb |
||||||
|
|
||||||
|
property int implicitChildrenWidth: 64 |
||||||
|
|
||||||
// make combobox implicit width a multiple of 32, so it aligns with others |
// make combobox implicit width a multiple of 32, so it aligns with others |
||||||
implicitWidth: Math.ceil(contentItem.implicitWidth/32)*32 + constants.paddingXXLarge |
implicitWidth: Math.ceil(implicitChildrenWidth/32)*32 + 2 * constants.paddingXLarge |
||||||
|
|
||||||
// redefine contentItem, as the default crops the text easily |
// redefine contentItem, as the default crops the text easily |
||||||
contentItem: Label { |
contentItem: Label { |
||||||
|
id: contentLabel |
||||||
text: cb.currentText |
text: cb.currentText |
||||||
padding: constants.paddingLarge |
padding: constants.paddingLarge |
||||||
rightPadding: constants.paddingXXLarge |
rightPadding: constants.paddingXXLarge |
||||||
font.pixelSize: constants.fontSizeMedium |
font.pixelSize: constants.fontSizeMedium |
||||||
} |
} |
||||||
|
|
||||||
|
// determine widest element and store in implicitChildrenWidth |
||||||
|
function updateImplicitWidth() { |
||||||
|
console.log('updating implicit width') |
||||||
|
console.log(cb.count) |
||||||
|
for (let i = 0; i < cb.count; i++) { |
||||||
|
var txt = cb.textAt(i) |
||||||
|
var txtwidth = fontMetrics.advanceWidth(txt) |
||||||
|
console.log(txt + ' is ' + txtwidth + ' wide') |
||||||
|
if (txtwidth > cb.implicitChildrenWidth) { |
||||||
|
cb.implicitChildrenWidth = txtwidth |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
FontMetrics { |
||||||
|
id: fontMetrics |
||||||
|
font: contentLabel.font |
||||||
|
} |
||||||
|
|
||||||
|
Component.onCompleted: updateImplicitWidth() |
||||||
|
onModelChanged: updateImplicitWidth() |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue