Search code examples
qtqcombobox

Simple connection between an int and QComboBox?


Somehow I feel really silly to ask this question, but I just spent 2 hours reading this site and other sources of information, and I only find complicated solutions, while I strongly believe there must be a really simple one for my problem.

In TCL/TK for example, I can define a variable and bind it to a combobox in 2 lines of code. Similarly in Qt/C++, I have an int defined in one of my classes, and I want the QCombobox to set that int to the number (index) of the selected element.

Do I really have to delve in the depths of model/view and its classes to achieve this?


Solution

  • All you need is a slot somewhere that is connected to the combo box's currentItemChanged(int) signal. The slot can update your int variable, or do whatever it likes with the selected index. (Look at QComboBox's other signals, there are alternatives.)

    (That's one line for the connect, and 1, 2 or 3 lines for the slot depending on how you count that.)