PDA

View Full Version : Adding columns to qtableview



pcheng
4th July 2012, 09:46
I would like to know if there is a way to add columns in qtableview that are not in an SQL table.

My problem is this:

I have a query that will return a number of product names. I want that to be the first column in a qtableview.
Then I want to have 3 more columns that I can edit such as setting a checkmark of which products to use and how many.

I will not be saving back to the database the table.

Any ideas will be helpful.

Thanks,

Pericles

high_flyer
4th July 2012, 10:01
do you mean QTableView or QTableWidget?

pcheng
5th July 2012, 10:12
Hmm after seeing your comment I looked into QTableWidget. It seems that QTableWidget allows you to enter data one by one so it might be able to add extra columns. I was using QTableView until now. QTableView only shows model data so that might have been the problem.

I will try QTableWidget and see how it goes.

Thanks

high_flyer
5th July 2012, 10:14
. QTableView only shows model data so that might have been the problem.
QTableWidgte also only shows model data.
But it implements common table use cases and offer API for it. :)

Krump Lee
5th July 2012, 11:39
QStandardItemModel *model = new QStandardItemModel(0, 4);

Also you can use QStandardItemModel::insertRows() and / or QStandardItemModel::insertColumns() to add rows and / or columns respectively.
Then you can set data with roles like


model->setData(index, Qt::Checked, Qt::CheckStateRole);
model->setData(index, "any string", Qt::DisplayRole);

and so on...
You can work with row and col like this:


model->setData(model->index(row, col), "any string", Qt::DisplayRole);

These must be enough to populate your model with the mysql data, then model must be passed to view to see what you did. (QTableView::setModel())

wysota
5th July 2012, 12:02
I would like to know if there is a way to add columns in qtableview that are not in an SQL table.

My problem is this:

I have a query that will return a number of product names. I want that to be the first column in a qtableview.
Then I want to have 3 more columns that I can edit such as setting a checkmark of which products to use and how many.

I will not be saving back to the database the table.

Any ideas will be helpful.

Thanks,

Pericles

You need to subclass the model you are using (presumably QSqlQueryModel or QSqlTableModel) and reimplement columnCount(), data(), index() and possibly setData() and flags() depending whether you want your additional columns to be editable.

pcheng
9th July 2012, 10:55
I ended up using QTableWidget in my code. I made one of the columns a spinbox following an example I found online. The widget seems to work fine and the spinbox works. But when I try to read the values that are in the spinbox I get nothing. Is there something that I am doing wrong?


// Populate the table widget with the products
int row = 0;
while (query.next()) {
QSpinBox *sb = new QSpinBox();
ui->tableWidget->setCellWidget(row,3, sb);
connect(sb, SIGNAL(valueChanged(int)), signalMapper, SLOT(map()));
signalMapper->setMapping(sb, row);
QTableWidgetItem *item = new QTableWidgetItem(tr("%1").arg(query.value(3).toString()));
QTableWidgetItem *item2 = new QTableWidgetItem(tr("%1").arg(itemSizes[query.value(6).toInt()]));
QTableWidgetItem *item3 = new QTableWidgetItem(tr("%1").arg(query.value(8).toInt()));
QTableWidgetItem *item4 = new QTableWidgetItem(tr("%1").arg(0));
ui->tableWidget->setItem(row, 0, item);
ui->tableWidget->setItem(row, 1, item2);
ui->tableWidget->setItem(row, 2, item3);
ui->tableWidget->setItem(row, 3, item4);
row++;
}

ui->tableWidget->resizeColumnsToContents();
ui->tableWidget->setAlternatingRowColors(true);
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);

connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(recalculate(int)));

calculateTotals();


void PackageDialog::calculateTotals()
{
int remains = 0;
int packaged = 0;

// Find the total remaining stock which is not yet packaged
for (int i=0; i < ui->tableWidget->rowCount(); i++)
{
QTableWidgetItem *itab = ui->tableWidget->item(i,2);
int itabtext = itab->text().toInt();
remains += itabtext;
qDebug() << "Remaining Value was" << itabtext << "Remaining is now" << remains;
}

ui->remainingLineEdit->setText(QString::number(remains));

// Find the total packaged stock
for (int i=0; i < ui->tableWidget->rowCount(); i++)
{
QTableWidgetItem *itab = ui->tableWidget->item(i,3);
int itabtext = itab->text().toInt();
packaged += itabtext;
qDebug() << "Packaged Value was" << itabtext << "Packaged is now" << packaged;
}

ui->packagedLineEdit->setText(QString::number(packaged));
}

The first for loop in the calculateTotals function works fine whereas the second one with the spinbox always returns 0.

Thanks,

Pericles

high_flyer
9th July 2012, 11:08
Do you mean this part?:

// Find the total packaged stock
for (int i=0; i < ui->tableWidget->rowCount(); i++)
{
QTableWidgetItem *itab = ui->tableWidget->item(i,3);
int itabtext = itab->text().toInt();
packaged += itabtext;
qDebug() << "Packaged Value was" << itabtext << "Packaged is now" << packaged;
}

You forgot(?) to get the spinbox and read the value from it.
You are asking the item for any text it has, and has none.
Ask the table for the spin box in this cell, and ask the spin box for its value.

pcheng
11th July 2012, 08:58
I get what you say high Flyer. But after searching Google for a while I cannot seem to find how to get the spinbox from the cell.

A couple of other posts here in Qt Centre have the same problems. One solution was to subclass the Spinbox and make it into a delegate while another talked about signals and slots.

Is there an easy way to get the spinbox from the cell and get the value from that spinbox?

Thanks,

Pericles

EDIT* I found another post http://www.qtcentre.org/threads/6366-QTableWidget-signals which was very helpful. I just needed to make the cell contents numbers and the spinbox would be available with no other changes. I feel stupid but that's why I am in the Newbie section. :)