How to keep rows together in QListView?
-
Hello again.
I have QListView and a delegate, delegate sets the height of row with sizeHint method.
then I switch listView to IconMode and sizeHint returns different values, but then, when I switch it back to list mode the rows have right height but there is free space between each row.
I don't want it I want them to be together what I should do? -
[quote author="Subst27" date="1421328209"]try use QAbstractListModel::data and handle Qt::SizeHintRole[/quote]
I disagree. The model should not concern itself with how it is viewed.
@tokafr: you mention that you are using different sizeHints for different view modes? Could you show your code?
Perhaps it is a matter of using two different itemDelegates for the modes.
-
[quote author="Andre" date="1421329134"]I disagree. The model should not concern itself with how it is viewed.
[/quote]really?? why?
such code in my program work perfect. Yes I'm not use SizeHint it this code, but what difference
@QVariant TTtxModel::data(const QModelIndex &index, int role) const
{
QRegExp regxp1;
regxp1.setMinimal(true);
regxp1.setPattern(QString("<foreignphrase.>.</foreignphrase>").toHtmlEscaped());QRegExp regxp2;
regxp2.setMinimal(true);
regxp2.setPattern(QString("<emphasis.>.</emphasis>").toHtmlEscaped());
switch (role)
{
case Qt::BackgroundRole:
{
//QString text=m_data[index.row()][SourceUi].toString();if (m_data[index.row()][Status].toInt()==Corrected)
return QColor::fromRgb(146, 208, 80);//0,128,255);if (m_data[index.row()][Status].toInt()==Ignored)
return QColor::fromRgb(166, 166, 166);//0,128,255);if (m_data[index.row()][Error].toInt()==GlossaryError)
return QColor::fromRgb(255, 146, 146);if (m_data[index.row()][Error].toInt()==ConsistencyError)
return QColor::fromRgb(197, 217, 241);if (m_data[index.row()][Error].toInt()==TagError)
return QColor::fromRgb(255, 255, 80);//if (m_data[index.row()][Status].toInt()==Visited)
// return QColor::fromRgb(255,255,128);//0,128,255);break;
}
case Qt::TextAlignmentRole:
{
switch (index.column())
{
case Segment:
case Line:
case Section:
case Item:
{
return QVariant(Qt::AlignVCenter | Qt::AlignRight);
break;
}
case Arch:
case SourceAttr:
case TargetAttr:
case SourceUi:
case TargetUi:
case Glossary:
case Error:
case Status:
{
return QVariant(Qt::AlignVCenter | Qt::AlignLeft);
break;
}
}
break;
}
case Qt::DisplayRole:
{
if (index.column()==Error)
{
switch (m_data[index.row()][index.column()].toInt())
{
case NoError:
{
return QString();
break;
}
case GlossaryError:
{
return tr("Glossary Error");
break;
}
case ConsistencyError:
{
return tr("Consistency Error");
break;
}
case TagError:
{
return tr("Tag Error");
break;
}
default:
{
return tr("Unknown Error");
break;
}
}
}if (index.column()==Status)
{
switch (m_data[index.row()][index.column()].toInt())
{
case NoError:
{
return QString();
break;
}
case Corrected:
{
return tr("Corrected");
break;
}
case Visited:
{
return tr("Visited");
break;
}
case Ignored:
{
return tr("Ignored");
break;
}
default:
{
return tr("Unknown");
break;
}
}
}
return m_data[index.row()][index.column()];
break;
}case Qt::UserRole:
{
return m_data[index.row()][index.column()];
break;
}
}
return QVariant();
}@ -
[quote author="Subst27" date="1421329491"][quote author="Andre" date="1421329134"]I disagree. The model should not concern itself with how it is viewed.
[/quote]really?? why?
[/quote]Separation of concerns. The model is there to provide the data in a structured way, not to determine how it is displayed. I consider those roles a design mistake in the item view framework (and not the only one either).
What happens for instance if you want to show your model in two different views at the same time, for instance like a file explorer does showing a tree with just the directories and a list or table view with the details of the selected directory? Now, put the file view in icon mode. Those are two views on the same data model (the file system). If you dictate size hints from the model, that would impact both displays... Not what you want.