Unable to use Key binding for clear in QTextEdit



  • I am using following code and clear() slot is not getting triggered when the user press Ctrl+R key . Basically nothing happens when I press Ctrl+R Let me know what I am missing in following code

    void myTextEdit:contextMenuEvent(QContextMenuEvent event) {
    QMenu * menu = createStandardContextMenu();
    QAction clearAction = new IN_CURRENT_POOL QAction("Clear",this);
    clearAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
    connect(clearAction,SIGNAL(triggered()),this,SLOT(clear()));
    menu->addSeparator();
    menu->addAction(clearAction);
    menu->exec(event->globalPos());
    delete menu;
    }

    Is there else I need to put in my code



  • @Qt-Enthusiast
    You may try this to activate short-cut using

        QShortcut *shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_R), this);
        connect(shortcut, SIGNAL(activated()), this, SLOT(clear()));


  • it is working but only when I use Right mouse button click i.e when the menu pops up

    I think My code should like following when I want clear to get called even when the user does open the pop up menu

    void myTextEdit::contextMenuEvent(QContextMenuEvent event) {
    QMenu * menu = createStandardContextMenu();
    menu->addSeparator();
    QAction
    clearAction = new IN_CURRENT_POOL QAction("Clear",this);
    menu->addAction(clearAction);
    clearAction->setShortcut(tr("Ctrl+R"));
    connect(clearAction,SIGNAL(triggered()),this,SLOT(clear()));
    menu->exec(event->globalPos());
    delete menu;
    }

    void GQLogView::keyPressEvent(QKeyEvent *event) {
    QTextEdit::keyPressEvent(event);
    if (event->key() == Qt::Key_R && event->modifiers() & Qt::ControlModifier) {
    clear();
    }
    event->accept();
    }

    Let me know your inputs



  • @Qt-Enthusiast said:

    Let me know your inputs

    Pressing CTRL+R will call the slot clear() and does what the clear() code says



  • Ok I changed that and it is working

    myTextEdit::myTextEdit(QWidget* dparent)
    : QTextEdit(dparent), u_shortcut(0)
    {
    setReadOnly(true);
    setLineWrapMode(QTextEdit::NoWrap);
    QShortcut *u_shortcut = new IN_CURRENT_POOL QShortcut(QKeySequence(Qt::CTRL + Qt::Key_R), this);
    connect(u_shortcut, SIGNAL(activated()), this, SLOT(clear()));
    }

    Please let me know if I need to delete the u_shortcut in the desctructor
    myTextEdit::~myTextEdit() {
    u_shortcut

    }



  • @Qt-Enthusiast
    Since you are passing the parent to u_shortcut . The u_shortcut will be deleted on deletion of parent.


Log in to reply
 

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