Domanda

QTextEdit needs to be tuned in such way that it should put space after each 2 symbols and it should check if these symbols are in sets from 0 to 9 or from A to F. for example I enter a2324Fcd and get A2 32 4F CD

È stato utile?

Soluzione

You can implement this behaviour manually:

void MainWindow::on_textEdit_textChanged() {
  QString text = ui->textEdit->toPlainText().toUpper();
  text.replace(QRegExp("[^A-F]"), "");
  QStringList tokens;
  for(int i = 0; i < text.length(); i += 2) {
    tokens << text.mid(i, 2);
  }
  ui->textEdit->blockSignals(true);
  ui->textEdit->setText(tokens.join(" "));
  ui->textEdit->moveCursor(QTextCursor::EndOfBlock);
  ui->textEdit->blockSignals(false);
}

Note that this implementation makes difficult to edit text in the middle of the line. If it's a problem, more complicated implementation required.

Altri suggerimenti

You can do the following:

QLineEdit le;
le.setInputMask("HH HH HH"); // Extend if more characters needed.
le.show();

BTW, QTextEdit does not seem to support input masks.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top