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. How disable copy and paste on QLineEdit
QtWS25 Last Chance

How disable copy and paste on QLineEdit

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 6.6k Views
  • 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.
  • T Offline
    T Offline
    tmtt66
    wrote on 1 Aug 2013, 08:40 last edited by
    #1

    How disable copy and paste on QLineEdit?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jazzycamel
      wrote on 1 Aug 2013, 09:46 last edited by jazzycamel
      #2

      Generally speaking I would discourage this kind of thing as it can be incredibly annoying for the user. Saying that, I have been in a situation where I had no choice so developed the following simple class:

      #include <QtGui/QLineEdit>
      #include <QtGui/QKeyEvent>
      class LineEdit : public QLineEdit
      {
          Q_OBJECT
      public:
          LineEdit(QWidget *parent=0) : QLineEdit(parent){ init(); }
          LineEdit(const QString &contents, QWidget *parent=0) : QLineEdit(contents,parent){ init(); }
      
      private:
          void init(){
              setAcceptDrops(false);
              setContextMenuPolicy(Qt::CustomContextMenu);
              connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(showMenu(QPoint)));
          }
      
      protected:
          void keyPressEvent(QKeyEvent *event){
              if(event->matches(QKeySequence::Copy) || event->matches(QKeySequence::Cut) || event->matches(QKeySequence::Paste))
                  event->ignore();
              else return QLineEdit::keyPressEvent(event);
          }
      
      private slots:
          void showMenu(QPoint position){}
      };
      

      It basically works by:

      • Catching the "QKeyEvent":http://qt-project.org/doc/qt-4.8/qkeyevent.html for the copy, cut and paste "QKeySequences":http://qt-project.org/doc/qt-4.8/qkeysequence.html and preventing there propagation,
      • Preventing the user from dragging and dropping data ("setAcceptDrops":http://qt-project.org/doc/qt-4.8/qwidget.html#acceptDrops-prop), and
      • Redirecting a request for a context menu to a custom slot to prevent the system menu being shown with the cut, copy and paste actions and allowing for the implementation of a custom menu.

      Hope this helps ;o)

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      2

      1/2

      1 Aug 2013, 08:40

      • Login

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