PDA

View Full Version : [QT4] QTreeView and rows with multiple lines



KShots
17th March 2006, 15:33
I've got a field in my TreeView that takes the text from a QTextEdit and stuffs it into a row in my tree. Unfortunately, if there's a newline, I get odd output in the field. It shows half of one line, and half of another - neither of which is really readable.

Is there a way to cause the tree view to recognize the newlines and expand the row?

I saw a property called "uniformRowHeights" in my tree view... it's currently set to "false", but I still see this behaviour.

Any suggestions? Thanks.

wysota
21st March 2006, 13:52
You have to reimplement the delegate -- sizeHint() should return the size of the item needed to fit all the text (which is to be calculated using QFontMetrics) and paint() should do the painting (using QPainter::drawText() with word wrapping flag active).

Moppel
21st March 2006, 15:21
Strange,

QT 4.1.1 and QT 4.1.0 behaving differently here.
When running the attached example I'm getting a different output depending on the library I'm using. With QT 4.1.0 (picture on the right) I'm getting what I would have expected whilst with QT 4.1.1 the second line is gone.

I found that by chance and I'm wondering if that is supposed not to work anymore.



MModel *model = new MModel;
QTreeView *view = new QTreeView;
view->setModel(model);




QVariant MModel::data(const QModelIndex &index, int role) const{
if( role!=Qt::DisplayRole)
return QVariant();

const MItem *item = indexToItem(index);
int col = index.column();
switch(col) {
case 0: return item->name();
// ============================
case 1: return QString("Line1 \n Line2");
//============================
default: break;
}
return QVariant();
}

KShots
21st March 2006, 16:49
Hmm... good example.

So the question becomes, what is the intended result from the library? Should we have to go in and define a custom delegate with an adjusted size hint, or should Qt handle newlines? Should we wait for a bug fix or is this the intended behaviour?

wysota
21st March 2006, 17:59
There should be an option for both. But there isn't :) You can report a suggestion if you wish (and it it hasn't been done before).

KShots
22nd March 2006, 17:50
Well, after some searching, I was not able to find a closely related item in their bug list... so I went ahead and posted N107752. I got the following response:
On Tuesday, 21. Mar 2006 19:28 KShots@warfaresdl.com wrote:

Hello,

The example below works fine for me in the latest snapshot. I suggest
you try the latest snapshot to see if this solves your problem.


#include <QApplication>
#include <QTreeView>
#include <QStandardItemModel>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QTreeView view;
QStandardItemModel *model = new QStandardItemModel;
// Prepend 4 rows into the model
model->insertRows(0, 4);
// Prepend 4 columns into the model
model->insertColumns(0, 4);

for (int row = 0; row < 4; row++) {
for (int col = 0; col < 4; col++) {
// Return a model index for the given row and column.
QModelIndex index = model->index(row, col);
// Set the index's data to the specified value
model->setData(index, QVariant("hello\nnextline"));
}
}


view.setModel(model);
view.show();
return app.exec();
}
>
> Created a model and linked it to a QTreeView. The data() in one of the
> columns contains newline characters.
>
> What I expected to see:
>
> Some rows of the tree having multiple lines, depending on the number
> of newlines / line wraps in the data
>
> What I got instead:
>
> Each row had exactly one line of text, with some rows showing some
> portion of multiple lines of text (looks like it just looks at the
> "middle"
>
> More info:
>
> This is discussed in some more depth (with some examples) at the
> following URL:
>
> http://www.qtcentre.org/forum/showthread.php?t=1289&goto=newpost
>
> Note that it appears to be working in 4.1.0, and breaks again in 4.1.1
> according to the topic.
>

Best regards,

Sigrid Fjell Nævdal
--
Trolltech AS, Sandakerveien 116, P.O.Box 4332 Nydalen, 0402 Oslo, Norway
... so apparently, the latest snapshot works correctly. I guess that means that 4.1.2 or whatever the next official version is will have this fixed.

KShots
27th March 2006, 17:59
For the record, I have verified Moppel's observation with my own code - it performs as expected with 4.1.0, and goes back to one line in 4.1.1...

However, in 4.1.1, instead of getting a snapshot of the middle of the box, I get the top of the box. I'd call that an improvement over 4.0.1, though I'm not sure why they took a step back from 4.1.0.

Moppel
28th March 2006, 22:59
Also for the record:

I tried my example posted last week with a freshly compiled QT:
qt-x11-opensource-src-4.1.3-snapshot-20060328

And guess what: I works again. Hurray!
Looks like we have to wait for the next QT release.

Cheers,

Moppel

Moppel
30th March 2006, 18:47
http://www.trolltech.com/developer/changes/changes-4.1.2.html

- QTreeWidget
Fix multi line text items.