PDA

View Full Version : QTableView populating question



onefootswill
5th August 2008, 04:10
I am populating a QTableView using the following code:



int row = ui.booksTable->rowCount();
ui.booksTable->insertRow(row);
QTableWidgetItem *item0 = new QTableWidgetItem;
item0->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui.booksTable->setItem(row, 0, item0);

QTableWidgetItem *item1 = new QTableWidgetItem;
item1->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui.booksTable->setItem(row, 1, item1);

QTableWidgetItem *item2 = new QTableWidgetItem;
item2->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui.booksTable->setItem(row, 2, item2);

For some reason, it is adding a spare blank row at the end. This gets messy as each time the button is pressed, the number of blank rows at the end of the table increases. How do I prevent this behaviour?

Also, I note that after you call clear() or clearContents() on a QTableView, the rowCount() does not reduce back to 0. That seems weird. I mean, shouldn't clear mean clear the table of all the rows and reduce the number of rows to zero?

spirit
5th August 2008, 09:42
this code works perfectly


QTableWidget *tw = new QTableWidget(this);
tw->setColumnCount(3);
int row = tw->rowCount();
tw->insertRow(row);
QTableWidgetItem *item0 = new QTableWidgetItem;
item0->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
tw->setItem(row, 0, item0);

QTableWidgetItem *item1 = new QTableWidgetItem;
item1->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
tw->setItem(row, 1, item1);

QTableWidgetItem *item2 = new QTableWidgetItem;
item2->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
tw->setItem(row, 2, item2);

onefootswill
5th August 2008, 23:20
There's something not right somewhere. I'm hitting the function that contains that code twice, and it is adding a 3rd row (an empty row). I cannot explain it.

aamer4yu
6th August 2008, 06:55
ui.booksTable->insertRow(row);

What if you dont call this ? Is it necessary before setItem() ??

spirit
6th August 2008, 07:46
if you remove
insertRow(row) then a row isn't created.
number of rows and columns should be specified before inserting items.

spirit
6th August 2008, 07:48
There's something not right somewhere. I'm hitting the function that contains that code twice, and it is adding a 3rd row (an empty row). I cannot explain it.

can you post code where you create a widget, table and inserting of items in the table?

onefootswill
6th August 2008, 22:16
Sure,

Here's the code:



void stuffLender::viewBkButtonSlot()
{
tag = BooksView;
setBooksEnv(tag);

switch(booksTableHit)
{
case false: populateBooks(); break;
default: break;
}

populateBooksTable();
addRowToBooks();
}

void stuffLender::populateBooks()
{
QSqlQuery queryBooks;
QSqlQuery queryBookAuthors;
bookList = new QList<book>;
int bkNr = 0;

queryBooks.exec("SELECT nr, title, edition FROM books;");
while(queryBooks.next())
{
bkNr = queryBooks.value(0).toInt();
QVector<author> authors;
QString selStmt = "SELECT nr, fname, lname, minitial FROM authors WHERE nr IN (SELECT authorNr FROM bookAuthors WHERE bookNr = ";
selStmt += QString::number(bkNr);
selStmt += ");";

queryBookAuthors.exec(selStmt);
while(queryBookAuthors.next())
{
author newAuthor(queryBookAuthors.value(0).toInt(), queryBookAuthors.value(1).toString(), queryBookAuthors.value(2).toString(), queryBookAuthors.value(3).toString());
authors.append(newAuthor);
}

book newBk(bkNr, queryBooks.value(1).toString(), queryBooks.value(2).toInt(), authors);
bookList->append(newBk);
}

booksTableHit = true;
}

void stuffLender::populateBooksTable()
{
ui.booksOnLoan->hide();
ui.booksTable->setRowCount(0);

ui.booksTable->show();
ui.booksTable->setHorizontalHeaderLabels(QStringList() << "Title" << "Author/s" << "Ed.");
int nrOfAuthors = 0;

for(int row = 0; row < bookList->count(); row++)
{
addRowToBooks();
QString authors;
const QVector<author> *theAuthors = bookList->at(row).getAuthors();
nrOfAuthors = theAuthors->count();
for(int idx = 0; idx < nrOfAuthors; idx++)
{
if(idx == nrOfAuthors - 1)
authors += theAuthors->at(idx).getFname() + " " + theAuthors->at(idx).getLname() + " " + theAuthors->at(idx).getMinitial();
else
authors += theAuthors->at(idx).getFname() + " " + theAuthors->at(idx).getLname() + " " + theAuthors->at(idx).getMinitial() + ", ";
}
ui.booksTable->item(row, 0)->setText(bookList->at(row).getTitle());
ui.booksTable->item(row, 1)->setText(authors);
ui.booksTable->item(row, 2)->setText(QString::number(bookList->at(row).getEdition()));
}
//ui.booksTable->removeRow(ui.booksTable->rowCount());
}

void stuffLender::addRowToBooks()
{
int row = ui.booksTable->rowCount();
ui.booksTable->insertRow(row);
QTableWidgetItem *item0 = new QTableWidgetItem;
item0->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui.booksTable->setItem(row, 0, item0);

QTableWidgetItem *item1 = new QTableWidgetItem;
item1->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui.booksTable->setItem(row, 1, item1);

QTableWidgetItem *item2 = new QTableWidgetItem;
item2->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui.booksTable->setItem(row, 2, item2);
}



The addRowToBooks() function is being hit twice (confirmed in debugging). So, I don't know why the 3rd empty row. It makes no sense.

spirit
7th August 2008, 08:42
I think trouble is here:


populateBooksTable();
addRowToBooks();

you insert rows several times in
populateBooksTable(); and then add extra row by calling
addRowToBooks();

onefootswill
7th August 2008, 22:29
Oh my gosh, that was such a dumb mistake. addRowToBooks should never have been called in viewBkButtonSlot at all. It must have been one of those things where I was looking at it so hard that I just could not see what was right there in front of me.

Thank you very much for your help.