0

I am trying to make all text in a QTextEdit capital, but currently am failing. This is my code and it does nothing.

void MainWindow::on_actionCapital_triggered()
{
    QTextCharFormat capital2;
    capital2.setFontCapitalization(QFont::AllUppercase);
    ui->textEdit->setCurrentCharFormat(capital2);
}

I am a java coder, so c++ is not my strong point

I also tried the following code with no success:

QFont font = ui->textEdit->font();
font.setCapitalization(QFont::AllUppercase);
ui->textEdit->setFont(font);

Can someone please point me to the right direction?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Ludger
  • 991
  • 2
  • 11
  • 22
  • What do you want ? That any character entered in the TextEdit ends up Upper case'd, or that the current content of the TextEdit is converted to upper case letters ? – JBL Sep 26 '13 at 21:21

1 Answers1

0

I figured it out not the most elegant solution, but it will do its job:

void MainWindow::on_actionCapital_triggered()
{
    QTextCursor c = ui->textEdit->textCursor();
    int current = c.position();
    if(capital)
    {
        QTextCharFormat capital2;
        capital2.setFontCapitalization(QFont::MixedCase);
        ui->textEdit->selectAll();
        ui->textEdit->setCurrentCharFormat(capital2);
        capital = false;
    }
    else
    {
        QTextCharFormat capital2;
        capital2.setFontCapitalization(QFont::AllUppercase);
        ui->textEdit->selectAll();
        ui->textEdit->setCurrentCharFormat(capital2);
        capital = true;
    }
    c = ui->textEdit->textCursor();
    c.setPosition(current);
    c.setPosition(current, QTextCursor::KeepAnchor);
    ui->textEdit->setTextCursor(c);
}

With this code you can switch between all Upper case and mixed case. For some reason the setCurrentCharFormat only works when the text is selected. So I had to get the current cursor position and then select all apply the FontCapitalization and then set the cursor back to where it was.

László Papp
  • 51,870
  • 39
  • 111
  • 135
Ludger
  • 991
  • 2
  • 11
  • 22