QValidator: no spaces
-
wrote on 8 Mar 2023, 14:07 last edited by
Hi. I am trying to create a validator for a QLineEdit that does not accept spaces in it's input. To do this, I have the following validator in a QLineEdit.
nameInput = new QLineEdit; nameInput->setValidator(new QRegExpValidator( QRegExp("^\S*$"), this ));
I am, however, unable to edit anything in the QLineEdit after I have set up the validator. Specifically, I am unable to add or delete any characters in the QLineEdit while it is active. I am unsure why this is the case as I believe my Regex syntax to be accurate.
The full relevant code:
inputBox = new QDialog(this); //Set size of QDialog. inputBox->resize(450, 160); inputBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); inputBox->setStyleSheet("font: 20px; color: navy; border: none; outline: none;"); inputBox->setWindowFlag(Qt::WindowContextHelpButtonHint, false); QGridLayout *gridLayout = new QGridLayout(this); inputBox->setLayout(gridLayout); //QLabel QLabel *boxText = new QLabel(this); boxText->setText("Update test presets:"); boxText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); boxText->setAlignment(Qt::AlignCenter); gridLayout->addWidget(boxText, 0, 0, 1, 2, Qt::AlignCenter); //Add a QLineEdit, problem code is here nameInput = new QLineEdit; nameInput->setText(configName); QRegularExpressionValidator *re = new QRegularExpressionValidator; nameInput->setValidator(new QRegExpValidator( QRegExp("/^\S/"), this )); gridLayout->addWidget(nameInput, 1, 1); //QLabel QLabel *nameText = new QLabel(this); nameText->setText("Preset name:"); nameText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); nameText->setAlignment(Qt::AlignLeft); gridLayout->addWidget(nameText, 1, 0); //Add a QLineEdit startTempInput = new QLineEdit; startTempInput->setText(QString::number(startTemp)); startTempInput->setValidator(new QIntValidator(0, 86, this)); gridLayout->addWidget(startTempInput, 2, 1); //QLabel QLabel *startTempText = new QLabel(this); startTempText->setText("Start temp:"); startTempText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); startTempText->setAlignment(Qt::AlignLeft); gridLayout->addWidget(startTempText, 2, 0); //Add a QLineEdit expFlashptInput = new QLineEdit; expFlashptInput->setText(QString::number(expFlashpt)); expFlashptInput->setValidator(new QIntValidator(0, 86, this)); gridLayout->addWidget(expFlashptInput, 3, 1); //QLabel QLabel *expFlashptText = new QLabel(this); expFlashptText->setText("Expected flashpt:"); expFlashptText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); expFlashptText->setAlignment(Qt::AlignLeft); gridLayout->addWidget(expFlashptText, 3, 0); //Add a QDialogButtonBox QDialogButtonBox *boxButton = new QDialogButtonBox(QDialogButtonBox::Ok, this); boxButton->setStyleSheet("QPushButton {border: 2px solid #8f8f91;border-radius: 9px;background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde);font: 25px;outline: none;}" "QPushButton:pressed {background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #dadbde, stop: 1 #f6f7fa) }"); boxButton->findChild<QPushButton *>()->setMinimumSize(80, 50); gridLayout->addWidget(boxButton, 4, 0, 1, 2, Qt::AlignCenter); //Fix magic numbers setTestConfig_isNew = false; //Read information connect(boxButton, SIGNAL(accepted()), this, SLOT(setTestConfig())); inputBox->exec();
Please let me know if more information is required to solve this issue.
-
@JonB Oh yeah crap. Though I can't recall anything about \S as a special backslash character, I assume in order for C++ to be able to read the backslash we will require "\" instead of "".
In that case, would the exclusion character be
[^
and]
? I assume something akin to[^\\S]
would be more appropriate.wrote on 9 Mar 2023, 15:00 last edited by JonB 3 Sept 2023, 15:00@Dummie1138
Good on the backslashes, but no you are going too far. The attempt you had is actually close. It would need to be"^\\S*$"
But if that doesn't work the link I referenced says
QRegExpValidator
already assumes^...$
around your expression, i.e. complete match. So your^
/$
may be being taken literally. Try just"\\S*"
?
-
Hi. I am trying to create a validator for a QLineEdit that does not accept spaces in it's input. To do this, I have the following validator in a QLineEdit.
nameInput = new QLineEdit; nameInput->setValidator(new QRegExpValidator( QRegExp("^\S*$"), this ));
I am, however, unable to edit anything in the QLineEdit after I have set up the validator. Specifically, I am unable to add or delete any characters in the QLineEdit while it is active. I am unsure why this is the case as I believe my Regex syntax to be accurate.
The full relevant code:
inputBox = new QDialog(this); //Set size of QDialog. inputBox->resize(450, 160); inputBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); inputBox->setStyleSheet("font: 20px; color: navy; border: none; outline: none;"); inputBox->setWindowFlag(Qt::WindowContextHelpButtonHint, false); QGridLayout *gridLayout = new QGridLayout(this); inputBox->setLayout(gridLayout); //QLabel QLabel *boxText = new QLabel(this); boxText->setText("Update test presets:"); boxText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); boxText->setAlignment(Qt::AlignCenter); gridLayout->addWidget(boxText, 0, 0, 1, 2, Qt::AlignCenter); //Add a QLineEdit, problem code is here nameInput = new QLineEdit; nameInput->setText(configName); QRegularExpressionValidator *re = new QRegularExpressionValidator; nameInput->setValidator(new QRegExpValidator( QRegExp("/^\S/"), this )); gridLayout->addWidget(nameInput, 1, 1); //QLabel QLabel *nameText = new QLabel(this); nameText->setText("Preset name:"); nameText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); nameText->setAlignment(Qt::AlignLeft); gridLayout->addWidget(nameText, 1, 0); //Add a QLineEdit startTempInput = new QLineEdit; startTempInput->setText(QString::number(startTemp)); startTempInput->setValidator(new QIntValidator(0, 86, this)); gridLayout->addWidget(startTempInput, 2, 1); //QLabel QLabel *startTempText = new QLabel(this); startTempText->setText("Start temp:"); startTempText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); startTempText->setAlignment(Qt::AlignLeft); gridLayout->addWidget(startTempText, 2, 0); //Add a QLineEdit expFlashptInput = new QLineEdit; expFlashptInput->setText(QString::number(expFlashpt)); expFlashptInput->setValidator(new QIntValidator(0, 86, this)); gridLayout->addWidget(expFlashptInput, 3, 1); //QLabel QLabel *expFlashptText = new QLabel(this); expFlashptText->setText("Expected flashpt:"); expFlashptText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); expFlashptText->setAlignment(Qt::AlignLeft); gridLayout->addWidget(expFlashptText, 3, 0); //Add a QDialogButtonBox QDialogButtonBox *boxButton = new QDialogButtonBox(QDialogButtonBox::Ok, this); boxButton->setStyleSheet("QPushButton {border: 2px solid #8f8f91;border-radius: 9px;background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde);font: 25px;outline: none;}" "QPushButton:pressed {background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #dadbde, stop: 1 #f6f7fa) }"); boxButton->findChild<QPushButton *>()->setMinimumSize(80, 50); gridLayout->addWidget(boxButton, 4, 0, 1, 2, Qt::AlignCenter); //Fix magic numbers setTestConfig_isNew = false; //Read information connect(boxButton, SIGNAL(accepted()), this, SLOT(setTestConfig())); inputBox->exec();
Please let me know if more information is required to solve this issue.
wrote on 8 Mar 2023, 15:39 last edited by@Dummie1138 try "^[^\s]+$"
-
Hi. I am trying to create a validator for a QLineEdit that does not accept spaces in it's input. To do this, I have the following validator in a QLineEdit.
nameInput = new QLineEdit; nameInput->setValidator(new QRegExpValidator( QRegExp("^\S*$"), this ));
I am, however, unable to edit anything in the QLineEdit after I have set up the validator. Specifically, I am unable to add or delete any characters in the QLineEdit while it is active. I am unsure why this is the case as I believe my Regex syntax to be accurate.
The full relevant code:
inputBox = new QDialog(this); //Set size of QDialog. inputBox->resize(450, 160); inputBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); inputBox->setStyleSheet("font: 20px; color: navy; border: none; outline: none;"); inputBox->setWindowFlag(Qt::WindowContextHelpButtonHint, false); QGridLayout *gridLayout = new QGridLayout(this); inputBox->setLayout(gridLayout); //QLabel QLabel *boxText = new QLabel(this); boxText->setText("Update test presets:"); boxText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); boxText->setAlignment(Qt::AlignCenter); gridLayout->addWidget(boxText, 0, 0, 1, 2, Qt::AlignCenter); //Add a QLineEdit, problem code is here nameInput = new QLineEdit; nameInput->setText(configName); QRegularExpressionValidator *re = new QRegularExpressionValidator; nameInput->setValidator(new QRegExpValidator( QRegExp("/^\S/"), this )); gridLayout->addWidget(nameInput, 1, 1); //QLabel QLabel *nameText = new QLabel(this); nameText->setText("Preset name:"); nameText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); nameText->setAlignment(Qt::AlignLeft); gridLayout->addWidget(nameText, 1, 0); //Add a QLineEdit startTempInput = new QLineEdit; startTempInput->setText(QString::number(startTemp)); startTempInput->setValidator(new QIntValidator(0, 86, this)); gridLayout->addWidget(startTempInput, 2, 1); //QLabel QLabel *startTempText = new QLabel(this); startTempText->setText("Start temp:"); startTempText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); startTempText->setAlignment(Qt::AlignLeft); gridLayout->addWidget(startTempText, 2, 0); //Add a QLineEdit expFlashptInput = new QLineEdit; expFlashptInput->setText(QString::number(expFlashpt)); expFlashptInput->setValidator(new QIntValidator(0, 86, this)); gridLayout->addWidget(expFlashptInput, 3, 1); //QLabel QLabel *expFlashptText = new QLabel(this); expFlashptText->setText("Expected flashpt:"); expFlashptText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); expFlashptText->setAlignment(Qt::AlignLeft); gridLayout->addWidget(expFlashptText, 3, 0); //Add a QDialogButtonBox QDialogButtonBox *boxButton = new QDialogButtonBox(QDialogButtonBox::Ok, this); boxButton->setStyleSheet("QPushButton {border: 2px solid #8f8f91;border-radius: 9px;background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde);font: 25px;outline: none;}" "QPushButton:pressed {background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #dadbde, stop: 1 #f6f7fa) }"); boxButton->findChild<QPushButton *>()->setMinimumSize(80, 50); gridLayout->addWidget(boxButton, 4, 0, 1, 2, Qt::AlignCenter); //Fix magic numbers setTestConfig_isNew = false; //Read information connect(boxButton, SIGNAL(accepted()), this, SLOT(setTestConfig())); inputBox->exec();
Please let me know if more information is required to solve this issue.
wrote on 8 Mar 2023, 15:42 last edited byI see a difference between your two code blocks
@Dummie1138 said in QValidator: no spaces:
nameInput->setValidator(new QRegExpValidator( QRegExp("^\S*$"), this ));
and
nameInput->setValidator(new QRegExpValidator( QRegExp("/^\S/"), this ));
In one case you use leading and trailing slashes, in the other you don't. your "full relevant code" is wrong.
-
Hi. I am trying to create a validator for a QLineEdit that does not accept spaces in it's input. To do this, I have the following validator in a QLineEdit.
nameInput = new QLineEdit; nameInput->setValidator(new QRegExpValidator( QRegExp("^\S*$"), this ));
I am, however, unable to edit anything in the QLineEdit after I have set up the validator. Specifically, I am unable to add or delete any characters in the QLineEdit while it is active. I am unsure why this is the case as I believe my Regex syntax to be accurate.
The full relevant code:
inputBox = new QDialog(this); //Set size of QDialog. inputBox->resize(450, 160); inputBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); inputBox->setStyleSheet("font: 20px; color: navy; border: none; outline: none;"); inputBox->setWindowFlag(Qt::WindowContextHelpButtonHint, false); QGridLayout *gridLayout = new QGridLayout(this); inputBox->setLayout(gridLayout); //QLabel QLabel *boxText = new QLabel(this); boxText->setText("Update test presets:"); boxText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); boxText->setAlignment(Qt::AlignCenter); gridLayout->addWidget(boxText, 0, 0, 1, 2, Qt::AlignCenter); //Add a QLineEdit, problem code is here nameInput = new QLineEdit; nameInput->setText(configName); QRegularExpressionValidator *re = new QRegularExpressionValidator; nameInput->setValidator(new QRegExpValidator( QRegExp("/^\S/"), this )); gridLayout->addWidget(nameInput, 1, 1); //QLabel QLabel *nameText = new QLabel(this); nameText->setText("Preset name:"); nameText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); nameText->setAlignment(Qt::AlignLeft); gridLayout->addWidget(nameText, 1, 0); //Add a QLineEdit startTempInput = new QLineEdit; startTempInput->setText(QString::number(startTemp)); startTempInput->setValidator(new QIntValidator(0, 86, this)); gridLayout->addWidget(startTempInput, 2, 1); //QLabel QLabel *startTempText = new QLabel(this); startTempText->setText("Start temp:"); startTempText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); startTempText->setAlignment(Qt::AlignLeft); gridLayout->addWidget(startTempText, 2, 0); //Add a QLineEdit expFlashptInput = new QLineEdit; expFlashptInput->setText(QString::number(expFlashpt)); expFlashptInput->setValidator(new QIntValidator(0, 86, this)); gridLayout->addWidget(expFlashptInput, 3, 1); //QLabel QLabel *expFlashptText = new QLabel(this); expFlashptText->setText("Expected flashpt:"); expFlashptText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); expFlashptText->setAlignment(Qt::AlignLeft); gridLayout->addWidget(expFlashptText, 3, 0); //Add a QDialogButtonBox QDialogButtonBox *boxButton = new QDialogButtonBox(QDialogButtonBox::Ok, this); boxButton->setStyleSheet("QPushButton {border: 2px solid #8f8f91;border-radius: 9px;background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #f6f7fa, stop: 1 #dadbde);font: 25px;outline: none;}" "QPushButton:pressed {background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #dadbde, stop: 1 #f6f7fa) }"); boxButton->findChild<QPushButton *>()->setMinimumSize(80, 50); gridLayout->addWidget(boxButton, 4, 0, 1, 2, Qt::AlignCenter); //Fix magic numbers setTestConfig_isNew = false; //Read information connect(boxButton, SIGNAL(accepted()), this, SLOT(setTestConfig())); inputBox->exec();
Please let me know if more information is required to solve this issue.
wrote on 8 Mar 2023, 15:48 last edited by JonB 3 Aug 2023, 15:49@Dummie1138 said in QValidator: no spaces:
QRegularExpressionValidator *re = new QRegularExpressionValidator;
nameInput->setValidator(new QRegExpValidator( QRegExp("/^\S/"), this ));
Why do you create a
QRegularExpressionValidator
and then introduceQRegExpValidator
/QRegExp
? The latter should not be used any longer.How do you think the C++ literal string
"/^\S/"
is treated/resolves to? -
@Dummie1138 said in QValidator: no spaces:
QRegularExpressionValidator *re = new QRegularExpressionValidator;
nameInput->setValidator(new QRegExpValidator( QRegExp("/^\S/"), this ));
Why do you create a
QRegularExpressionValidator
and then introduceQRegExpValidator
/QRegExp
? The latter should not be used any longer.How do you think the C++ literal string
"/^\S/"
is treated/resolves to?wrote on 9 Mar 2023, 14:33 last edited by@JonB
QRegularExpressionValidator
is there as a piece of commented code. As forQRegExpValidator/QRegExp
, what's wrong with it?My understanding of
"/^\S/"
is that\S
indicates a whitespace, and^
indicates that it should not be the starter character. -
@JonB
QRegularExpressionValidator
is there as a piece of commented code. As forQRegExpValidator/QRegExp
, what's wrong with it?My understanding of
"/^\S/"
is that\S
indicates a whitespace, and^
indicates that it should not be the starter character.wrote on 9 Mar 2023, 14:35 last edited by JonB 3 Sept 2023, 14:48@Dummie1138 said in QValidator: no spaces:
what's wrong with it?
Like I said "The latter should not be used any longer." It was deprecated at Qt 5.x, removed in Qt6. Since
QRegularExpression
does the same and is better there's not much to discuss.@Dummie1138 said in QValidator: no spaces:
My understanding of "/^\S/" is that \S indicates a whitespace
Forget about regular expressions. Review the rules on
\
characters in C/C++ literal strings.Then regular expression
\S
does not match whitespace, it matches non-whitespace. IfQRegExp
supports this at all, I don't know.and
^
indicates that it should not be the starter character.^
indicates the start of the string match, what follows it must be the first character, not "not be the starter character". However, see https://doc.qt.io/qt-5/qregexpvalidator.html#details about^
/$
, those may be part of your problem, I don't know for sure since it'sQRegExp
stuff. -
@Dummie1138 said in QValidator: no spaces:
what's wrong with it?
Like I said "The latter should not be used any longer." It was deprecated at Qt 5.x, removed in Qt6. Since
QRegularExpression
does the same and is better there's not much to discuss.@Dummie1138 said in QValidator: no spaces:
My understanding of "/^\S/" is that \S indicates a whitespace
Forget about regular expressions. Review the rules on
\
characters in C/C++ literal strings.Then regular expression
\S
does not match whitespace, it matches non-whitespace. IfQRegExp
supports this at all, I don't know.and
^
indicates that it should not be the starter character.^
indicates the start of the string match, what follows it must be the first character, not "not be the starter character". However, see https://doc.qt.io/qt-5/qregexpvalidator.html#details about^
/$
, those may be part of your problem, I don't know for sure since it'sQRegExp
stuff.wrote on 9 Mar 2023, 14:49 last edited by@JonB Oh yeah crap. Though I can't recall anything about \S as a special backslash character, I assume in order for C++ to be able to read the backslash we will require "\" instead of "".
In that case, would the exclusion character be
[^
and]
? I assume something akin to[^\\S]
would be more appropriate. -
@JonB Oh yeah crap. Though I can't recall anything about \S as a special backslash character, I assume in order for C++ to be able to read the backslash we will require "\" instead of "".
In that case, would the exclusion character be
[^
and]
? I assume something akin to[^\\S]
would be more appropriate.wrote on 9 Mar 2023, 15:00 last edited by JonB 3 Sept 2023, 15:00@Dummie1138
Good on the backslashes, but no you are going too far. The attempt you had is actually close. It would need to be"^\\S*$"
But if that doesn't work the link I referenced says
QRegExpValidator
already assumes^...$
around your expression, i.e. complete match. So your^
/$
may be being taken literally. Try just"\\S*"
?
-
1/8