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

ComboBox onActivate: Text value not updated yet



  • Hi,

    So I have a combo box. I didn't want the slot to be called when the value is updated when setting currentIndex, so I used onActivate over onCurrentTextChanged.

    The problem is that when I access currentText, I find that the value is old. In essence, assume I have a combo box with values A,B and C. The current value is 'A'. When I select 'B', the value sent to my function (which is using currentText) displays 'A'. Setting the value to something else will then forward the value 'B' no matter if I choose 'A' or 'C'.

    Anyway around this? Some other slot or something I can call for the value to be changed?


  • Qt Champions 2017

    Can you give simple sample & show the issue on what you are trying to do ? We are using the onActivated Signal & getting the currentText properly. So simple program would help to help you.



  • ComboBox {
       id: someId
       x: 100
       y: 100
       width: 150
       model: ["A" , "B", "C"]
       onActivated: { foo(currentText) }
       currentIndex: <c++ variable which is exported as a Q_PROPERTY>
    }
    

    Here, I am using onActivated over onCurrentTextChanged because I don't want the binding on changeIndex to be calling foo(). But I find that when I put a print in foo(); that onActivate is called before the text is changed. This leads to currentText having stale data.

    I can technically do something like:

    property bool activatedBoolean: false
    onActivate: activatedBoolean = true
    onCurrentTextChanged: { 
          if(activatedBoolean) {
              activatedBoolean = false
              foo(currentText) 
          }
    }
    

    But, I can't be sure if onActivated will always be called before onCurrentTextChanged which can result in abnormal behavior.

    P.S. I am using Qt v5.6


  • Qt Champions 2017

    Let me try to understand your issue.

    1. You have combobox with A,B,C
    2. You are handling the onActivated signal
    3. Current selection is A.
    4. You select the B in ComboBox.
    5. Now onActivated handle is triggered.
    6. Now currentText prints A. But your expectation is it should print B.

    Is that the issue are taking about ? So you want the value B ?. Is that the expectation ?
    If you can help me what you wanted on onActivated. Do you want the currentText to changed B or you just need to value based on the current selection ? Please note we are not worried what&why you are doing etc.



  • @dheerendra Yes, that's exactly what I'm talking about.


  • Qt Champions 2017

    Use the model to take the value. Take the following example.

             ComboBox{
                model :["D","E","F"]
                 onActivated: {
                    console.log(index)                
                    console.log(" Current Text Value = "+model[index])
                }
            }
    


  • @dheerendra Thanks for that.

    I'd tried something similar using currentIndex before and it gave the same bug. I find it odd that the object carries multiple indexes (index and currentIndex).


  • Qt Champions 2017

    ComboBox does not have multiple indexes properties. It just has currentIndex as property. Thats it.

    1. onActivate(index) signal is just to tell that things are getting changed.
    2. Index is not a property of Combobox.
    3. currentIndex is property.
    4. index is coming as argument to signal. It is just telling which location(index) is getting activated.

    Based on this activity & argument(index) rest of the things are getting changed. i.e currentText & currentIndex.


Log in to reply