PDA

View Full Version : QTreeView children with multiple coulms



maxpower
19th June 2012, 15:39
How can I have children that have a second column of data? I am creating a table of contents and the parent items will properly show the second column with the page number but children do not. I have tried making the child page number a child of the parent page number. I read somewhere that children only get 1 column. Is there a way around this? I tried using HTML so that the title was aligned left and the page was aligned right in 1 column for both parent and child. However it seems the QTextDocument (inserted using QStyledItemDelegate) does not want to use the full width of the QTreeView (all of the text is together). Also, the QTextDocument does not highlight when a row is selected in the QTreeView.

Any Suggestions?



QString data = QString::fromUtf8(title) + "<span style=font-weight:bold;text-align:right;float:right>" + QString::number(page) + "</span>";


void StyledItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
QStyleOptionViewItemV4 options = option;
initStyleOption(&options, index);

painter->save();
QTextDocument doc;
qDebug() << option.rect.width();
doc.setTextWidth(option.rect.width());
doc.setHtml(index.data().toString());
//painter->save();
painter->translate(option.rect.topLeft());
doc.drawContents(painter,QRect(QPoint(0,0),option. rect.size()));
painter->restore();
}

QSize StyledItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
QStyleOptionViewItemV4 options = option;
initStyleOption(&options, index);

QTextDocument doc;
doc.setHtml(options.text);
doc.setTextWidth(options.rect.width());

QSize size = QStyledItemDelegate::sizeHint(option, index);
return QSize(size.width() > doc.idealWidth() ? size.width() : doc.idealWidth(),
size.height() > doc.size().height() ? size.height() : doc.size().height());
}

ChrisW67
20th June 2012, 00:25
Put the data in a second column in the model and let QTreeView do the work for you. Children can have more than one column.



#include <QtGui>

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

QStandardItemModel model;
QStandardItem *parent0 = new QStandardItem("Parent 0");
QStandardItem *col1 = new QStandardItem("1");
model.appendRow(QList<QStandardItem *>() << parent0 << col1);
// make some children
for (int i = 0; i < 3; ++i) {
QStandardItem *child0 = new QStandardItem(QString("Child %1").arg(i));
QStandardItem *child1 = new QStandardItem(QString("%1").arg(i));
parent0->appendRow(QList<QStandardItem *>() << child0 << child1);
}

QTreeView v;
v.setModel(&model);
v.show();

return app.exec();
}

maxpower
20th June 2012, 14:10
Thank you! I was trying to add the child's second column to the parent in the second column. Your way actually works!



QStandardItem *titleItem = new QStandardItem(QString::fromUtf8(title));
model->setItem(row,0,titleItem);
QStandardItem *pageParentItem = new QStandardItem(QString::number(page));
model->setItem(row,1,pageItem);
.
.
.
QStandardItem *titleItem = new QStandardItem(QString::fromUtf8(title));
titleItem->setData(outline->dest.ld.gotor.page,33);
titleParentItem->appendRow(titleItem);
QStandardItem *pageItem = new QStandardItem(QString::number(page));
pageParentItem->appendRow(pageItem);