tweak history prefix search (up/^p) in qtconsole

moving the cursor around the line could result in weird inconsistencies in the prefix.

This simplifies the logic by always using the cursor position to set the history prefix, and better determine when the history prefix has actually changed.

also fixes a small typo where a while-loop was always checking the initial value, rather than the iterator, which could result in an IndexError at the end of the history.

closes #2485