What layout to use for data listing if I don't need most of the model features?



  • I have a set that stores a list of numbers and I want to output them in a Qdialog. I also want to apply an conversion to each number and output them as well. The layout I'm thinking is vertical table like layout that holds the number and the conversion result in two columns like this

    Number            Conversion Result
     300                6500 from 300
     350                7000 from 350
     400                7500 from 400
    

    I have tried out the table/view stuffs like QStandardItemModel and QStringListModel, but I don't need their features. For example, I don't need the data to be modifiable when double clicked, and I don't need sorting when the header was clicked. All I need are to highlight row when the row was clicked and the table be scrollable for longer list.

    Do I still need model for this? And what layout should I be using?


  • Lifetime Qt Champion

    Hi
    I think using a QTableWidget would be the fastest way.
    You dont need a model for that. just insert items.
    https://wiki.qt.io/How_to_Use_QTableWidget



  • @mrjj

    Thanks I got it to work.

    ui->setupUi(this);    
    ui->tableWidget->setColumnCount(2);    
    ui->tableWidget->setRowCount(numberSet.size());
    
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    
    QStringList tableHeader = {"Number", "Result"};
    ui->tableWidget->setHorizontalHeaderLabels(tableHeader);
    
    int rowCounter = 0;
    for(int i : numberSet)
    {
        ui->tableWidget->setItem(rowCounter, 0, new QTableWidgetItem(QString::number(i)));
        ui->tableWidget->setItem(rowCounter, 1, new QTableWidgetItem(someConversion(i)));
        rowCounter++;
    }
    

Log in to reply
 

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