Results 1 to 4 of 4

Thread: middle mouse paste event

  1. #1
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default middle mouse paste event

    I'm working with a QLineEdit object in which spaces are not allowed.
    As a space might be insert via a paste action, I've removed the context menu and re-implemented the paste() method (empty).

    Now I have to fix the last problem: the user is able to paste any char via the mouse middle button (on *nix systems).

    I've re-implemented the mousePressEvent() method but with no positive result.

    Is there a way to do that ?

  2. #2
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: middle mouse paste event

    It is possible to install an event filter on the QLineEdit see QObject::installEventFilterinstall the filter check if the event type is QMouseButton

    Qt Code:
    1. bool UrClass::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if (event->type() == QEvent::MouseButtonPress)
    4. {
    5. QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    6. if (mouseEvent->button() == Qt::MiddleButton)
    7. {
    8. //add you're stuff here
    9. return true; //consumed the event
    10. }
    11. }
    12. return false; //not consumed the event
    13. }
    To copy to clipboard, switch view to plain text mode 

    maybe this is also a solution:
    connect to the editingFinished signal
    and strip all white spaces

    Qt Code:
    1. QString tmp = QLineEdit.text();
    2. tmp=tmp.simplified();
    3. tmp.replace(" ","");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: middle mouse paste event

    That's not a good approach. Instead of trying to fix every possible method of the user inserting text containing a space, focus on manipulating the text that's being set on the widget. What you want to achieve can easily be done for example this way:

    Qt Code:
    1. class NoSpacesLineEdit : public QLineEdit {
    2. Q_OBJECT
    3. public:
    4. NoSpacesLineEdit(QWidget *parent = 0) : QLineEdit(parent) {
    5. connect(this, SIGNAL(textChanged(QString)), this, SLOT(tryingToSetText(QString)));
    6. }
    7. private slots:
    8. void tryingToSetText(const QString &txt) {
    9. if(txt.contains(' '))
    10. setText(m_prevText);
    11. else m_prevText = txt;
    12. }
    13. private:
    14. QString m_prevText;
    15. };
    To copy to clipboard, switch view to plain text mode 

    Or:

    Qt Code:
    1. class NoSpacesValidator : public QValidator {
    2. public:
    3. NoSpacesValidator(QObject *parent = 0) : QValidator(parent) {}
    4. State validate(QString & input, int & pos) const {
    5. if(input.contains(' ')) return Invalid;
    6. return Acceptable;
    7. }
    8. };
    9.  
    10. QLineEdit *le = new QLineEdit;
    11. le->setValidator(new NoSpacesValidator(le));
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: middle mouse paste event

    Well, thanks to both.

    I'll see which is the best solution as the gui will be used by the customer so the customer will decide during the test phase.

    From my point of view it would be better to avoid pasting strange text as the gui is used to create grammars for asr programs (auto speech recog).
    So, every word in the LineEdit is very important. The user is already guided through the creation of the grammars (he creates phrases, indeed, the grammars are created by me via software) so it would be consistent forbidding some actions like pasting or space keys inserting.

Similar Threads

  1. Ogre+qt mouse event (add object with mouse problem)
    By rimie23 in forum Qt Programming
    Replies: 7
    Last Post: 24th April 2012, 10:49
  2. Replies: 14
    Last Post: 17th January 2012, 09:01
  3. Replies: 3
    Last Post: 7th January 2012, 08:38
  4. Drag/Drop on middle mouse button?
    By joeld42 in forum Qt Programming
    Replies: 0
    Last Post: 21st August 2010, 00:20
  5. Middle Button Mouse
    By jaime in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2006, 03:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.