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. Unable to use Key binding for clear in QTextEdit
Forum Updated to NodeBB v4.3 + New Features

Unable to use Key binding for clear in QTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.4k Views 1 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    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

    RatzzR 1 Reply Last reply
    0
    • Q Qt Enthusiast

      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

      RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by
      #2

      @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()));

      --Alles ist gut.

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        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

        RatzzR 1 Reply Last reply
        0
        • Q Qt Enthusiast

          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

          RatzzR Offline
          RatzzR Offline
          Ratzz
          wrote on last edited by
          #4

          @Qt-Enthusiast said:

          Let me know your inputs

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

          --Alles ist gut.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #5

            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

            }

            RatzzR 1 Reply Last reply
            0
            • Q Qt Enthusiast

              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

              }

              RatzzR Offline
              RatzzR Offline
              Ratzz
              wrote on last edited by
              #6

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

              --Alles ist gut.

              1 Reply Last reply
              0

              • Login

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