Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to create a button to underline selectedText in QTextEdit
Forum Updated to NodeBB v4.3 + New Features

How to create a button to underline selectedText in QTextEdit

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    beckstar101
    wrote on last edited by
    #1

    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);
    }@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        beckstar101
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • B Offline
            B Offline
            beckstar101
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • B Offline
                B Offline
                beckstar101
                wrote on last edited by
                #7

                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);
                }
                }
                }

                }
                @

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved