How to set column width using QStandardItem or QStandardItemModel?
-
I am inserting qstring in QStandardItem using setData. Inserting this QStandardItem object in QStandardItemModel object at given row & column. Consider i have multiple rows & columns. I want to set width for each column then how shall i set it, so that string will display in single line instead of multiple rows.
-
How item is wrapped and elided is a function of a view, not model.
For example QTableView (and thus QTableWidget) has a method that sets if the text should be wrapped or single line:
setWordWrap(bool)
.
To resize a column to fit the text you can either calculate the required size and resize that column via its header:
tableWidget->horizontalHeader()->resizeSection(columnIndex, 42);
or simply callresizeColumnsToContents()
on the view to let it do all that heavy lifting for you. -
@Chris-Kawa said:
resizeColumnsToContents
Thanks Chris for your reply. But i am looking for QListView. How i can set column width for that or is there any way so that list will be automatically resized as per maximum length of string?
-
QListView doesn't have columns as it displays only one column of a model at a time so I'm not sure what you're asking...
Do you mean the width of the widget? In that case that is governed by the layout it is in and you can provide hints to it like minimum width or size policy.
-
Just to understand more in details please refer the following code snippet.
QStringList NameList; NameList << "aaaaaaaaaaaa" << "bbbbbb 11111 22222" << "nnnnnn *****"; QStandardItemModel* m_Model = new QStandardItemModel(3, 3); for(row = 0; row < NameList.size(); row++) { QStandardItem* iconItem = new QStandardItem(); QString iconPath = "../test.png"; iconItem->setData(QVariant(QPixmap(iconPath)), Qt::DecorationRole); m_Model->setItem(row, 0, iconItem); QStandardItem* nameItem = new QStandardItem(); nameItem->setData(QVariant(NameList.at(row))); m_Model->setItem(row, 1, nameItem); QStandardItem* dateItem = new QStandardItem(); dateItem->setData(QVariant(QDate::currentDate().toString())); dateItem->setData(QVariant(Qt::AlignRight)); m_Model->setItem(row, 2, dateItem); } // END OF FOR LOOPn m_Model->setHeaderData(0, Qt::Horizontal, "Icon"); m_Model->setHeaderData(1, Qt::Horizontal, "Name"); m_Model->setHeaderData(2, Qt::Horizontal, "Date"); m_Model->setHeaderData(0, Qt::Horizontal, QVariant(Qt::AlignLeft | Qt::AlignVCenter), Qt::TextAlignmentRole); m_Model->setHeaderData(2, Qt::Horizontal, Qt::AlignRight, Qt::TextAlignmentRole); NameList->setColumnWidth(1, 500); myListView->SetModel(m_Model); // we have our own framework, in that APIs are developed using Qt. here myListView is object of class in our framework which is similar to QListView
Expected output :
Icon Name Date
Icon aaaaaaaaaaaa 12-08-2015
Icon bbbbbb 11111 22222 12-08-2015
Icon nnnnnn ***** 12-08-2015Actual output :
Icon Name Date
Icon aaaaaaaaa aa
aa 12-08-2015
Icon bbbbbb 11111
22222 12-08-2015
Icon nnnnnn ***** 12-08-2015 -
@neetaj said:
bbbbbb 11111 22222
in above example i want to show "bbbbbb 11111 22222" string on single line, but if you observe actual output it is showing on 2 lines. Thats why I to want to set width of column2 to some fixed width or it should increase as per contents maximum size.
-
// here myListView is object of class in our framework which is similar to QListView
Well there is no way for me (or anyone here?) to know api of your framework.
The comment is not really true because, as I said, Qt's QListView only displays a single column. It really more resembles QTreeView with just top level items.
As I mentioned earlier, Qt's views, including QListView have
setWordWrap
method that controls how text wraps. Maybe your api has something similar but since it's your framework and your type I can only guess.Btw. please surround your code with ``` so it is easier to read (I did that for you for now).