PDA

View Full Version : middle mouse paste event



trallallero
21st May 2012, 16:34
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 ?

StrikeByte
21st May 2012, 17:08
It is possible to install an event filter on the QLineEdit see QObject::installEventFilter
install the filter check if the event type is QMouseButton


bool UrClass::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if (mouseEvent->button() == Qt::MiddleButton)
{
//add you're stuff here
return true; //consumed the event
}
}
return false; //not consumed the event
}

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



QString tmp = QLineEdit.text();
tmp=tmp.simplified();
tmp.replace(" ","");

wysota
22nd May 2012, 00:59
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:


class NoSpacesLineEdit : public QLineEdit {
Q_OBJECT
public:
NoSpacesLineEdit(QWidget *parent = 0) : QLineEdit(parent) {
connect(this, SIGNAL(textChanged(QString)), this, SLOT(tryingToSetText(QString)));
}
private slots:
void tryingToSetText(const QString &txt) {
if(txt.contains(' '))
setText(m_prevText);
else m_prevText = txt;
}
private:
QString m_prevText;
};

Or:


class NoSpacesValidator : public QValidator {
public:
NoSpacesValidator(QObject *parent = 0) : QValidator(parent) {}
State validate(QString & input, int & pos) const {
if(input.contains(' ')) return Invalid;
return Acceptable;
}
};

QLineEdit *le = new QLineEdit;
le->setValidator(new NoSpacesValidator(le));

trallallero
22nd May 2012, 08:39
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.