Forcing uppercase only into QLineEdit



  • How can i set a line edit (named textbox1) to uppercase only?

    I tried InputMethodHints:

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->textbox1->setMaxLength(50);
    ui->textbox1->setInputMethodHints(Qt::ImhUppercaseOnly);
    }
    @

    I tried it via the Design Mode as well, going to properties, expanding the InputMethodHints list and selecting ImhUppercaseonly checkbox.

    I also tried this:
    @
    void MainWindow::on_textbox1_textChanged(const QString &arg1)
    {
    QFont.setCapitalization(QFont::AllUppercase);
    }
    @

    and this:
    @
    QValidator::State pqUpcaseValidator::validate(QString& Text, int& Pos) const
    {
    Text=Text.Upper();
    return Acceptable;
    }
    @

    Any help would be appreciated. Thank you.



  • Try this.

    @ QRegExp reg("[A-Z]{1,20}");
    QRegExpValidator val(reg,0);
    edit.setValidator(&val);@

    Helps you enter only 20 characters. It will only accept the capital letters.



  • One error message says 'edit' wasn't declared in the scope.

    @
    void MainWindow::on_textbox1_textEdited(const QString &arg1)
    {
    QRegExp reg("[A-Z]{1,50}");
    QRegExpValidator val(reg,0);
    edit.setValidator(&val);
    }
    @



  • edit is object of QLineEdit. Please pass object of QLineEdit you have.



  • 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);
    }
    @


  • Lifetime Qt Champion

    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));
    

    }
    @


  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why not use QRegExpValidator ?


  • Lifetime Qt Champion

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.