chunjiw
chunjiw

Reputation: 1201

Qt QSpinBox: How to display uppercase hexadecimal number

To use a QSpinBox to input and display hex numbers, simply set displayIntegerBase to 16. However, I couldn't find a property or method to set the display to uppercase (e.g. 1A rather than 1a).

I'm aware that I can override the textFromValue() method to do it but it feels like a fairly normal use case. There must be a simpler way to do it, right?

I'm using Qt 5.12.

Upvotes: 3

Views: 1787

Answers (1)

Cendolt
Cendolt

Reputation: 228

You can force the uppercase by setting the capitalization of your spinBox's font to QFont::AllUppercase

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

EDIT: I have prepared a small example to show the behavior

#include <QWidget>
#include <QApplication>
#include <QHBoxLayout>
#include <QSpinBox>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget *w = new QWidget();
    QLayout* layout = new QHBoxLayout(w);

    QSpinBox* spinBox = new QSpinBox(w);
    spinBox->setRange(0, 0xFF);
    spinBox->setDisplayIntegerBase(16);
    QFont font = spinBox->font();
    font.setCapitalization(QFont::AllUppercase);
    spinBox->setFont(font);

    QSpinBox* spinBox2 = new QSpinBox(w);
    spinBox2->setRange(0, 0xFF);
    spinBox2->setDisplayIntegerBase(16);

    spinBox->setValue(0x1a);
    spinBox2->setValue(0x1a);

    layout->addWidget(spinBox);
    layout->addWidget(spinBox2);

    w->show();

    return a.exec();
}

This gives the following result :

Uppercase and lowercase hexadecimal spinboxes

Upvotes: 6

Related Questions