解決輸入數據過長致使打印預覽時報中斷問題

#define MAXQSTRINGLENGTH_NORMAL 15

#define MAXNUM_LETTER_W 9 字符串



在編輯框輸入時作文本長度控制: it

void MainWindowTable::on_textChange(QStandardItem* item)
{
if (isInputing)
{
return;
}

m_hasModify = true;



if (item->text().length()>MAXQSTRINGLENGTH_NORMAL)
{
item->setText(item->text().left(MAXQSTRINGLENGTH_NORMAL));
}
else if(item->text().length() >= MAXNUM_LETTER_W)
{
int nCountChineseStr = 0;
for (int i = 0 ;i<item->text().length();i++)
{
if (item->text().at(i)>(char)127)
{
nCountChineseStr++;
}

}

if (item->text().count(QString("W"))/*+ item->text().count(QString("M"))*/ + nCountChineseStr> MAXNUM_LETTER_W)
{
item->setText(item->text().left(MAXNUM_LETTER_W));
}

}
....... io

} model

繪製表格時提取固定長度: 數據

for (int row = 0; row < rows+1; row++)//將view表的內容畫到表格中去
{
for(int column = 0; column < colnum_num; column++)
{
QModelIndex idx = printTable->model()->index(row,column);
option.rect = printTable->visualRect(idx);

if (printTable->item(row,column))
{
QRectF rt(option.rect.left(),option.rect.top(),option.rect.width(),option.rect.height());
rt = rt.translated(offPt);
if (row != 0 && column == 0)//行頭
{
painter.drawText(rt,Qt::AlignCenter,printTable->item(row,column)->text());
}
else if (row == 0 &&printTable->item(row,column)->text() != "" )//列頭
{
painter.drawText(rt,Qt::AlignCenter,printTable->item(row,column)->text());
}
else if(printTable->item(row,column)->text() != "")
{
QString str = printTable->item(row,column)->text();
/*if(row == 10 || row == 11 || row == 12)
str = str.left(14);
else
str = str.left(8);*/
str = str.left(MAXQSTRINGLENGTH_NORMAL);
//每頁顯示5組數據,騰出的寬度分配給每列,這樣子繪製圖像時的QRect能存放14位長的字符串,纔不會想以前那樣報錯。原始數據長度不變,不存在數據截斷狀況。
painter.drawText(rt,Qt::AlignCenter,str);
}
}
}
top

這樣子就沒有了之間的報錯問題 di

相關文章
相關標籤/搜索