Archive

Posts Tagged ‘combobox’

C++: Qt: "qBaseConverter"

August 17, 2009 Leave a comment

Today I implemented a Base Converter in C++ using Qt and after about 15 Minutes first successes have become apparent. You can convert from any Base you want to to any Base you want to (you only have to fix the “Number of Bases”-Variable if the default 30 Bases aren’t enough for you):

qBaseConverter

The basic Function (Slot) is – guess – “void convert()” that gets a Signal whenever a ComboBox or the Text at the LineEdit gets changed:

bool BaseConverter::getInputNumber(long long &number, int base) {
    bool ok;
    // Get Input-Number
    number = ui->lineEdit_input->text().toULongLong(&ok, base);
    // Return if Conversion ok
    return ok;
}
void BaseConverter::convert() {
    // Base to convert from
    int baseFrom = ui->comboBox_choiceFrom->currentIndex() + 2;
    // Base to convert to
    int baseTo = ui->comboBox_choiceTo->currentIndex() + 2;

    long long number;
    // If there is a valid Input
    if(getInputNumber(number, baseFrom)) {
        // Display the converted Number
        ui->lineEdit_output->setText(QString::number(number, baseTo));
    }
}
Follow

Get every new post delivered to your Inbox.