Get text prior to edit from inside QLineEdit::textEdited() - c++

When I receive a QLineEdit::textEdited() signal, can I see what the text was prior to the edit? I need to compare the text as it was prior to the edit and the text after the edit.
The textEdited() signal has only one argument, which is the new text. Calling QLineEdit::text() also returns the new text.
Right now I can only think of is holding the old text in a QString, and updating that QString each time there is an edit, but then I need to account for programmatic changes (made via QLineEdit::setText()).
Or is there another way to intercept a text change (via validators perhaps) that would allow me to get the text prior to change and after change simultaneously? Preferably for user-changes only (non programmatic).

Right now I can only think of is holding the old text in a QString, and updating that QString each time there is an edit, but then I need to account for programmatic changes (made via QLineEdit::setText())
Granted, I can also only think of that way, but please note that unlike textEdited(), textChanged() would allow you to even catch the programmatic changes as per documentation:
Unlike textChanged(), this signal is not emitted when the text is changed programmatically, for example, by calling setText().

Related

Qt generic widget handling

I am using qt (pretty new to it). I am currently using filing to save user checkbox states. Whenever the user opens that windows file is read and implemented, but there are a lot of checkboxes and I am using a lot of if statements (to load previous states). I am just curious if there is a generic way to do this as I have a lot of windows and checkboxes to handle.
Thank you.
Each QObject instance has a name, available via QObject::objectName, and you can use a combination of the QObject::findChild methods to find all of the QCheckBox instances under whatever parent widget contains all of these. Then you can extract the name and checkbox state to write to the file. When you read the file in, you reverse that process.
The caveat is that the names of your objects need to remain consistent; through the life cycle of your program, if you change a name of a QCheckBox, files containing that name either become invalid, or you have to handle the name conversion. What I would recommend is to use Qt Designer to manually set the names of the QCheckBox instances so that they make sense based on what they represent rather than the automatically-generated names they get otherwise.

QTextEdit: typing in HTML/richttext

I have a QTextEdit and want the user to be able to type rich text which will then automatically be (correctly) shown in the widget (so: formatted).
It works fine when setting the text programmatically (using setText()), but not when manually typed. See picture below.. "Input" is set using setText, the following line is manually typed. I would like this line to automatically be formatted a
What's the (easiest) way to do this? The only way I can think about it to manually catch key events and explicitly set the text as HTML.. But I'm sure there's a better way.
Manual typed html gets escaped, the < will become a < etc . .
You wouldn't be able to edit it if that would not be the case, for obvious reasons.
You could try adding a [render] button or something like that to render the entered text to html. Trying to render on keypress is very dangerous because it makes it terribly inconvenient and counter-intuitive to type something and then have it magically change the output. Also un-finished markup will probably throw a stick in your wheel.
Also pasting from a rich text source (for example a webpage) keeps the formatting.
As "the JinX" already said it will not be so intuitive if you try to capture every key event and then try to change the text to render in HTML.
Though you can use some special key sequences, say "shift+return key" to change the text of current line/entire textedit to to html formatted one.
This is just a suggestion.
In this case more than implementation it is also about what a user will expect.
Changing the text of 1 line/entire textedit from plain to HTML would be easy to achieve as well.

Cancel edit in QTableView when delegate loses focus

I am using a basic QTableView + QAbstractTableModel architecture.
Data can be edited via a QAbstractItemDelegate subclass. Everything works as expected but I need to slightly change the editing behaviour:
Qt default behaviour is:
For an open delegate editor, hitting Enter confirms the edit
For an open delegate editor, hitting Escape cancels the edit
For an open delegate editor, clicking in a different table view cell confirms the edit
I want to get rid of the last one. If the delegate editor loses focus, the edit shall be canceled (i. e. it shall not emit editingFinished()) so that a user can only submit changes by pressing Enter.
Is there a convenient way to do this?
Thanks in advance
Override the class, find out which function that last behaviour is that you dont want, then override that function and just leave the implementation of it empty, then it gets called just as it would usually but now it does nothing (or does something else, you're programming it make it do whatever you want)
I would override the QAbstractItemModel::setData() in your model and skip emitting dataChanged() on attempt to set the same data as it is already existing for the given item.

Saving user's data for my application part 2

My first question was: should I use dom, sax, or sqlite to save the data the user is inputting into my application. The answer I am going with is to use DOM.
My second question is: How should I load the contents of the file into the application when the user decides to open the file? Should it go through the whole file and distribute all the data to the correct spots in the GUI once the user clicks "open" on the file? Or should it only open the stuff up as the user clicks on certain areas?
My third question is: How does qt handle knowing when things have changed? How would i know when the user has changed something and ask them to save the file?
If you do not understand, please let me know and I will try to explain again.
Example:
I am not only reading gui locations.
But the contents of those. For
instance. The user is able to create
tabs that contain edit text boxes. And
those tabs are associated with items
that are in a list. When the user
clicks on an item in the list the user
will be presented with a whole set of
new tabs. And each tab has some
editing forms. The file will need to
contain what is in the list, what tabs
the user has created under each item
in that list and the contents of each
tab associated with the tab of each
item in the list.
Sorry that I posted another question that is similar to my last, but the other question was answered and now I need a new post.
Question 2: This very much depends on how much data you're dealing with. It will be much easier to load everything in one step. If you are expecting complex documents, it might be better to do it incrementally, but I would strongly recommend starting with the simpler approach.
Question 3: Qt does not handle this, except in as far as widgets will fire signals when they are modified. You need to do it, using a model of some sort. You could just use the DOM document directly as the model, although it may help maintainability to abstract the save format. Each change the user makes would cause a change in the model. You will need to detect when e.g. the user edits some text, update your model appropriately and keep track of whether it has changed since the last save.
What do you want to achieve with your solution? If you want to simply set Configuration why not using a simple Ini file (QSettings Class).
I don't know your application, but you should be able to recognise changes (lets say, if the user changed a QLineEdit or hit a radioButton).
There would be also a "sync" method for QSettings, which "rereads" the file you are working with. Qt won't recognise changes itself, you have to do that on your own.

Implementing Undo feature (like Ctrl+Z) in Qt/C++

I am using Qt 4.5 and C++ on Windows XP.
Basically I will be having an UI where the user will enter some data. He can go and modify the values available in the UI. The UI will have basic Qt UI elements like QLineEdit,QTableWidget etc.,
So now, if the user presses Undo button (or Ctrl+Z) the previous value should be retained in the corresponding UI element.
Say, if there is QLineEdit with the text 25. Now the user modifies to 30. Now by clicking Undo, the older value 25 should be retained.
Like the Undo feature that usually available in many applications. Is there any way to do it?
You could use Qt's undo framework.
The typical way of implementing Undo is to represent each action done by the user, and store them. You also want the ability to compute the inverse of a given action.
So, for an insert into a text buffer, the action would store the text inserted, and the location at which the insert happened. The inverse then becomes a delete, at the same location and with the size of the inserted text.
When the user asks the application to undo, simply look at the most recent stored action, and execute its inverse. If you now instead of deleting the "spent" action remember it, too, you can implement Redo by moving the other way in the history of actions.
Note that this is an abstract and generic explanation; as other answers point out, Qt has a framwork in place for implementing Undo already, so you should of course investigate that, first.
You could use the Command Pattern to realize undo/redo
QLineEdit has a built-in undo/redo support, exported as public slots, check : http://doc.trolltech.com/4.7/qlineedit.html#undo