c++qtqtablewidgetqtablewidgetitem

Is there a way to display integer data inside a QTableWidget as hexadecimal?


I have a class that inherits from QTableWidget called InsnTable and one of its columns has integral data... I want to display the integers as 32-bit hexadecimal values. Is there an easy way to do so? I think of storing the data as QStrings instead of int and convert integers to hexadecimal accordingly... The problem with that, in my case, is that I have to continuously search for values in that column so I'll have to convert every single data item back to integer in order to successfully search... So is there a way to only "view" this column as hexadecimal values but have them stored regularly as integers?

I insert the integer data as follows:

void InsnTable::insertInsn(const InsnEntry &insn)
{
    this->insertRow(this->rowCount());

    QTableWidgetItem *addrValue = new QTableWidgetItem();
    uint64_t addr = insn.addr();

    addrValue->setData(Qt::EditRole, QVariant::fromValue(addr));
    this->setItem(this->rowCount() - 1, 0, addrValue);
}

Solution

  • sure:

    you can in the part where you do:

    QVariant::fromValue(addr)
    

    give a string formatted as hexadecimal, you need something like this to convert a number into a hex QString

    uint decimal = 255;
    QString hexadecimal{};
    hexadecimal.setNum(decimal,16);
    

    at the end the code can looks like:

    void InsnTable::insertInsn(const InsnEntry &insn)
    {
        this->insertRow(this->rowCount());
    
        QTableWidgetItem *addrValue = new QTableWidgetItem();
        uint64_t addr = insn.addr();
        QString hexadecimal{};
        hexadecimal.setNum(decimal,16);
    
        addrValue->setData(Qt::EditRole, QVariant::fromValue(hexadecimal));
        this->setItem(this->rowCount() - 1, 0, addrValue);
    }