Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3#include <QApplication>
4#include <QTextBlock>
5#include <QTextEdit>
6
7QString tr(const char *text)
8{
10}
11
12int main(int argc, char *argv[])
13{
14 QApplication app(argc, argv);
15 QTextEdit *editor = new QTextEdit;
16
17 QTextDocument *document = new QTextDocument(editor);
18 QTextCursor cursor(document);
19
20 QTextImageFormat imageFormat;
21 imageFormat.setName(":/images/advert.png");
22 cursor.insertImage(imageFormat);
23
24 QTextBlock block = cursor.block();
25 QTextFragment fragment;
27
28 for (it = block.begin(); !(it.atEnd()); ++it) {
29 fragment = it.fragment();
30
31 if (fragment.contains(cursor.position()))
32 break;
33 }
34
36 if (fragment.isValid()) {
37 QTextImageFormat newImageFormat = fragment.charFormat().toImageFormat();
38
39 if (newImageFormat.isValid()) {
40 newImageFormat.setName(":/images/newimage.png");
41 QTextCursor helper = cursor;
42
43 helper.setPosition(fragment.position());
44 helper.setPosition(fragment.position() + fragment.length(),
46 helper.setCharFormat(newImageFormat);
48 }
50 }
52
53 cursor.insertBlock();
54 cursor.insertText("Code less. Create more.");
55
56 editor->setDocument(document);
57 editor->setWindowTitle(tr("Text Document Image Format"));
58 editor->resize(320, 480);
59 editor->show();
60
61 return app.exec();
62}
The QApplication class manages the GUI application's control flow and main settings.
static int exec()
Enters the main event loop and waits until exit() is called, then returns the value that was set to e...
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\reentrant
iterator begin() const
Returns a text block iterator pointing to the beginning of the text block.
\reentrant \inmodule QtGui
Definition qtextcursor.h:30
void setPosition(int pos, MoveMode mode=MoveAnchor)
Moves the cursor to the absolute position in the document specified by pos using a MoveMode specified...
void setCharFormat(const QTextCharFormat &format)
Sets the cursor's current character format to the given format.
\reentrant \inmodule QtGui
The QTextEdit class provides a widget that is used to edit and display both plain and rich text.
Definition qtextedit.h:27
void setDocument(QTextDocument *document)
QTextImageFormat toImageFormat() const
Returns this format as an image format.
\reentrant
bool contains(int position) const
Returns true if the text fragment contains the text at the given position in the document; otherwise ...
bool isValid() const
Returns true if this is a valid text fragment (i.e.
QTextCharFormat charFormat() const
Returns the text fragment's character format.
int length() const
Returns the number of characters in the text fragment.
int position() const
Returns the position of this text fragment in the document.
void setName(const QString &name)
Sets the name of the image.
int main()
[0]
QString text
QCursor cursor
QSet< QString >::iterator it
#define tr(X)
QApplication app(argc, argv)
[0]