Results 1 to 5 of 5

Thread: Use QLineEdit with mask AND validator for money OUTPUT

  1. #1
    Join Date
    Aug 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Use QLineEdit with mask AND validator for money OUTPUT

    I created a dialog containing some QLineEdits and a QTableView.
    The QLineEdits are connected to a QTableView using QDataWidgetMapper, so, whenever I select a row in QTableView, QLineEdits are filled with their respective values (see below).
    One column in the QTableView is CURRENCY type, but it is not showing correctly the value.

    For example, the value 1234.50 should be presented in format "1.234,50" (because I'm using Brazilian Real), however, the QLineEdit presents in the format "1234.5", removing the zero and not exchange point by comma.
    I'm using setValidator (as specified below), but this works only when I edit the content of the QLineEdit, not when the values are showed.
    Please, how can I solve this problem? Thank you for your help everyone.
    -- Luciano.
    Qt Code:
    1. // Set the mask to Price.
    2. ui->leditPreco->setInputMask("000.009,99;_");
    3. // Set validator to Price.
    4. QRegExp preco("^\\d{1,3}(([.]\\d{3})*),(\\d{2})$");
    5. ui->leditPreco->setValidator(new QRegExpValidator(preco, ui->leditPreco));
    6. ui->leditPreco->setLocale( QLocale(QLocale::Portuguese, QLocale::Brazil) );
    7. ...
    8. _model = new TSqlTableModel(this); _model->setTable("Produtos");
    9. _model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    10. ...
    11. _model->setHeaderData(3, Qt::Horizontal, QObject::tr("Price"));
    12. ...
    13. ui->tviewProdutos->setModel(_model);
    14. _mapper = new QDataWidgetMapper(this); _mapper->setModel(_model);
    15. _mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    16. _mapper->addMapping(ui->leditCodigo, 0);
    17. _mapper->addMapping(ui->leditDescr, 1);
    18. _mapper->addMapping(ui->leditQtde, 2);
    19. _mapper->addMapping(ui->leditPreco, 3);
    20. // Populate the table and go to first record.
    21. _model->select();
    22. _mapper->toFirst();
    23. connect( ui->tviewProdutos->selectionModel()
    24. , SIGNAL(currentRowChanged(QModelIndex,QModelIndex))
    25. , _mapper
    26. , SLOT(setCurrentModelIndex(QModelIndex))
    27. );
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use QLineEdit with mask AND validator for money OUTPUT

    The input mask and the validator don't go together well. It's best to implement the mask using the validator API.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use QLineEdit with mask AND validator for money OUTPUT

    Quote Originally Posted by wysota View Post
    The input mask and the validator don't go together well. It's best to implement the mask using the validator API.
    Yes, I did this, as you can see in the source code, but still does not work:
    Qt Code:
    1. //removed ui->leditPreco->setInputMask("000.009,99;");
    2. QRegExp preco("^(\\d{1,3}(\\.\\d{3})*|(\\d+))(\\,\\d{2})?$");
    3. ui->leditPreco->setValidator(new QRegExpValidator(preco, ui->leditPreco));
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use QLineEdit with mask AND validator for money OUTPUT

    No, you didn't. You used both the validator and the input mask. You should subclass QValidator and implement proper validation for your data including the fixup routine.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use QLineEdit with mask AND validator for money OUTPUT

    Quote Originally Posted by wysota View Post
    No, you didn't. You used both the validator and the input mask. You should subclass QValidator and implement proper validation for your data including the fixup routine.
    Yes. You're right. I subclass and it works. Thank you, wysota!

Similar Threads

  1. QLineEdit and input mask
    By tpf80 in forum Qt Programming
    Replies: 7
    Last Post: 9th May 2014, 19:47
  2. QLineedit mask
    By sudhansu in forum Qt Programming
    Replies: 11
    Last Post: 12th March 2010, 12:10
  3. QLineEdit automatic money format.
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 31st October 2009, 07:24
  4. how to make input mask and validator work together?
    By homerli in forum Qt Programming
    Replies: 11
    Last Post: 5th June 2009, 09:53
  5. QLineEdit with money format
    By haldrik in forum Qt Programming
    Replies: 10
    Last Post: 12th September 2008, 16:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.