Skip to content
  • 0 Votes
    4 Posts
    46 Views
    JonBJ

    @Khamza
    First to answer your question. Your new code delays the update till after the text edit has been shown. In that sense it is similar to the QTimer approach. For unknown reason you are claiming the code does not work correctly until after the text edit has been shown.

    There are cases in Qt which this is necessary. In particular sizes of widgets are not calculated till they are actually shown, so code which requires to know a size is often delayed in one of the above two fashions.

    HOWEVER I was never convinced by your assertion "The function itself is called but neither text is inserted nor block format changed:". While I could believe that possibly an operation on textCursor() might require the text edit to be shown I never thought that insertPlainText() for sure would depend on that. It should be callable any time, including e.g. during construction.

    I have now had time to create a minimal repro. Here are the 3 files:

    #include "passwordshowarea.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); PasswordShowArea w; w.show(); return a.exec(); } #ifndef PASSWORDSHOWAREA_H #define PASSWORDSHOWAREA_H #include <QTextEdit> class PasswordShowArea : public QTextEdit { Q_OBJECT public: PasswordShowArea(QWidget *parent = nullptr); private: void init(); void updateBlockFormat(); }; #endif // PASSWORDSHOWAREA_H #include <QAbstractTextDocumentLayout> #include <QTimer> #include "passwordshowarea.h" PasswordShowArea::PasswordShowArea(QWidget *parent) : QTextEdit(parent) { init(); //doesn't work updateBlockFormat(); // works - gpt4 suggest // QTimer::singleShot(0, this, &PasswordShowArea::updateBlockFormat); } void PasswordShowArea::init() { // QObject::connect(document()->documentLayout(), &QAbstractTextDocumentLayout::update, // this, &PasswordShowArea::updatePasswordShowArea); setTextColor(palette().color(QPalette::Text)); } void PasswordShowArea::updateBlockFormat() { insertPlainText("Some text"); QTextBlockFormat fmt = textCursor().blockFormat(); fmt.setTextIndent(20); fmt.setLineHeight(fontMetrics().height() * 2, QTextBlockFormat::LineDistanceHeight); fmt.setBackground(Qt::red); textCursor().mergeBlockFormat(fmt); }

    I have made couple of tiny tweaks where I did not have all of your code. I added fmt.setBackground(Qt::red); so that we had something to see. And here is what I get:

    Screenshot 2024-12-21 093554.png

    You can see that not only do I get the inserted text but I do also get the QTextBlockFormat changes. Identical if I change it to only call updateBlockFormat() on the delayed timer. Ubuntu 24.04, Qt 6.4.2.

    Sooo.... I suggest you try just this code.

  • 0 Votes
    6 Posts
    107 Views
    I

    @Khamza It is quite hard to tell. The piece of code you pasted has several issues unfortunately, and it would very hard to say what the exact cause it without the whole thing to reproduce.

    I'll say that the most pressing problem in the code is that you are using values that are in document coordinates (which is what the rectangle that QAbstractTextDocumentLayout::blockBoundingRect returns is in) to calculate parameters for a QPainter which works in viewport coordinates. These are not the same, especially when there are scroll bars shown, and in my experience the main cause of issues around scrolling.

    That said, it wouldn't explain the text itself just disappearing when scrolling back up; I'd expect it to just be garbled. First try to comment out your paintEvent to see if text is drawn correctly at the expected positions when scrolling back and forth - perhaps something in the formats isn't right. Otherwise, please post a complete yet minimal project that reproduces the problems you see.

  • 0 Votes
    4 Posts
    75 Views
    JonBJ

    @Khamza
    You are not supposed to just call paintEvent() from somewhere else, it's designed to be called during actual painting. If you override a base paintEvent() you can (and should) indeed call the base implementation from there.

  • 0 Votes
    10 Posts
    539 Views
    SGaistS

    @Pl45m4 nope, I understood ;-) I wanted to know why additional letter was needed since it's not how QSyntaxHighlighter works as you correctly explained.

  • 0 Votes
    11 Posts
    968 Views
    T

    I searched this a bit more.

    First of all the behavior might be depending on OS. (Noticed some differences on Unix vs Windows)

    Secondly, I created a timer that every 2 seconds prints out qApp->focusObject(). It looks like after a QEvent::ApplicationStateChange and a QEvent::WindowDeactivate that all the widgets receive afterwards, the qApp->focusObject() is always nullptr. And after nullptr Qt selects the widget on MainWindow that was the last widget to have focus, and gives focus there.

    So I feel that subwindows are ignored somehow...

    I am leaving this open for a bit, in case someone has a good solution to track if a subwindow had focus, it should regain it after application re activation.

  • QTextEdit HTML font issue

    Unsolved General and Desktop
    5
    0 Votes
    5 Posts
    1k Views
    L

    @Soren-Juul
    I know this post is old, but ...

    I had a similar problem changing the font on a QtextCursor in QTextEdit
    I solved it by using setFontFamilies instead of setFontFamily

    here a snip of my solution:

    QTextCursor cursor = ui->textEdit->textCursor(); QTextCharFormat current = cursor.charFormat(); QStringList ffamily; ffamily.append(font); current.setFontFamilies(ffamily); cursor.setCharFormat(current);
  • 0 Votes
    4 Posts
    1k Views
    M

    Thank buddy for your searching ! But could you please share how do you implement in mainwindow?

    I am using a qtextedit in my mainwindow where I want to zoom and when I implement by creating an instance of this class i am getting errors.

    Thank you.

  • 0 Votes
    4 Posts
    372 Views
    J

    Oh.. I figured it out.
    That's just a simple unit conversion error.

    I used millimeter instead of pixel...

    >>> # Python >>> __editor.mmMarginLeft() 20 >>> __editor.pxMarginLeft() 110 >>> __editor.mmMarginLeft() == __editor.pxMarginLeft() False

    Now it's solved! THANKS a lot!
    Screenshot 2022-11-27 at 4.29.45 PM.png

  • 0 Votes
    8 Posts
    812 Views
    T

    @tapsbrighton This is a better solution rather than append:

    insertPlainText("\n");
  • 1 Votes
    3 Posts
    527 Views
    cristian-adamC

    This looks similar to what https://forum.qt.io/topic/141297/exception-about-qt-creator-ui-on-ubuntu experienced.

    Also on VMware.

    I assume it has something to do with the graphics drivers. At https://www.qt.io/blog/qt-6.4-released we can see the topic:

    QQuickWidget with full RHI support

    With QQuickWidget you can build application user interfaces where Qt Quick and Qt Widget elements coexist. Until now this required using OpenGL. For Qt 6.4 we redesigned the class from the ground up, and now QQuickWidget is functional with all supported graphics APIs: Metal, Vulkan, Direct3D 11, in addition to OpenGL.

    Can you post the Graphics configuration of your VMware player? Aso a bug report at https://bugreports.qt.io/ wouldn't hurt either.

  • 0 Votes
    3 Posts
    1k Views
    A

    @raven-worx Thanks a lot for the quick response! I'll look into it.

  • 0 Votes
    1 Posts
    341 Views
    No one has replied
  • 0 Votes
    4 Posts
    536 Views
    jsulmJ

    @Mr-Workalot But what location do you need? Where do you want to store the file? It is really not clear from your description.
    Did you check the link I provided: https://doc.qt.io/qt-5/qstandardpaths.html ?
    Example: to store in Documents folder call https://doc.qt.io/qt-5/qstandardpaths.html#writableLocation with QStandardPaths::DocumentsLocation as parameter

  • 0 Votes
    12 Posts
    1k Views
    ChabaC

    @SGaist

    My data is ready to append. So i append them directly. There are 10-100 QTextEdits. I append 100-200 line to each of QTextEdits. Lines have arround 300 characters. I have one vector of vectors source.

    By the way, i moved my data source to a different thread. Then emitted signals to append with BlockingQueuedConnection. Gui is responsive now. But its not smooth. I dont know using blockingqueuedconnection instead of queuedconnection is the right way but my gui is responsive while appending strings now.

  • 0 Votes
    1 Posts
    248 Views
    No one has replied
  • 0 Votes
    8 Posts
    4k Views
    R

    @jonb Yes, that is the document to which I was referring. Thank you for posting the link!

  • 0 Votes
    8 Posts
    2k Views
    J.HilkJ

    @legitnameyo

    QString str = textEdit->toPlainText(); QString targetChars = str.mid(textEdit->textCursor().columnNumber() -2 ,2); qDebug() <<targetChars;
  • 0 Votes
    13 Posts
    2k Views
    L

    The following partially works and I post in the hopes that someone might get an idea that will fix my issue :)

    bool ZoomCode::event(QEvent *event) { if (event->type() == QEvent::NativeGesture) { return gestureEvent(static_cast<QNativeGestureEvent*>(event)); } return QWidget::event(event); } bool ZoomCode::gestureEvent(QNativeGestureEvent *e) { float value = e->value(); textEdit->setFontPointSize(NULL); // zoom does not work without this textEdit->selectAll(); // some part of the text has to be selected before the zoom in the text starts to work. It does NOT matter if it is the WHOLE text or just a character in the text, just as long as something gets highlighted. Afterwards, nothing has to be highlighted again for the zoom to work. The zoom works on the whole text, no matter what part has been highlighted. if(value >0) { zoomIn(value + 5); // + 5 to add some zoom strength } else if(value < 0) zoomOut(value - 5); // -5 to add some zoom strength } }
  • 0 Votes
    16 Posts
    3k Views
    L

    Yes, and works perfectly