PDA

View Full Version : Design a commandline widget in Qt4?



Ran
5th January 2009, 12:18
Hi all,

I am planning to code a commandline widget using Qt4. I don't know how many people here know Matlab. I want to have a input window just like Matlab has. That is, you can input/modify any text in current line before Enter is pressed. Once it becomes previous line, it is not editable anymore, but only displayed on the widget.

Is QTextEdit capable of doing this? I read through the manual but with no found. Several `search' attempts also returned nothing on our forum. I am now open to any suggestion.

Thank you in advance.

rexi
5th January 2009, 19:34
You could simply use two QTextEdits, one containing the already issued commands and one to edit the current command. Then you can set the first one readonly with QTextEdit::setReadOnly(). To connect the two QTextEdits, have a look at the QTextEdit::append() slot, for example.

wysota
5th January 2009, 20:43
I'd say QLineEdit will be better for the second line. You can also use a single QTextEdit and reimplement keyPressEvent to handle the up key.

rexi
5th January 2009, 22:27
Agreed, QLineEdit would probably be better for the editable one.

But how would handling the up key help (I guess by handle you mean disable)? There's still the "left" key, or the user could click anywhere in the textedit, or shortcuts like Ctrl+A. Just to name a few things that come to my mind to tamper with the textedit's contents. Never underestimate what a user can do with (or to) a program ;)

wysota
5th January 2009, 23:01
The easiest way is to react on the positionChanged() signal and make sure the position is always only on the last line. There is also QTextDocument::cursorPositionChanged() which might be more useful. As for ways to abuse the suggested solution, there is a finite and small number of things the user can do in this situation so it's not hard to guard them all.

Ran
5th January 2009, 23:18
Thanks, guys.

Yes, I can simply use two QTextEdit widget. But I don't feel this is the most nice look for put two QTextEdit together. I might go with wysota's suggestion to handle the positionChanged() signal which, I think, is better for now.

rexi
6th January 2009, 10:28
Just for completeness: you can disable the seperate frames of the textboxes and place them in a single, shared QFrame. That should make them visually indistinguishable.

wysota
6th January 2009, 11:56
Just for completeness: you can disable the seperate frames of the textboxes and place them in a single, shared QFrame. That should make them visually indistinguishable.

The difference will be visible once the scrollbar appears.

Ran
7th January 2009, 04:14
Yes. That's right. It's still visible once it has some content.