Question

every body

i have a problem in populating Qtablewidget with editable items for the first row , and then

non editable items for the rest of rows her is my implementation so far

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableWidgetItem>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);




    QTableWidgetItem *item= NULL;
    for(int row=0; row < ui->tableWidget->rowCount(); row++)
    {
        for (int col=0; col< ui->tableWidget->columnCount(); col++)
        {
            if(row == 1)
            {
                item = new QTableWidgetItem;
                item->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled);
                ui->tableWidget->setItem(row,col,item);

            }else{

                item = new QTableWidgetItem;
                item->setFlags(Qt::NoItemFlags);
                ui->tableWidget->setItem(row,col,item);

            }
        }
    }




}

MainWindow::~MainWindow()
{
    delete ui;
}
Was it helpful?

Solution

Also, you need to set number of rows and columns before populating tableWidget:

ui->tableWidget->setRowCount(numberOfRows) and

ui->tableWidget->setColumnCount(numberOfColumnss)

Since you do not know how many rows you'll need (because you want the user to have the ability to edit lines from line to line infinity) I would do it like this:

ui->tableWidget->setRowCount(2);

...add only 2 rows ("line zero and one"), so user will have visible only two lines that he must fill. You'll need mechanism to check if all cells are filled (for example: every time cell item isChanged you check all other items if they are filled - not empty) so only when they are, user will be able to pass to editing new line, which you will add to table dynamically for every next row like this:

    rowNumber = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(rowNumber);
    for (int col=0; col< ui->tableWidget->columnCount(); col++){
        item = new QTableWidgetItem;
        item->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled);
        ui->tableWidget->setItem(rowNumber ,col,item);
    }

I hope this helps.

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