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 implement the clone formatting functionality of a word processor?

How to implement the clone formatting functionality of a word processor?

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 835 Views
  • 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.
  • N Offline
    N Offline
    ntos
    wrote on last edited by
    #1

    Hi,
    How do you implement this feature?
    I have tried to do it this way:

    1. The easy part: Creating a clone action to copy the format of a piece of text and storing the format in global variable to be used later.
    2. This is the part that I haven't been able to do: selecting another piece of text and after releasing the left mouse button, the format is applied.

    I'd appreciate it if you could help me to do part 2.

    JonBJ 1 Reply Last reply
    0
    • N ntos

      Hi,
      How do you implement this feature?
      I have tried to do it this way:

      1. The easy part: Creating a clone action to copy the format of a piece of text and storing the format in global variable to be used later.
      2. This is the part that I haven't been able to do: selecting another piece of text and after releasing the left mouse button, the format is applied.

      I'd appreciate it if you could help me to do part 2.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @ntos
      Well, at some level you have to override a mouseReleaseEvent(), get the QTextCursor selection and apply the desired format to it.

      1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher referenced this topic on
      • N Offline
        N Offline
        ntos
        wrote on last edited by ntos
        #3

        @JonB .
        Thank you. I overrode mouseReleaseEvent like this:
        void cloneFormatting(); //cloneFormatting copies of the selected text format into clonedFormat to be applied in applyCLonedFormatting.
        void applyClonedFormatting();
        void SdiWindow::mouseReleaseEvent(QMouseEvent *event)
        {
        QDebug() << "mouse released";
        if((event->buttons() & Qt::LeftButton) && isCloneFormatting)
        docWidget->textCursor().setCharFormat(clonedFormat);
        }
        Somehow, mouseReleaseEvent is never called.
        @Christian-Ehrlicher . No. It's not me.

        JonBJ 1 Reply Last reply
        0
        • N ntos

          @JonB .
          Thank you. I overrode mouseReleaseEvent like this:
          void cloneFormatting(); //cloneFormatting copies of the selected text format into clonedFormat to be applied in applyCLonedFormatting.
          void applyClonedFormatting();
          void SdiWindow::mouseReleaseEvent(QMouseEvent *event)
          {
          QDebug() << "mouse released";
          if((event->buttons() & Qt::LeftButton) && isCloneFormatting)
          docWidget->textCursor().setCharFormat(clonedFormat);
          }
          Somehow, mouseReleaseEvent is never called.
          @Christian-Ehrlicher . No. It's not me.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @ntos
          Please append override to your method declaration (in the .h file) to make sure you have defined it correctly.
          Your code won't print anything as it stands --- in fact it should give you a compilation error --- so if you want help you need to paste actual code, not just type something in here.
          The overriding does work so I don't know what you are doing.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            ntos
            wrote on last edited by
            #5

            Thank you. I'll wait till the chapter on mouseevent.

            JonBJ 1 Reply Last reply
            0
            • N ntos

              Thank you. I'll wait till the chapter on mouseevent.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @ntos What "chapter" on mouse event?

              1 Reply Last reply
              0
              • N Offline
                N Offline
                ntos
                wrote on last edited by ntos
                #7

                @JonB . I am on chapter 4 of foundations of Qt development about mdi. I don't know if there is a chapter on mouse event. I have tried, with the help of gpt, to implement the clone formatting as seen in libreoffice, but the sdi window doesn't detect mouse event because the QTextEdit is set as the main widget. Here is the code if you are interested:
                Please see the edited post tomorrow perhaps. I need 3600 seconds to be allowed to edit my post.

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  ntos
                  wrote on last edited by
                  #8

                  @JonB . I can post the complete code. The forum says it is spam.
                  void SdiWindow::cloneFormatting()
                  {

                  QTextCursor cursor = docWidget->textCursor();
                  clonedFormat = cursor.charFormat();
                  isCloneFormatting = true;
                   qDebug() << "Cloned format - Bold:" << (clonedFormat.fontWeight() == QFont::Bold);
                  statusBar()->showMessage(tr("Select text or click to apply formatting"));
                  

                  }

                  void SdiWindow::applyClonedFormatting()
                  {
                  qDebug() << "Applying cloned formatting";
                  QTextCursor cursor = docWidget->textCursor();
                  if (cursor.hasSelection()) {
                  qDebug() << "Applying to selection";
                  cursor.mergeCharFormat(clonedFormat);
                  } else {
                  qDebug() << "Applying to cursor position";
                  docWidget->setCurrentCharFormat(clonedFormat);
                  }
                  isCloneFormatting = false;
                  docWidget->setFocus(); // Ensure the text edit has focus
                  statusBar()->clearMessage();
                  qDebug() << "Format applied - Bold:" << (cursor.charFormat().fontWeight() == QFont::Bold);
                  }

                  // void SdiWindow::mouseReleaseEvent(QMouseEvent *event)
                  // {
                  // if ((event->button() == Qt::LeftButton) && isCloneFormatting )
                  // {
                  // qDebug() << "Left mouse button released.";
                  // applyClonedFormatting();
                  // }
                  // // QWidget::mouseReleaseEvent(event);
                  // }

                  JonBJ 1 Reply Last reply
                  0
                  • N ntos

                    @JonB . I can post the complete code. The forum says it is spam.
                    void SdiWindow::cloneFormatting()
                    {

                    QTextCursor cursor = docWidget->textCursor();
                    clonedFormat = cursor.charFormat();
                    isCloneFormatting = true;
                     qDebug() << "Cloned format - Bold:" << (clonedFormat.fontWeight() == QFont::Bold);
                    statusBar()->showMessage(tr("Select text or click to apply formatting"));
                    

                    }

                    void SdiWindow::applyClonedFormatting()
                    {
                    qDebug() << "Applying cloned formatting";
                    QTextCursor cursor = docWidget->textCursor();
                    if (cursor.hasSelection()) {
                    qDebug() << "Applying to selection";
                    cursor.mergeCharFormat(clonedFormat);
                    } else {
                    qDebug() << "Applying to cursor position";
                    docWidget->setCurrentCharFormat(clonedFormat);
                    }
                    isCloneFormatting = false;
                    docWidget->setFocus(); // Ensure the text edit has focus
                    statusBar()->clearMessage();
                    qDebug() << "Format applied - Bold:" << (cursor.charFormat().fontWeight() == QFont::Bold);
                    }

                    // void SdiWindow::mouseReleaseEvent(QMouseEvent *event)
                    // {
                    // if ((event->button() == Qt::LeftButton) && isCloneFormatting )
                    // {
                    // qDebug() << "Left mouse button released.";
                    // applyClonedFormatting();
                    // }
                    // // QWidget::mouseReleaseEvent(event);
                    // }

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @ntos
                    Please use the the forums Code tags (</> button) when pasting blocks of code.

                    I see a bunch of code for applying some formatting. I see a function commented out. I don't see a question.

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      ntos
                      wrote on last edited by
                      #10

                      @JonB . I cannot edit my original post. I had to post the code in quick reply, which doesn't have that code tag. But as you can see the mouseReleaseEvent function that is commented out. It never got called because of this code:

                      docWidget = new QTextEdit( this );
                      setCentralWidget( docWidget );

                      So applyClonedFormatting never got called .
                      I even installed an eventfilter in docWidget to intercept mouse events but it never worked.
                      If only this forum allowed uploading files or editting posts at any time, it would be much easier.

                      JonBJ 1 Reply Last reply
                      0
                      • N ntos

                        @JonB . I cannot edit my original post. I had to post the code in quick reply, which doesn't have that code tag. But as you can see the mouseReleaseEvent function that is commented out. It never got called because of this code:

                        docWidget = new QTextEdit( this );
                        setCentralWidget( docWidget );

                        So applyClonedFormatting never got called .
                        I even installed an eventfilter in docWidget to intercept mouse events but it never worked.
                        If only this forum allowed uploading files or editting posts at any time, it would be much easier.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @ntos
                        Your code is quite incomplete. You comment out the code you would like to work? You define a SdiWindow::mouseReleaseEvent() but no definition of SdiWindow not where it is used given to us? You have some QMainWindow, in order to go setCentralWidget(), but we don't see it? You say setting a central widget of a QTextEdit stops what working? (Presumably you realize any widget will gobble mouse events on it?) If you are going to want a format to be applied in a QTextEdit when you drag-select some text and release the mouse, maybe you need the mouseReleaseEvent() of the text edit? Or, if this is a "selection", what about QTextEdit::selectionChanged() signal?

                        Maybe someone else can tell from the code you provide.

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          ntos
                          wrote on last edited by
                          #12

                          Ok. Let me try again. I am still new around here and haven't known the ropes yet. Why is this forum so prohibitive. It doesn't allow you to post long code, saying content is a spam by Askismet.com.
                          Thank you for your perseverance. I am not allowed to post code.

                          artwawA 1 Reply Last reply
                          0
                          • N ntos

                            Ok. Let me try again. I am still new around here and haven't known the ropes yet. Why is this forum so prohibitive. It doesn't allow you to post long code, saying content is a spam by Askismet.com.
                            Thank you for your perseverance. I am not allowed to post code.

                            artwawA Offline
                            artwawA Offline
                            artwaw
                            wrote on last edited by
                            #13

                            @ntos if it's too long for the forum post you can use PastBin . com?

                            For more information please re-read.

                            Kind Regards,
                            Artur

                            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