Question

I have struct vector of 128 items. I should put the first 64 items to first QTableWidget and remaining 64 items to second QTableWidget. It is essential to show “index” numbers as they are in my struct vector. Those are Id numbers. I have designed my ui using UI designer, I tried to change QTableWidget First row number there, but it didn’t work. How can I initialize the first tablewidget which index starts with 0 and the second tablewidget which index starts from 64. Please help guyz... Thanks in Advance.


Please Help Me to Solve this.

I will explain it clearly with a Snapshot image. ! Here i have 2 QTableWidgets. Each table widget starts with a Row id as 1. The Row id of the Second Widget should not be the same as 1. Instead of that it must be 65. (since i have 128 items, the first 64 items goes to the first table and the remaining 64 items should be placed in the second Table). Hope you all understand my requirement. Please help me. enter image description here

Was it helpful?

Solution

If the question is about vertical header labels, then the easiest way to change them in QTableWidget is to use QTableWidget::setVerticalHeaderLabels().

#include <QApplication>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QAbstractItemModel>

#define COL_NUM 2
#define ROW_NUM 5

int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    QTableWidget table(ROW_NUM, COL_NUM);
    QAbstractItemModel *model = table.model();
    QStringList labels;
    for (int i = 0; i < ROW_NUM; ++i) {
        /* Fill the row */
        for (int j = 0; j < COL_NUM; ++j) {
            QTableWidgetItem *item =
                new QTableWidgetItem(QString("item %1     %2").arg(i).arg(j));
            table.setItem(i, j, item);
        }
        /* Get row's vertical header label */
        QVariant data = model->headerData(i, Qt::Vertical);
        labels << QString("%1").arg(data.toInt() + 64);
        /* Apparently, two lines above can be replaced with one below */
        // labels << QString("%1").arg(65 + i);
    }
    /* Update vertical header labels */
    table.setVerticalHeaderLabels(labels);
    table.show();

    return app.exec();
}

QTableWidget has simpler API, but is limited in features. For example, QAbstractItemModel::setHeaderData() is no operation and not re-implemented in QTableModel. Alternative approach is to use QTableView with QAbstractItemView to hold the data. As you said you already have the data separately in some kind of structure, so you can subclass your structure and QAbstractItemView to produce your custom model to be shown by QTableView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top