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:
Q_OBJECT
public:
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:
};
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;
};
To copy to clipboard, switch view to plain text mode
Or:
public:
State validate
(QString & input,
int & pos
) const { if(input.contains(' ')) return Invalid;
return Acceptable;
}
};
le->setValidator(new NoSpacesValidator(le));
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));
To copy to clipboard, switch view to plain text mode
Bookmarks