Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use QLineEdit with mask AND validator for money OUTPUT
Forum Updated to NodeBB v4.3 + New Features

Use QLineEdit with mask AND validator for money OUTPUT

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lubrodi
    wrote on last edited by
    #1

    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.
    @
    // Set the mask to Price.
    ui->leditPreco->setInputMask("000.009,99;_");
    // Set validator to Price.
    QRegExp preco("^\d{1,3}(([.]\d{3})*),(\d{2})$");
    ui->leditPreco->setValidator(new QRegExpValidator(preco, ui->leditPreco));
    ui->leditPreco->setLocale( QLocale(QLocale::Portuguese, QLocale::Brazil) );
    ...
    _model = new TSqlTableModel(this); _model->setTable("Produtos");
    _model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ...
    _model->setHeaderData(3, Qt::Horizontal, QObject::tr("Price"));
    ...
    ui->tviewProdutos->setModel(_model);
    _mapper = new QDataWidgetMapper(this); _mapper->setModel(_model);
    _mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    _mapper->addMapping(ui->leditCodigo, 0);
    _mapper->addMapping(ui->leditDescr, 1);
    _mapper->addMapping(ui->leditQtde, 2);
    _mapper->addMapping(ui->leditPreco, 3);
    // Populate the table and put in first record.
    _model->select();
    _mapper->toFirst();
    connect( ui->tviewProdutos->selectionModel()
    , SIGNAL(currentRowChanged(QModelIndex,QModelIndex))
    , _mapper
    , SLOT(setCurrentModelIndex(QModelIndex))
    );@

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      I'm not sure what do you mean. Which format the data you're trying to set has? An integer / a double you need to format in a specific way? ( Qt 4.8 will introduce QLocale support for currencies (f.i. QLocale::toCurrencyString). )

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lubrodi
        wrote on last edited by
        #3

        [quote author="peppe" date="1313510909"]I'm not sure what do you mean. Which format the data you're trying to set has? An integer / a double you need to format in a specific way? ( Qt 4.8 will introduce QLocale support for currencies (f.i. QLocale::toCurrencyString). )[/quote]

        It is a double. But someone suggested to me subclass QValidator to validate what is introduced in the lineedit. I did this and it worked.
        Thank you!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved