Dalibor Frivaldsky
Dalibor Frivaldsky

Reputation: 850

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

Upvotes: 2

Views: 1139

Answers (2)

friendzis
friendzis

Reputation: 809

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

Upvotes: 1

Dave Mateer
Dave Mateer

Reputation: 17946

Are you sure your keyboard is configured correctly? The following is handling curly quotes just fine for me (Windows 7):

#include <QtGui>

class MyLineEdit : public QLineEdit {
  Q_OBJECT
public:
  explicit MyLineEdit() : QLineEdit(NULL) {
    connect(this, SIGNAL(textChanged(QString)), SLOT(on_textChanged(QString)));
  }
private slots:
  void on_textChanged(const QString &text) {
    qDebug() << text;
  }
};

int main(int argc, char **argv) {
  QApplication app(argc, argv);
  MyLineEdit edit;
  edit.show();
  return app.exec();
}

#include "main.moc"

Another idea: Are you sure the font you are using in the text edit widget uses a different glyph for straight vs. curly quotes?

Upvotes: 1

Related Questions