Page 1 of 1

qtcreator: how to write in a text frame widget by the program? [solved]

Posted: Fri Oct 04, 2019 9:19 am
by dsyleixa123
qtcretor/designer:
how to write in a text frame widget by the program? (no extra window/no extra box)
It should be integrated into the main window, just like labels or buttons, and should be able to scroll automatically for either new line, feat. a vertical and a horizontal scroll bar.

Re: qtcretor: how to write in a text frame widget by the program?

Posted: Fri Oct 04, 2019 12:13 pm
by dsyleixa123
PS,
I can d+d TextEdit or PlainTextEdit to my form, but I have no idea how to
ui->PlainTextEdit->TextColor(black);
ui->PlainTextEdit->Writeln("Hello World");

Re: qtcretor: which is the name of a text frame widget where one can write and writeln text messages by the program?

Posted: Fri Oct 04, 2019 1:43 pm
by procount
ui->PlainTextEdit->setStyleSheet("{color: black}"); https://doc.qt.io/archives/qt-4.8/style ... mples.html
ui->PlainTextEdit->setPlainText("Hello World"); https://doc.qt.io/archives/qt-4.8/qplai ... mbers.html

Re: qtcretor: how to write in a text frame widget by the program?

Posted: Fri Oct 04, 2019 2:35 pm
by dsyleixa123
hi,
thanks, I now get 1 single "Hello Word"
But if I do that repeatedly, I do not get many of them, one after the other

Code: Select all

Hello WordHello WordHello WordHello WordHello WordHello WordHello WordHello Word
and also not beneath each other liike using writeln("Hello Word"), i.e. seperated by a "\n"

Code: Select all

Hello Word
Hello Word
Hello Word
Hello Word
Hello Word
also if I try instead "Hello World\n"
theres only just 1 single

Code: Select all

Hello Word
and that's it :roll: :roll: :roll:

Re: qtcretor: how to write in a text frame widget by the program?

Posted: Fri Oct 04, 2019 3:08 pm
by dsyleixa123
just found out:
ui->PlainTextEdit->appendPlainText
makes line feeds in between


but how to write anything just behind one another?

Re: qtcretor: how to write in a text frame widget by the program?

Posted: Fri Oct 04, 2019 4:40 pm
by dsyleixa123
weird that there is no simple print/println funcion available, preferably formattable by stdio sprintf parameters, and automatically writing 1 string behind another... :(

Re: qtcretor: how to write in a text frame widget by the program?

Posted: Sat Oct 05, 2019 1:53 am
by Paeryn
There is insertPlainText() which inserts the text at the current cursor position (if the cursor is a selection then the selection is replaced with the text). The edit box's current cursor can be obtained with

Code: Select all

EditBox->textCursor()
Infact insertPlainText() is a convenience function which does

Code: Select all

EditBox->insertPlainText(text_to_insert);
// is equivalent to
EditBox->textCursor().insertText(text_to_insert);
TextCursor is of the type QTextCursor and has all the methods for defining where in the edit field the cursor is at.

Re: qtcreator: how to write in a text frame widget by the program?

Posted: Sat Oct 05, 2019 8:03 am
by dsyleixa123
thank you!

as my EditBox is called "PlainTextEdit", so IIUC: one needs 2 commands to write 1 string after the other

for write in PlainTextEdit (flowing text)

Code: Select all

#define Qwrite(str)  { ui->PlainTextEdit->textCursor(); ui->PlainTextEdit->insertPlainText(str); }

for writeln (\n) in PlainTextEdit (wordwrap):

Code: Select all

#define Qwriteln(str)   { ui->PlainTextEdit->appendPlainText(str); }
cumbersome - but correct?

Now perhaps sort of an extra gotoxy(x,y) to move the cursor to (x,y) would be nice 8)

Re: qtcretor: how to write in a text frame widget by the program?

Posted: Sat Oct 05, 2019 9:09 am
by Paeryn
dsyleixa123 wrote:
Sat Oct 05, 2019 8:03 am
thank you!

as my EditBox is called "PlainTextEdit", so IIUC: one needs 2 commands to write 1 string after the other

for write in PlainTextEdit (flowing text)

Code: Select all

#define Qwrite(str)  { ui->PlainTextEdit->textCursor(); ui->PlainTextEdit->insertPlainText(str); }
You don't need the ui->PlainTextEdit->textCursor(); here, it just returns the QTextCursor but you throw that away (you don't assign it to a variable or use it in any way). PlainTextEdit->insertPlainText() calls the insertText() method of PlainTextEdit's QTextCursor for you.
dsyleixa123 wrote:
Sat Oct 05, 2019 8:03 am
Now perhaps sort of an extra gotoxy(x,y) to move the cursor to (x,y) would be nice 8)
There are quite a few methods you can call on a QTextCursor to move it to your desired location within the contents of the edit box, see https://doc.qt.io/qt-5/qtextcursor.html

Re: qtcreator: how to write in a text frame widget by the program?

Posted: Sat Oct 05, 2019 9:21 am
by dsyleixa123
thank you for your explanations!
Also thank you for your link (I actually knew that link already), but those tons of stuff are conpletely overburdening and confusing... :roll:

Re: qtcreator: how to write in a text frame widget by the program?

Posted: Sun Oct 06, 2019 7:00 pm
by dsyleixa123
IIUC, there are only relative move actions (up, down, left, right,...), no absolute (x,y) for (rownumber, linenumber)
(enum QTextCursor::MoveOperation)

Re: qtcreator: how to write in a text frame widget by the program?

Posted: Sun Oct 06, 2019 9:34 pm
by Paeryn
You can move to an absolute position within the document with

Code: Select all

QTextCursor::setPosition(int pos, QTextCursor::MoveMode m = MoveAnchor)
Setting it to an arbitrary line and column (assuming by line y you mean block y and by column x you mean xth character of block y) then you could always do this to position the cursor at (column x, line y) assuming both line and column are 1-based (i.e. start at 1 rather than 0)

Code: Select all

    QTextCursor cursor = ui->plainTextEdit->textCursor();
    cursor.movePosition(QTextCursor::Start);
    if (y > 1)
         cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, y-1);
    if (x > 1)
        cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, x-1);
    ui->plainTextEdit->setTextCursor(cursor); // move the edit's cursor to the new position
    ui->plainTextEdit->insertPlainText("*");
I moved the widget's cursor at the end so that plainTextEdit->insertPlainText() will insert at the selected position.
movePosition() will return false if it fails to make the requested number of movements (like reaching end-of-document).

Re: qtcreator: how to write in a text frame widget by the program?

Posted: Mon Oct 07, 2019 7:45 am
by dsyleixa123
thank you very much!
It's not urgent for me right now but I'll keep it in mind if I once will need a text spreadsheet, but ftm it's too complicated tbh...