Search code examples
c++qttextinputquotes

Quotation marks in QT text editing widgets


I'm having problems with writing quotation marks in Qt text editing widgets. Every single or double quotation mark I enter gets inserted as a straight one. However, I'd like to input curly left and right quotation marks (and if possible, lower left at the beginning and upper right at the end, as is common in some languages - slovak or czech e.g.).

I thought switching to the language's keyboard layout would take care of that (as is the case with left-to-right and right-to-left languages), but this doesn't change anything. I haven't found anything in the documentation regarding this, which makes me think I'm missing something. Or not.

Do you know of any way to achieve this with Qt (for C++) of version 4.7?

Thank you


Solution

  • Now I understand your problem. I see two solutions here:

    • Use of QRegExpValidator. This would require to act upon QTextEdit::textChanged() event. In this case you would have to parse ALL the text on every change - not very performance efficient (:
    • You could capture " key and add some logic behind it

      class editor : public QTextEdit
      {
          Q_OBJECT
      public:
          explicit editor();
          void keyPressEvent(QKeyEvent *e)
          {
              if (e->key() == Qt::Key_QuoteDbl)
              {
                   this->insertHtml("“");
                   this->insertHtml("”");
                   this->insertHtml("„");
                   this->insertHtml("“");
               }
               else
                   QTextEdit::keyPressEvent(e); // this passes other keys for ordinary processing
               }
           }
      }
      

      You should add some logic to control which quotes are inserted (maybe locale and if-opening-quotes-are-already-inserted based). Hope that helps