I'm quite new on Qt softw.
I would like to make a small program, for my internship.
I've made a textedit object for write a MAC Address (17 chars max so.).
I want that after writing 2 chars, when you write the third one, it automatically put a : before write the third char.
DC:R
^
so when you press R it puts before automatically the :.
I Hope I'm clear.
use QLineEdit instead of QTextEdit and use setInputMask function of QLineEdit to set a format;
setInputMask("HH:HH:HH:HH:HH:HH;_");
Take a look at here
Related
I am building a part of a tower defense game in a strictly console environment and i am stuck at the moving of a creature lets say "c", i would like the letter "c" to start on the left and move a space at a time to the right on the same line basically:
c (one second later)
c (one second later)
c and so on....
i thought that this could be implimented with an array but am lost, i want to be able to use simple code, not weird libraries and weird methods, just simple as possible. Thank you
One method is display all the characters, then a carriage return ('\r') and then reprint the line.
This allows you to "walk" characters across. This will only work on video terminals that do not advance a line upon receiving a CR.
Another method would be to print 10 backspace characters, a space, then your 10 'c'. This may not be as fast as the carriage return method above, but worth looking at.
As others have said, you may want to look into a terminal library such as ncurses. The library allows you to position the cursor on the screen, based on the terminal type. This may require setting up the console window to emulate a terminal.
I've got problem with following task. I need to load output txt data from other program to my one and search it for following string:
"zhi": 97.92716217041016,
and especially numerical value (97.92...). The "," sign is separator between other exported values.
I was trying to deal with that in c++ builder following way:
1. read file
2. load lines as strings
3. find position of zhi
4. add 6 to position - this will point on numbers
5. delete everything before new pointer
6. delete everything after 15 char
I know there have to be some easier way but I'm beginner with c++.
Can someone guide me what function I can use to find numbers between "zhi": and , ?
Use std::getline to read file as std::string types.
Use string::find to get the position of the text after "zhi":.
Use string::find to get the position of the ','.
Use string::substr to get a copy of the string between the two positions.
Remember, modifying files in the middle is difficult, especially if you have to delete or insert more characters.
The traditional method of modifying files is:
1. Copy all unchanged content from original file to new file.
2. Write changed text to new file.
3. Write remaining unchanged text to new file.
4. Close all files.
I am using a QTextEdit in my C++ GUI application, I use textEdit->append(byteArray); to add some text, unfortunately append() adds a new line character at the end that I would like to remove after each call of append(). I know I could use insertPlainText() which does not add a new line character but it uses a lot more memory when dealing with big documents.
Thanks for your help!
Since the documentation for QTextEdit::insertPlainText says
It is equivalent to
edit->textCursor().insertText(text);
I would assume that you can just do something like
edit->textCursor().deletePreviousChar();
If you need to you can first clear any selection with
edit->textCursor().clearSelection();
I'm extending a QPlainTextEdit.
When I double click on a word containing a pipe char ex : {"foo"|upper|reverse}
the whole text is surrounded.
I'd like to exclude the pipe char "|" from the selection and don't know what to do
Is there a way to change the behavior of QTextCursor::WordUnderCursor?
I'd like that char to act the same as a space or more generally as an unselectable char.
As stated in QT docs:
Selects the word under the cursor. If
the cursor is not positioned within a
string of selectable characters, no
text is selected.
Currently there is no official way to change the way a text edit finds the word boundaries. See http://bugreports.qt-project.org/browse/QTBUG-150.
You may use their private API to change the behaviour of QTextEngine::atWordSeparator. This way is not recommanded by Qt. The pipe is recognized as word separator in 4.6 but not in 4.5.1 or earlier. I would suggest to update your Qt version, if that is an option. Otherwise you may give your QTextEdit a new QTextLayout with a modified QTextEngine.
I'm rendering some HTML in a QT QLabel. The HTML looks like this:
<pre>foo\tbar</pre>
(note that I've put "\t" where there is a tab chracter in the code).
This renders fine, but the tab character appears to be rendered as eight spaces, whereas I want it to be redered as 4. How can I change this without having to change the source HTML?
According to W3 (HTML4):
The horizontal tab character (decimal 9 in [ISO10646] and [ISO88591]) is usually interpreted by visual user agents as the smallest non-zero number of spaces necessary to line characters up along tab stops that are every 8 characters. We strongly discourage using horizontal tabs in preformatted text since it is common practice, when editing, to set the tab-spacing to other values, leading to misaligned documents.
It's implementation-defined, essencially. Most, if not all, browsers/renderers use eight spaces for tabs. This cannot be configured in Qt.
It is, however somewhat trivial to go through your HTML and replace tabs with however many spaces you wish. Write a simple parser for that. Pseudocode:
for each <pre> block {
for each line in block {
position_in_line = 0
for each character in line {
if character is a tab {
remove tab character
do {
add a space character
++position_in_line
} while position_in_line % 8 != 0
} else {
++position_in_line
}
}
}
}
In case you're curious, HTML3 specifies the use of eight-character tabs:
Within <PRE>, the tab should be interpreted to shift the horizontal column position to the next position which is a multiple of 8 on the same line; that is, col := (col+8) mod 8.
While QLabel uses a QTextDocument internally when rendering rich text, it does not allow access to it in it's API. However, since QTextDocument is a QObject, you can try to use
QTextDocument * tl = label->findChild<QTextDocument>();
to get access to it (will work if the QLabel creates the QTextDocument as a (direct or indirect) child of itself).
Once you have a pointer to the text document, you can use the QTextDocument API, e.g. QTextOption::setTabsStop(), to change the tab stops.
The last step is to somehow make the QLabel repaint itself. Probably a call to QWidget::update() suffices, but caching (or worse, recreating the text document) might thward that. In this case, you can register an event listener on the label to adjust the text document just prior to a paintEvent(), but note that the sizeHint() might also change when the tab stops change, so it might be more complicated still.
That said, it's how I'd approach the problem.
Try this:
<pre style="tab-interval:0.5in">foo\tbar</pre>
Could work