QTextEdit implementing keyPressEvent



  • I have subclassed QTextEdit byt I cannot get keyPressEvent to work. It appears to the the same as paintEvent which I use in a subclass of QWidget.

    #ifndef FORMULAEDITOR_H
    #define FORMULAEDITOR_H

    #include <QObject>
    #include <QWidget>
    #include <QTextEdit>
    #include <QKeyEvent>

    class FormulaEditor : public QTextEdit
    {
    Q_OBJECT
    public:
    explicit FormulaEditor(QWidget *parent = 0);
    void keyPressEvent(QKeyEvent * e);
    void keyReleaseEvent(QKeyEvent * e);
    };

    #endif // FORMULAEDITOR_H

    #include "formulaeditor.h"
    #include <QDebug>
    #include <QKeyEvent>

    FormulaEditor::FormulaEditor(QWidget *parent) : QTextEdit(parent)
    {

    }

    void FormulaEditor::keyPressEvent(QKeyEvent *e)
    {
    qDebug() << "Key Press Event";
    if (e->key() == Qt::Key_ParenLeft) {
    qDebug() << "Left Parenthesis";
    e->ignore();
    //this->QTextEdit::keyPressEvent( ek);
    }
    }

    void FormulaEditor::keyReleaseEvent(QKeyEvent *e)
    {
    qDebug() << "Key Release Event";
    if (e->key() == Qt::Key_ParenLeft) {
    qDebug() << "Left Parenthesis";
    e->ignore();
    //this->QTextEdit::keyReleaseEvent( ek);
    }

    }

    The only difference that I see is that my base class is QTextEdit and the function were reimplemented from QWidget, which is the not the direct base class. Any suggestions?



  • Hi @ofmrew

    What do you mean by keyPressEvent don't work ?

    I have just tested your code and , keypress , keyrelease event work without any problem
    this is my console output after , press and release inside FormulaEditor

    Key Press Event
    Key Release Event
    Key Press Event
    Key Release Event
    Key Press Event
    Key Release Event
    


  • Hi mostefa

    The original code was tried on Linux Mint 64-bits, and I just tried it on Windows 10-64bit, with Qt using MinGW 32. Neither will show those messages. You ran it as a console application, I assume. I am using QtCreator and the FormulaEditor was promoted from QTextEdit. What is the difference between what you ran and what I ran. Can I see your code?



  • @ofmrew said in QTextEdit implementing keyPressEvent:

    Hi mostefa

    What is the difference between what you ran and what I ran. Can I see your code?

    I call your class in main.cpp

    #include <QApplication>
    #include "formulaeditor.h"
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        FormulaEditor f;//here
        f.show();
    
        return a.exec();
    }
    


  • @mostefa

    The difference is Qt Creator and the promote. In Qt Creator I tried creating a console program with the above, adding the header and source files, but the compile error was that #include <QWidget> could not be found. Should I be using Unit Test or something else?

    You must have a way of testing code that is fast and efficient. Care to share?



  • @ofmrew

    I think I found the problem, my mistake. For some reason I promoted a QWidget UI object to FormulaEditor and not the QTextEdit UI object. It looks like there is no way to unpromote a promotion, so I will start fresh and let you know the results.



  • @ofmrew

    Thanks for the help. It was exactly as I suspected, I promoted the wrong UI object. I created a new application and promoted the correct UI object and the code works as expected. Sorry for wasting you time.
    thanks.



  • With the FormulaEditor promoted from QTextEdit I get the debug messages, but none of the typed text shows. I have two choices: totally implement the keyPressEvent function or have my function act only on the key strokes I want modified action and then let the base class function perform its action. I tried:

    void FormulaEditor::keyPressEvent(QKeyEvent *e)
    {
    qDebug() << "Key Press Event " << e;
    if (e->key() == Qt::Key_ParenLeft) {
    qDebug() << "Left Parenthesis";
    this->QTextEdit::keyPressEvent(QKeyEvent *e);
    }
    e->ignore();
    }

    The e->ignore() did not result in the event being forwarded to QTextEdit::keyPressEvent. Then I tried this->QTextEdit::keyPressEvent(QKeyEvent *e) which should have invoked the QTextEdit method but will not compile. The Errors for different attempts are:

    no matching function for call to ‘FormulaEditor::keyPressEvent(QKeyEvent&)’
    this->QTextEdit::keyPressEvent( *e);

    expected primary-expression before ‘*’ token
    this->QTextEdit::keyPressEvent(QKeyEvent *e);

    expected primary-expression before ‘*’ token
    QTextEdit::keyPressEvent(QKeyEvent *e);

    Every example I have found uses this format; however, none are passing arguments. This must be a syntax problem. Can someone point me in the correct direction?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.