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. QTextEdit functionality
Qt 6.11 is out! See what's new in the release blog

QTextEdit functionality

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 317 Views 2 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    Programmatically I've created an instance of QTextEdit and added to a window, however for some reason space and carriage return do not work unless I type in an alpha, then cursor back to the previous position and then space and carriage return work.

    Any suggestions what could explain this behaviour ?

    Kind Regards,
    Sy

    SGaistS J.HilkJ 2 Replies Last reply
    0
    • J.HilkJ J.Hilk

      @SPlatten do you have a validator installed?

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #5

      @J-Hilk , @SGaist , OMG, found it, now I feel very stupid. Before the setData is being called the string is trimmed.

      Kind Regards,
      Sy

      1 Reply Last reply
      2
      • SPlattenS SPlatten

        Programmatically I've created an instance of QTextEdit and added to a window, however for some reason space and carriage return do not work unless I type in an alpha, then cursor back to the previous position and then space and carriage return work.

        Any suggestions what could explain this behaviour ?

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Any filter or other widgets stealing the focus ?

        Without more details it's hard to tell.

        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
        • SPlattenS SPlatten

          Programmatically I've created an instance of QTextEdit and added to a window, however for some reason space and carriage return do not work unless I type in an alpha, then cursor back to the previous position and then space and carriage return work.

          Any suggestions what could explain this behaviour ?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #3

          @SPlatten do you have a validator installed?


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          SPlattenS 2 Replies Last reply
          0
          • J.HilkJ J.Hilk

            @SPlatten do you have a validator installed?

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #4

            @J-Hilk , I don't think so, I've been working on this project for a long time, coming back to it when I have spare time, this is my derived class:

            /**
             * File:    clsQtTextEdit.cpp
             * Notes:   Contains implementation of the overriden signal/slot
             *          helper class for the QTextEdit control
             * History: 2020/06/11 Created by Simon Platten
             */
            #include <clsCNT.h>
            #include <clsQtTextEdit.h>
            #include <clsScriptHelper.h>
            //Static members
            const char clsQtTextEdit::mscszQtSignalCopyAvailable[]            = "copyAvailable";
            const char clsQtTextEdit::mscszQtSignalCurrentCharFormatChanged[] = "currentCharFormatChanged";
            const char clsQtTextEdit::mscszQtSignalCursorPositionChanged[]    = "cursorPositionChanged";
            const char clsQtTextEdit::mscszQtSignalRedoAvailable[]            = "redoAvailable";
            const char clsQtTextEdit::mscszQtSignalSelectionChanged[]         = "selectionChanged";
            const char clsQtTextEdit::mscszQtSignalTextChanged[]              = "textChanged";
            const char clsQtTextEdit::mscszQtSignalUndoAvailable[]            = "undoAvailable";
            mpSignals clsQtTextEdit::mscmpSignals = {
                    std::make_pair(QString(clsQtTextEdit::mscszQtSignalCopyAvailable)
                                  ,new clsSignal(clsCNT::mscszTextEdit
                                                ,clsQtTextEdit::mscszQtSignalCopyAvailable))
                   ,std::make_pair(QString(clsQtTextEdit::mscszQtSignalCurrentCharFormatChanged)
                                  ,new clsSignal(clsCNT::mscszTextEdit
                                                ,clsQtTextEdit::mscszQtSignalCurrentCharFormatChanged))
                   ,std::make_pair(QString(clsQtTextEdit::mscszQtSignalCursorPositionChanged)
                                  ,new clsSignal(clsCNT::mscszTextEdit
                                                ,clsQtTextEdit::mscszQtSignalCursorPositionChanged))
                   ,std::make_pair(QString(clsQtTextEdit::mscszQtSignalRedoAvailable)
                                  ,new clsSignal(clsCNT::mscszTextEdit
                                                ,clsQtTextEdit::mscszQtSignalRedoAvailable))
                   ,std::make_pair(QString(clsQtTextEdit::mscszQtSignalSelectionChanged)
                                  ,new clsSignal(clsCNT::mscszTextEdit
                                                ,clsQtTextEdit::mscszQtSignalSelectionChanged))
                   ,std::make_pair(QString(clsQtTextEdit::mscszQtSignalTextChanged)
                                  ,new clsSignal(clsCNT::mscszTextEdit
                                                ,clsQtTextEdit::mscszQtSignalTextChanged))
                   ,std::make_pair(QString(clsQtTextEdit::mscszQtSignalUndoAvailable)
                                  ,new clsSignal(clsCNT::mscszTextEdit
                                                ,clsQtTextEdit::mscszQtSignalUndoAvailable))
                                                    };
            /**
             * @brief clsQtTextEdit::clsQtTextEdit
             * @param pobjNode : Pointer to XML node
             * @param pstrCSS : Pointer to CSS
             * @param pslstProperties : Pointer to properties
             * @param pParent : Pointer to parent widget
             */
            clsQtTextEdit::clsQtTextEdit(clsXMLnode* pobjNode, QString* pstrCSS
                                                             , QStringList* pslstProperties
                                                             , QWidget* pParent)
                                : QTextEdit(pParent), clsXMLinterface(pobjNode) {
                mpobjNode->setWidget(this); //Should always be first line in constructor
                clsXMLnode::addSignalsToMap(&clsQtTextEdit::mscmpSignals);
            //Any properties
                if ( pslstProperties != nullptr ) {
                    foreach( QString strProperty, *pslstProperties ) {
                        QStringList slstProperty(strProperty.split(clsXMLnode::msccPropertyDelimiter));
                        if ( slstProperty.length() != 2 ) {
                            continue;
                        }
                        setProperty(slstProperty[0], slstProperty[1]);
                    }
                }
                if ( pstrCSS != nullptr && pstrCSS->isEmpty() != true ) {
                    setStyleSheet(*pstrCSS);
                }
                QString strColumns(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrColumns))
                       ,strRows(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrRows));
                QRect rctGeom(geometry());
                bool blnModified(false);
                if ( strColumns.isEmpty() != true ) {
                    int intCharWidth(QFontMetrics(font()).maxWidth())
                       ,intPixelWidth(strColumns.toInt() * intCharWidth);
                    rctGeom.setWidth(intPixelWidth);
                    setFixedWidth(intPixelWidth);
                    blnModified = true;
                }
                if ( strRows.isEmpty() != true ) {
                    int intCharHeight(QFontMetrics(font()).height())
                       ,intPixelHeight(strRows.toInt() * (intCharHeight + 1));
                    rctGeom.setHeight(intPixelHeight);
                    setFixedHeight(intPixelHeight);
                    blnModified = true;
                }
                if ( blnModified == true && rctGeom.isValid() == true ) {
                    mpobjNode->setGeometry(rctGeom);
                }    
                if ( blnModified == true ) {
                    QWidget* pobjWidget(mpobjNode->pobjGetWidget());
                    if ( pobjWidget != nullptr ) {
                        pobjWidget->setMinimumSize(rctGeom.width(), rctGeom.height());
                    }
                }
                //Connect native signals to repeater signals
                QMetaObject::Connection cn;
                cn = QObject::connect(this, &clsQtTextEdit::copyAvailable
                                     ,this, &clsQtTextEdit::rptrCopyAvailable);
                Q_ASSERT_X(cn, clsQtTextEdit::metaObject()->className()
                          ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                            .arg(clsQtTextEdit::mscszQtSignalCopyAvailable)
                                      .toLocal8Bit().data());
                cn = QObject::connect(this, &clsQtTextEdit::currentCharFormatChanged
                                     ,this, &clsQtTextEdit::rptrCurrentCharFormatChanged);
                Q_ASSERT_X(cn, clsQtTextEdit::metaObject()->className()
                          ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                            .arg(clsQtTextEdit::mscszQtSignalCurrentCharFormatChanged)
                                      .toLocal8Bit().data());
                cn = QObject::connect(this, &clsQtTextEdit::cursorPositionChanged
                                     ,this, &clsQtTextEdit::rptrCursorPositionChanged);
                Q_ASSERT_X(cn, clsQtTextEdit::metaObject()->className()
                          ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                            .arg(clsQtTextEdit::mscszQtSignalCursorPositionChanged)
                                      .toLocal8Bit().data());
                cn = QObject::connect(this, &clsQtTextEdit::redoAvailable
                                     ,this, &clsQtTextEdit::rptrRedoAvailable);
                Q_ASSERT_X(cn, clsQtTextEdit::metaObject()->className()
                          ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                            .arg(clsQtTextEdit::mscszQtSignalRedoAvailable)
                                      .toLocal8Bit().data());
                cn = QObject::connect(this, &clsQtTextEdit::selectionChanged
                                     ,this, &clsQtTextEdit::rptrSelectionChanged);
                Q_ASSERT_X(cn, clsQtTextEdit::metaObject()->className()
                          ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                            .arg(clsQtTextEdit::mscszQtSignalSelectionChanged)
                                      .toLocal8Bit().data());
                cn = QObject::connect(this, &clsQtTextEdit::textChanged
                                     ,this, &clsQtTextEdit::rptrTextChanged);
                Q_ASSERT_X(cn, clsQtTextEdit::metaObject()->className()
                          ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                            .arg(clsQtTextEdit::mscszQtSignalTextChanged)
                                      .toLocal8Bit().data());
                cn = QObject::connect(this, &clsQtTextEdit::undoAvailable
                                     ,this, &clsQtTextEdit::rptrUndoAvailable);
                Q_ASSERT_X(cn, clsQtTextEdit::metaObject()->className()
                          ,QString(clsXMLnode::mscszAssertCannotConnectToRepeater)
                            .arg(clsQtTextEdit::mscszQtSignalUndoAvailable)
                                      .toLocal8Bit().data());
                mpobjWindow = mpobjNode->pobjGetWindow();
                if ( mpobjWindow != nullptr ) {
                    cn = QObject::connect(mpobjWindow, &clsXMLnode::blankControls
                                         ,this, &clsQtTextEdit::blankControl);
                    Q_ASSERT_X(cn, clsQtTextEdit::metaObject()->className()
                              ,QString(clsXMLnode::mscszAssertCannotConnect)
                                .arg(clsXMLnode::mscszSignalBlankControls)
                                          .toLocal8Bit().data());
                }
                //Now the widget should be set-up, finalise set-up
                addNodeToWindow();
                cn = QObject::connect(mpobjNode, &clsXMLnode::setData
                                     ,this, &clsQtTextEdit::setData);
                Q_ASSERT_X(cn, clsQtTextEdit::metaObject()->className()
                          ,QString("Cannot connect %1!").arg(clsXMLnode::mscszSignalSetData)
                                      .toLocal8Bit().data());
            }
            /**
             * @brief clsQtTextEdit::blankControl
             */
            void clsQtTextEdit::blankControl() {
                setData("");
            }
            /**
             * @brief clsQtTextEdit::blnValidSignal
             * @param strSignal : The signal to validate
             * @return true if the signal is valid else false
             */
            bool clsQtTextEdit::blnValidSignal(QString strSignal) {
                return clsXMLinterface::blnValidSignal(&clsQtTextEdit::mscmpSignals, strSignal);
            }
            /**
             * @brief clsQtTextEdit::objGetData
             * @param pstrValue : Optional, pointer return value as string
             * @return Current text from control
             */
            QJsonObject clsQtTextEdit::objGetData(QString* pstrValue) {
                QString strValue(strGetData());
                QJsonObject objJSON;
                objJSON.insert(clsXMLnode::mscszAttrText, strValue);
                if ( pstrValue != nullptr ) {
                    *pstrValue = strValue;
                }
                return objJSON;
            }
            /**
             * @brief clsQtTextEdit::rptrCopyAvailable
             * @param blnYes : boolean yes = true else false
             */
            void clsQtTextEdit::rptrCopyAvailable(bool blnYes) {
                QJsonObject objJSON;
                if ( mpobjNode->blnCheckSubscribers(clsQtTextEdit::mscszQtSignalCopyAvailable
                                                   ,&objJSON) == true ) {
                    QJsonObject objParam;
                    objParam["yes"] = blnYes;
                    objJSON[clsXMLnode::mscszAttrParameters] = objParam;
                    emit mpobjNode->commonRptdSignal(objJSON);
                }
            }
            /**
             * @brief clsQtTextEdit::rptrCurrentCharFormatChanged
             * @param format : format information
             */
            void clsQtTextEdit::rptrCurrentCharFormatChanged(const QTextCharFormat& format) {
                QJsonObject objJSON;
                if ( mpobjNode->blnCheckSubscribers(clsQtTextEdit::mscszQtSignalCurrentCharFormatChanged
                                                   ,&objJSON) == true ) {
                    QJsonObject objParam;
                    //objParam["format"] = format;
                    objJSON[clsXMLnode::mscszAttrParameters] = objParam;
                    emit mpobjNode->commonRptdSignal(objJSON);
                }
                (void)format;
            }
            /**
             * @brief clsQtTextEdit::rptrCursorPositionChanged
             */
            void clsQtTextEdit::rptrCursorPositionChanged() {
                QJsonObject objJSON;
                if ( mpobjNode->blnCheckSubscribers(clsQtTextEdit::mscszQtSignalCursorPositionChanged
                                                   ,&objJSON) == true ) {
                    QJsonObject objParam;
                    QCursor curPos = cursor();
                    QPoint ptCursor = curPos.pos();
                    objParam[clsXMLnode::mscszAttrX] = ptCursor.x();
                    objParam[clsXMLnode::mscszAttrY] = ptCursor.y();
                    objJSON[clsXMLnode::mscszAttrParameters] = objParam;
                    emit mpobjNode->commonRptdSignal(objJSON);
                }
            }
            /**
             * @brief clsQtTextEdit::rptrRedoAvailable
             * @param blnAvailable : boolean yes = true else false
             */
            void clsQtTextEdit::rptrRedoAvailable(bool blnAvailable) {    
                QJsonObject objJSON;
                if ( mpobjNode->blnCheckSubscribers(clsQtTextEdit::mscszQtSignalRedoAvailable
                                                   ,&objJSON) == true ) {
                    QJsonObject objParam;
                    objParam[clsXMLnode::mscszAttrAvailable] = blnAvailable;
                    objJSON[clsXMLnode::mscszAttrParameters] = objParam;
                    emit mpobjNode->commonRptdSignal(objJSON);
                }
            }
            /**
             * @brief clsQtTextEdit::rptrSelectionChanged
             */
            void clsQtTextEdit::rptrSelectionChanged() {
                QJsonObject objJSON;
                if ( mpobjNode->blnCheckSubscribers(clsQtTextEdit::mscszQtSignalSelectionChanged
                                                   ,&objJSON) == true ) {
                    emit mpobjNode->commonRptdSignal(objJSON);
                }
            }
            /**
             * @brief clsQtTextEdit::rptrTextChanged
             */
            void clsQtTextEdit::rptrTextChanged() {
                QJsonObject objJSON(mpobjNode->makeJSONresponse(clsQtTextEdit::mscszQtSignalTextChanged));
                const QString cstrText(strGetData());
                objJSON[clsXMLnode::mscszAttrData] = cstrText;
                emit mpobjNode->commonRptdSignal(objJSON);
                setData(cstrText);
            }
            /**
             * @brief clsQtTextEdit::rptrUndoAvailable
             * @param blnAvailable : boolean yes = true else false
             */
            void clsQtTextEdit::rptrUndoAvailable(bool blnAvailable) {
                QJsonObject objJSON;
                if ( mpobjNode->blnCheckSubscribers(clsQtTextEdit::mscszQtSignalUndoAvailable
                                                   ,&objJSON) == true ) {
                    QJsonObject objParam;
                    objParam[clsXMLnode::mscszAttrAvailable] = blnAvailable;
                    objJSON[clsXMLnode::mscszAttrParameters] = objParam;
                    emit mpobjNode->commonRptdSignal(objJSON);
                }
            }
            /**
             * @brief clsQtTextEdit::setData
             * @param crstrData : Constant reference to data string
             * @param blnPush : Optional true to push change
             */
            void clsQtTextEdit::setData(const QString& crstrData, bool blnPush) {
                //Get the ID
                QString strID(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrID));
                if ( strID.isEmpty() == true ) {
                    return;
                }
                if ( blnPush == true ) {
                    QJsonObject objData;
                    int intPosition;
                    objData.insert(strID, crstrData);
                    if ( (intPosition = intPush(objData)) >= 0 ) {
                        mpobjNode->mpobjXMLIF->setPosition(intPosition);
                    }
                }
                bool blnBlock(QTextEdit::blockSignals(true));
                QString strCurrent(QTextEdit::toPlainText());
                if ( strCurrent.compare(crstrData) != 0 ) {
                    //QCursor cursor(this->cursor());
                    QTextCursor cursor(textCursor());
                    int intPosition(cursor.position());
                    QTextEdit::setText(crstrData);
                    cursor.setPosition(intPosition);
                    setTextCursor(cursor);
                    //this->setCursor(cursor);
                }
                QTextEdit::blockSignals(blnBlock);
                clsXMLinterface::setData(crstrData, blnPush);
            }
            /**
             * @brief clsQtTextEdit::setProperty
             * @param strProperty : The property name
             * @param strValue : Value to assign to property
             */
            void clsQtTextEdit::setProperty(QString strProperty, QString strValue) {
                if ( strProperty.compare(clsXMLnode::mscszPropertyCursorWidth) == 0 ) {
                    setCursorWidth(strValue.toInt());
                } else if ( strProperty.compare(clsXMLnode::mscszPropertyOverwrite) == 0 ) {
                    bool blnOverwrite;
            
                    if ( strValue.compare(clsXMLnode::mscszTrue) == 0 ) {
                        blnOverwrite = true;
                    } else {
                        blnOverwrite = false;
                    }
                    setOverwriteMode(blnOverwrite);
                } else if ( strProperty.compare(clsXMLnode::mscszPropertyReadOnly) == 0 ) {
                    bool blnReadOnly;
            
                    if ( strValue.compare(clsXMLnode::mscszTrue) == 0 ) {
                        blnReadOnly = true;
                    } else {
                        blnReadOnly = false;
                    }
                    setReadOnly(blnReadOnly);
                } else if ( strProperty.compare(clsXMLnode::mscszPropertyRichText) == 0 ) {
                    bool blnRichText;
            
                    if ( strValue.compare(clsXMLnode::mscszTrue) == 0 ) {
                        blnRichText = true;
                    } else {
                        blnRichText = false;
                    }
                    setAcceptRichText(blnRichText);
                }
            }
            /**
             * @brief clsQtTextEdit::setText
             * @param crstrText : Constant reference to text to use
             */
            void clsQtTextEdit::setText(const QString& crstrText) {
                setData(crstrText);
            }
            /**
             * @brief clsQtTextEdit::strGetData
             * @return Text trimmed of whitespace
             */
            QString clsQtTextEdit::strGetData() {
                return toPlainText().trimmed();
            }
            

            Kind Regards,
            Sy

            1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @SPlatten do you have a validator installed?

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #5

              @J-Hilk , @SGaist , OMG, found it, now I feel very stupid. Before the setData is being called the string is trimmed.

              Kind Regards,
              Sy

              1 Reply Last reply
              2
              • SPlattenS SPlatten has marked this topic as solved on

              • Login

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