How to display multiline messages in QTableView ?
-
Hello, I am using custom model that inherits QAbstractTableModel, QTableView, and custom class inherits QSortFilterProxyModel. I am using beginInsertRows and endInsertRows in my custom model.
I am trying to display messages with multiline characters in my qtableview, but it shows only first line. When I change it to TableView->verticalHeader->setSectionResize(QHeaderView::ResizeToContentes)
. It shows multilines but it will slow down GUI. I can't even scroll down when there are lots of rows.
I tried QTableView::resizeRowsToContents() but nothing changed. I am open to any suggestions. -
Hi,
How big are these texts ?
How big is your model ? -
Trouble starts when the data I'm reading is more than 10,000 rows.
Each line has the message and other information. (I have 7 columns in my model).
The number of lines in my messages may vary depending on the data I read.
It can be 10 lines or 30.
Thanks for your reply. -
In that case you should compromise on a certain size to show your messages. Otherwise the view is going to have to do lots of calculation in order to be able to show all the different texts which are from different size.
-
I meant the cell size.
-
@SGaist
I am sorry but I dont't quite understand what you are asking for. Even though I am using basic model with 5 columns , all of my rows(100.000) have "hello", It gets stuck with QTableView::setSectionSize(QHeaderView::ResizeToContents). After I am adding some other rows, or scrolling down, It gets stuck too. I can't even make the GUI full screen.
I am curious if there is a way for only computing rowHeight only once because with this problem, TableView is updating height for every update to model or some filtering operations.
You are asking me to comprimise message certain size but even though I have "Hello" in every cell problem still exists.
Thank you for your response. -
Don't use ResizeToContent as it is recalculating everything for each cell.
How is your model implemented ?
-
@SGaist
I have a custom model that inherits QAbstractTAbleModel. Inside that model I have a container (std::vector), a function for updating my vector and another function for beginInsertRows() and endInsertRows(), and other virtual functions. (rowCount, columnCount, data) -
A vector of vector of string ?
-
struct MyCustomStruct
{
QString data1;
QString data2;
QString data3;};
// MY CUSTOM MODEL .CPP
#include "mycustommodel.h"MyCustomModel::MyCustomModel(QObject *parent) : QAbstractTableModel(parent)
{}
void MyCustomModel::parseCustomStruct(const QByteArray &data)
{
std::vector<MyCustomStruct> tempVec;
MyCustomStruct str;
auto size = data.split('\n').size();
for(;size!=0;--size){
str.data1 = "DATA1";
str.data2 = "DATA2";
str.data3 = "DATA3";
tempVec.push_back(str);
}
updateModel(tempVec);
}void MyCustomModel::updateModel(const std::vector<MyCustomStruct> vec)
{
beginInsertRows(QModelIndex(), myData.size(), myData.size() + vec.size() -1);
myData.insert(myData.end(), vec.begin(), vec.end());
endInsertRows();}
int MyCustomModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return myData.size();
}int MyCustomModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
}QVariant MyCustomModel::data(const QModelIndex &index, int role) const
{
if( !index.isValid() || role != Qt::DisplayRole )
{
return QVariant();
}
switch(index.column())
{
case 0:
return myData[index.row()].data1;
case 1:
return myData[index.row()].data2;
case 2:
return myData[index.row()].data3;}
return QVariant();
}QVariant MyCustomModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role != Qt::DisplayRole )
{
return QVariant();
}
if(orientation == Qt::Horizontal)
{
switch(section)
{
case 0:
return QLatin1String("Hallo");
case 1:
return QLatin1String("Hallo");
case 2:
return QLatin1String("Hallo");
}
}
if(orientation == Qt::Vertical)
{
return section + 1;
}
return QVariant();
}