QTabelView with Arabic-Indic numerals for the vertical header



  • I'm working on a project that requires arabic-indic numerals for numbering the vertical header. I've managed to set them manually as follows:

    m_model = new QStandardItemModel(this);
    m_model = new QStandardItemModel(15,3,this);
    m_model->setHorizontalHeaderItem(0, new QStandardItem(QString("الاول")));
    m_model->setHorizontalHeaderItem(1, new QStandardItem(QString("الثاني")));
    m_model->setHorizontalHeaderItem(2, new QStandardItem(QString("الثالث")));
    QStringList vertHeaders;
    vertHeaders << "١" << "٢" << "٣";
    m_model->setVerticalHeaderLabels( vertHeaders );
    ui->tableView->setModel(m_model);
    ui->tableView->setLayoutDirection(Qt::RightToLeft);
    ui->tableView->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
    

    With the following output:

    0_1515304836022_Untitled.png

    How to configure it and use Arabic-Indic as a default numbering system?



  • I would go as far as to say this is a Qt Bug.
    The problem is https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qheaderview.cpp.html#2765 is using QVariant::toString instead of QLocale::toString

    You'd need to subclass QHeaderView and reimplement void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const copy paste basically everything, wiggle out of using private stuff and replace opt.text = d->model->headerData(logicalIndex, d->orientation, Qt::DisplayRole).toString(); with:

    const QVaraiant textData = model()->headerData(logicalIndex, orientation(),Qt::DisplayRole);
    switch(textData.type()){
    case QMetaType::Int:
     opt.text = locale().toString(textData.toInt());
    break;
    // other cases for double/float, unsigned int and other signed/unsigned things (short, long long, etc), and dates/times
    default:
     opt.text = textData.toString();
    }
    
    


  • @VRonin Thank you for the answer. Would you please elaborate how can I subclass QHeaderView and how to pass the customized class to TableView. Thank you.


  • Lifetime Qt Champion

    Hi,

    QTableView::setVerticalHeader.

    class LocalizedHeaderView : public QHeaderView
    {
    protected:
        void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
        {
            // The code
            const QVariant textData = model()->headerData(logicalIndex, orientation(),Qt::DisplayRole);
            switch (textData.type()) {
                case QMetaType::Int:
                opt.text = locale().toString(textData.toInt());
                break;
                // other cases for double/float, unsigned int and other signed/unsigned things (short, long long, etc), and dates/times
            default:
                opt.text = textData.toString();
            }
            // Rest of the code
        }
    };
    


  • @SGaist
    I got this error

    error: 'd_func' is a private member of 'QHeaderView'
            Q_D(const QHeaderView);
    


  • @VRonin said in QTabelView with Arabic-Indic numerals for the vertical header:

    wiggle out

    How do I avoid private members?


  • Lifetime Qt Champion

    Just thought of a possibly simpler way: why not subclass the headerData method and fix there the returned value ?



  • @SGaist could you please elaborate a bit?


  • Qt Champions 2017

    @CroCo
    Hi
    headerdata is a virtual function in the model
    http://doc.qt.io/qt-5/qabstractitemmodel.html#headerData
    It returns the data used in headers for any HedaderView attached.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.