How to create a button to underline selectedText in QTextEdit



  • Hello I have a QTextEdit in my Programm which I want to format by Buttons.
    My first button shall underline the selectedText. I tried several ways but I always got 2 main problems.

    1. The selectedText gets underlined, but all chars I writer after will be still underlined.
    2. It underlines with pressbutton and deunderlines with releasebutton

    My goal is to underline selectedText by pressingbutton
    and setfontunderline=false afterwards by releasing the button, without de-underline the selected text.
    cursor.clearselection() doesn't work.
    Do I need to find another way to do this or do I just need another way of clearselection?

    in the constructor:
    @ QTextCursor cursor = ui.textfeld->textCursor();
    ui.textfeld->setTextCursor(cursor);
    ui.textfeld->ensureCursorVisible();
    @

    @bool Hauptfenster::eventFilter(QObject *obj, QEvent *event)
    {
    if (obj == ui.bunterline && event->type() == QEvent::MouseButtonPress)
    {
    ui.textfeld->setFontUnderline(true);
    }
    if (obj == ui.bunterline && event->type() == QEvent::MouseButtonRelease)
    {
    cursor.clearSelection();
    ui.textfeld->setFontUnderline(false);
    }

    return QObject::eventFilter(obj, event);
    }@


  • Lifetime Qt Champion

    Hi,

    You should find what you need in this "example":http://doc.qt.io/qt-5/qtwidgets-richtext-textedit-example.html



  • I tried to run it in in visual studio but did not find out where to place th files?


  • Lifetime Qt Champion

    Do you mean you tried to open the project in visual studio ?



  • yes. Sorry Im learning QT and c++ at the same time

    btw.

    I changed my code with snippets from the example, but it doesnt work correctly. it's buggy.

    line in constructor
    @ cursor = ui.textfeld->textCursor();@

    1.Method de-underline
    2.Method underline
    3.Method help Method from example
    @
    void Hauptfenster:: b_no_more_underline()
    {
    QTextCharFormat fmt;
    fmt.setFontUnderline(false);
    mergeFormatOnWordOrSelection(fmt);

    }
    void Hauptfenster:: b_underline_pressed()
    {
    QTextCharFormat fmt;
    fmt.setFontUnderline(true);
    mergeFormatOnWordOrSelection(fmt);

    }
    @
    @

    void Hauptfenster::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
    {
    QTextCursor cursor = ui.textfeld->textCursor();
    if (!cursor.hasSelection())
    cursor.select(QTextCursor::WordUnderCursor);
    cursor.mergeCharFormat(format);
    ui.textfeld->mergeCurrentCharFormat(format);
    }
    @

    The problem is that it is buggy as hell

    For example: I write something, double click on it with mouse for selection and click underline button after that. It gets underlined, but the other chars I write after that steps get underlined too..

    So this function is not good for only underline MouseselectedChars

    Other example:

    I Write something and Click underlineButton without marking chars with mouse. Then the written gets marked
    and what I write afterwards gets marked too.

    So this function also isnt a function who operators from the time of clicking Underline till clicking de_Underline. Because it also underlines the chars which where written before.

    It looks like can write something in one line, and afterwards formatting the hole line underlined or de-underlined. BUT
    If you write in a line 20 chars and underline then afterswards, Select char 3-7 and press de-underline, these chars are suddenly de-underlined.

    Im so confused I dont know what I shall do. I dont get in any rule which operates every time. Pls help :D Im sitting here isnce about 6 hours with this function.


  • Lifetime Qt Champion

    Since you are beginning everything, you should rather user Qt Creator, it will be more simple to get started and it'll be easier to handle Qt projects.



  • I solved my Problem with a function which starts every time the cursor posittion changes :

    I know many people did not understand what my problem is, maybe it gets clear with my solution.

    @void Hauptfenster::cursorchanged()
    {

    QTextCursor cursor = ui.textfeld->textCursor();
    QTextCharFormat format;
    if(!cursor.charFormat().font().underline())

    {
    if (test1==true)
    {
    test1=false;
    QColor col = Qt::white;
    if(col.isValid()) {
    QString qss = QString("background-color: %1").arg(col.name());
    ui.bunterline->setStyleSheet(qss);
    }}
    }
    else
    {
    if (test1==false)
    {
    test1=true;
    QColor col = Qt::red;
    if(col.isValid()) {
    QString qss = QString("background-color: %1").arg(col.name());
    ui.bunterline->setStyleSheet(qss);
    }}}

    if(!cursor.charFormat().font().bold())
    {
    if (testfett==true)
    {

    testfett=false;
    QColor col = Qt::white;
    if(col.isValid()) {
    QString qss = QString("background-color: %1").arg(col.name());
    ui.bFett->setStyleSheet(qss);}}

    }
    else
    {
    if (testfett==false)
    {

    testfett=true;
    QColor col = Qt::red;
    if(col.isValid()) {
    QString qss = QString("background-color: %1").arg(col.name());
    ui.bFett->setStyleSheet(qss);
    }
    }
    }

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.