Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Setting baseline



  • Hi,

    I have an Item, which contains a TextField and a Text:

    Item {
        property bool displayTextField: true
        baseline: textField.baseline
        TextField {
            id: textField
            visible: displayTextField
        }
    
        Text {
            id: text
            visible: !displayTextField
        }
    
    }
    

    I would like to position this item using its baseline property. I thought in order to do this, I must set its baseline property to be either the baseline of textField or text, but if I try to do so I get the error

    qrc:/test.qml:14 Invalid property assignment: "baseline" is a read-only property
    

  • Moderators

    There is baselineOffset property which is not read-only.

    But what is your goal? Why do you try to use baseline at all?



  • I would like to align the text in the above item (either Textfield or Text depending on displayTextField) with the text of a label on the left of this item. Using top anchors is not working very well, as I use the Material style and then the text is below the label.


  • Moderators

    Perhaps you can use verticalAlignment instead?



  • Yes, but that means playing around a bit, I thought if I want to align the text baselines the obvious solution would be to anchor to baselines


  • Moderators

    @maxwell31 said in Setting baseline:

    Yes, but that means playing around a bit, I thought if I want to align the text baselines the obvious solution would be to anchor to baselines

    But you are not anchoring them. You are trying to assign one value to another. Perhaps you've meant to write:

    Item {
        property bool displayTextField: true
        anchors.baseline: textField.baseline
        // or
        anchors.baseline: textField.anchors.baseline
    

    I've never tried any of this, but maybe it will work.



  • It might be what I meant. I might have a problem of understanding here: Usually we can anchor items to another items baseline. If I design my own Item A, and would like some other Item B to be anchored to item As baseline, I must define somehow for Item A where its baseline is lying, right?


  • Moderators

    @maxwell31 said in Setting baseline:

    It might be what I meant. I might have a problem of understanding here: Usually we can anchor items to another items baseline. If I design my own Item A, and would like some other Item B to be anchored to item As baseline, I must define somehow for Item A where its baseline is lying, right?

    I guess so. I've never used the baseline property myself.


Log in to reply