Forcing uppercase only into QLineEdit
-
No error message! But it still won't output capital letters.
@
void MainWindow::on_textbox1_textEdited(const QString &arg1)
{
QRegExp reg("[A-Z]{1,50}");
QRegExpValidator val(reg,0);
ui->textbox1->setValidator(&val);
}
@ -
You are setting the validator too late, do it in the constructor. Also, it will be destroyed at the end of on_textbox1_textEdited since it's allocated on the stack.
-
Like this? It's still not working.
@
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// setStyleSheet("background-image:url(':/step1/step2/step4.jpg');");
ui->setupUi(this);QRegExp reg("[A-Z]{1,50}"); QRegExpValidator val(reg,0); ui->textbox1->setValidator(&val); ui->textbox2->setValidator(&val); ui->textbox3->setValidator(&val); ui->textbox1->setMaxLength(50); ui->textbox3->setMaxLength(40); ui->textbox4->setMaxLength(3); ui->textbox4->setValidator(new QIntValidator(0,999, this));
}
@ -
QRegExpValidor will still be destroyed at the end of the constructor.
Please have a look at it's usage in the documentation
-
This is what I've been doing.
Using your example
@ void MainWindow::on_textbox1_textChanged(const QString &arg1)
{
// QFont.setCapitalization(QFont::AllUppercase);
ui->textbox1.setText(arg1.toUpper());
}
@I was hoping there was a better way to do it. But it seems to me like there isn't.
-
This is what I've been doing.
Using your example
@ void MainWindow::on_textbox1_textChanged(const QString &arg1)
{
// QFont.setCapitalization(QFont::AllUppercase);
ui->textbox1.setText(arg1.toUpper());
}
@I was hoping there was a better way to do it. But it seems to me like there isn't.
-
Hi and welcome to devnet,
Why not use QRegExpValidator ?
-
Hi and welcome to devnet,
Why not use QRegExpValidator ?
-
Thank you for the warm welcome.
I do use QRegExpValidator quite frequently, but I feel like this restriction reduces usability. From my experience, a validator will force exact input only, and reject anything else. Unless I'm not using it correctly.
What I usually need is a qtextbox or qlineedit that accepts all input, then transforms the input to be valid and acceptable (eg. uppercase) and reflects the changes immediately while the user is typing. Hence the @SLOT (textChanged(QString))@.
Is there a better way of doing this?
-
Thank you for the warm welcome.
I do use QRegExpValidator quite frequently, but I feel like this restriction reduces usability. From my experience, a validator will force exact input only, and reject anything else. Unless I'm not using it correctly.
What I usually need is a qtextbox or qlineedit that accepts all input, then transforms the input to be valid and acceptable (eg. uppercase) and reflects the changes immediately while the user is typing. Hence the @SLOT (textChanged(QString))@.
Is there a better way of doing this?
-
Try using this:
@ui->textbox1->setText(QString::number(whatever you need to output,16).toUpper());@
-
Try using this:
@ui->textbox1->setText(QString::number(whatever you need to output,16).toUpper());@
-
You can also start capturing and modifying your key events.
-
You can also start capturing and modifying your key events.