Re: [問題] QT QTableView 換行?
想到一個笨方法~~就自己用畫的
實做Delegate來達成,應該還有更簡單的方法吧~~QQ
QSize Delegate::sizeHint( const QStyleOptionViewItem &option,const
QModelIndex &index ) const
{
return QSize( 100, 100); // 自己調整大小~~
}
void Delegate::paint( QPainter *painter, const QStyleOptionViewItem
&option, const QModelIndex &index ) const
{
// 取值
QString value = index.model()->data( index, Qt::DisplayRole ).toString();
// 開始畫~~
painter->save();
if( value.length() > 20 ) // 要取sizeHint的值來決定在哪一個換行
value[10] = '\n'; // 20是我自己懶惰取的
// 若直接畫出來 value,UI顯示是空白,而不是換行,why??
// 所以我把它切成兩個字串
QStringList strlist = value.split('\n');
// 自己調整大小字要在哪裡畫出來,下面的值是亂弄得,也要根據sizeHint
QPointF point(option.rect.x() ,option.rect.y()/2);
QPointF point1(option.rect.x() ,option.rect.y()/2 + 10);
QPointF point2(option.rect.x() ,option.rect.y()/2 + 20);
// 開始畫字串
painter->drawText ( point , strlist.at(0));
painter->drawText ( point1 , QString("\n"));
painter->drawText ( point2 , strlist.at(1));
painter->restore();
}
※ 引述《Xphenomenon (啦 )》之銘言:
: 請問如何在 QTableView 中將項目的資料自動換行,
: 我的資料如果過大的話,他會一直延伸到 horizontal scroll bar 出現,
: 我希望他可以自動換行,我有試過 resizeRowsToContents(),但似乎
: 不是總是成功,請問有人有類似的問題嗎? 麻煩了,謝謝!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.174.36.103
推
02/27 22:32, , 1F
02/27 22:32, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章
73
179