<转>有关QT表格
void QCurveQueryTool::printview() //printFlag =2 , 打印预览
{
QPrinter printer(QPrinter::ScreenResolution);
QPrintPreviewDialog preview(&printer, this);
preview.setMinimumSize(1000,900);
connect(&preview, SIGNAL(paintRequested(QPrinter *)),this, SLOT(printTable(QPrinter *)));
preview.exec();
}
void QCurveQueryTool::printTable(QPrinter *printer)
{
QString title = QString("曲线打印");
printTableWidget(table,title,printer);
}
void QCurveQueryTool::printTableWidget(const QTableWidget* view,QString stitile,QPrinter *printer) //printFlag =1,打印表格
{
double totalWidth = 0.0;
double totalHeight = 0.0;
double totalPageHeight=0.0;
int rows = view->model()->rowCount(); //行总数
const int cols = view->model()->columnCount(); //列总数
for (int c = 0; c < cols; ++c) //求出列宽
{
totalWidth += view->columnWidth(c);
}
for (int r = 0; r < rows; ++r) //求出行宽
{
totalHeight += view->rowHeight(r);
}
QPainter painter(printer);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.begin(printer);
QRect area = printer->paperRect();// paperRect();
QVector<int> startRow;
QVector<int> endRow;
int iCount = 0;
qreal left,top,right,bottom;
printer->getPageMargins(&left,&top,&right,&bottom,QPrinter::Point);
cout << "left = " << left << ", right = " << right << ", top = " << top << ", bottom = " << bottom << endl;
area.setHeight( int(area.height() - top - bottom) );
area.setWidth( int(area.width() - left - right) );
for (int p=0; p < rows; p++) //rows中保存了所有的行数 , 找出每次打印的开始行和结束行
{
totalPageHeight += view->rowHeight(p); //求出所有的列高度
int pageFooter = 50 ; //页脚的高度
int pageHeader = 50; //页眉的高度
if (startRow.size() == 0)
{
pageFooter = 50 ;
pageHeader = 500;
}
if ((totalPageHeight >= (area.height() - pageFooter - pageHeader)) || (p == rows -1)) //如果目前累加列的高度大于或者等于可用页面高度 || 到达最后一行
{
totalPageHeight = view->rowHeight(p);
if (p == rows -1)
{
if (p - iCount - 2 < 0)
{
startRow.push_back(0);
}
else
{
startRow.push_back(p - iCount - 2);
}
endRow.push_back(p);
//cout << startRow.at(startRow.size() - 1) << " " << endRow.at(endRow.size() - 1) << endl;
}
else
{
if (p - iCount - 2 < 0)
{
startRow.push_back(0);
endRow.push_back(p - 2);
}
else
{
startRow.push_back(p - iCount - 1);
endRow.push_back(p - 3);
}
//cout << startRow.at(startRow.size() - 1) << " " << endRow.at(endRow.size() - 1) << endl;
}
iCount = 0;
}
iCount++;
}
QTableWidget *printTable = new QTableWidget();
//printTable->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
//printTable->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
//printTable->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
//printTable->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
const double xscale = area.width() / totalWidth;
const double yscale = area.height() / totalHeight;
//const double pscale = area.height() / totalPageHeight;
//painter.scale(xscale , yscale); // With the scaling 3 It is Printing all
//painter.translate(area.x() + xscale, area.y() + pscale); //This is original
painter.translate(area.x() + xscale, area.y() + yscale);
//painter.save(); //commented
//int y=0 ;// This is introduced for the columns
//view->paintEvent(&event);
//QPainter paint(this);
//paint.setPen(Qt::red);
//paint.drawRect(0, 0, 0, 0);
QStyleOptionViewItem option;
QPen pen;
pen.setColor(Qt::black);
pen.setWidth(1);
painter.setPen(pen);
int irowcount = -1;
float rate = 1;
for(int ipage = 0; ipage < startRow.size();ipage++) //startRow有大,就有多少页需要打印
{
printTable->setColumnCount(view->columnCount() + 1); //打印的列数为view的列数
printTable->setRowCount(endRow[ipage] - startRow[ipage] + 2); //设置当前打印页的行数
for(int c = 1;c < cols + 1;c++)
{
printTable->setColumnWidth( c,(view->columnWidth(c - 1)) );
QTableWidgetItem *newItem = printTable->item(0,c);
if (!newItem) //如果printTable的首行不存在,添加文字或者序号
{
if (view->horizontalHeaderItem(c - 1)) //Returns the horizontal(水平的) header item for column
newItem = new QTableWidgetItem(view->horizontalHeaderItem(c - 1)->text());
else
newItem = new QTableWidgetItem(QString::number(c));
printTable->setItem(0,c ,newItem);
}
else //否则,直接设置文字或者序号
{
if (view->horizontalHeaderItem(c - 1))
newItem->setText(view->horizontalHeaderItem(c - 1)->text());
else
newItem->setText(QString::number(c));
}
}
/求出合适的比率,用来完美表格的显示
QRect firstTable , lastTable;
QModelIndex firstIndex = printTable->model()->index(0,0);
QModelIndex lastIndex = printTable->model()->index(0,cols);
firstTable = printTable->visualRect(firstIndex);
lastTable = printTable->visualRect(lastIndex);
float totalTableWidth = lastTable.topRight().x() - firstTable.topLeft().x();
float pagerWidth = printer->paperRect().width() - 20 - 20;
rate = totalTableWidth/pagerWidth;
cout << "totalTableWidth = " << totalTableWidth << ", pagerWidth = " << pagerWidth << ", rate = " << rate << endl;
/求出合适的比率,用来完美表格的显示
for(int c = 1;c < cols + 1;c++) //重设宽度
{
printTable->setColumnWidth( c,int((view->columnWidth(c - 1))/rate) );
}
printTable->setColumnWidth(0,view->verticalHeader()->width()); //Returns the table view's vertical header.
int iRow = 0;
for(int ir = startRow[ipage]; ir <= endRow[ipage];ir++) //ir变量用来标识当前打印的是哪些行,设置当前打印页的首行
{
printTable->setRowHeight(iRow,view->rowHeight(ir)); //Sets the height of the given row to be height.
for(int ic = 0; ic < view->columnCount(); ic++) //设置每个单元格的内容
{
QTableWidgetItem *newItem = printTable->item(iRow + 1,ic + 1);
if (!newItem)
{
if (view->item(ir,ic))
newItem = new QTableWidgetItem(view->item(ir,ic)->text());
else
newItem = new QTableWidgetItem("");
printTable->setItem(iRow + 1,ic + 1,newItem);
}
else
{
if (view->item(ir,ic))
newItem->setText(view->item(ir,ic)->text());
else
newItem->setText("");
}
}
iRow++;
}
qreal ioffx = (area.width() - totalWidth)/2;
qreal ioffy = 50;
if (ipage == 0)
{
ioffy = 500;
QwtPlotPrintFilter filter;
int options = QwtPlotPrintFilter::PrintAll;
options &= ~QwtPlotPrintFilter::PrintBackground;
options |= QwtPlotPrintFilter::PrintFrameWithScales;
filter.setOptions(options);
d_plot->print(&painter,QRect(50,65,650,400),filter);
}
rows = printTable->rowCount();
iRow = 1;
for(int ir = startRow[ipage] ; ir <= endRow[ipage] ; ir++) //ir变量用来标识当前打印的是哪些行,设置当前打印页的首列序号
{
QTableWidgetItem *newItem = printTable->item(iRow,0);
if (!newItem)
{
if (view->verticalHeaderItem(ir))
newItem = new QTableWidgetItem(view->verticalHeaderItem(ir)->text());
else
newItem = new QTableWidgetItem(QString::number(iRow));
printTable->setItem(iRow,0,newItem);
}
else
{
if (view->verticalHeaderItem(ir))
newItem->setText(view->verticalHeaderItem(ir)->text());
else
newItem->setText(QString::number(iRow));
}
iRow ++;
}
QPointF offPt(20,ioffy); //20表示边框的空格
for (int r = 0; r < rows; r++)
{
QRect rt1,rt2;
QModelIndex idx1 = printTable->model()->index(r,0);
QModelIndex idx2 = printTable->model()->index(r,cols);
rt1 = printTable->visualRect(idx1);
rt2 = printTable->visualRect(idx2);
painter.drawLine( (rt1.bottomLeft() + offPt) , (rt2.bottomRight() + offPt) );
if (r == 0)
{
painter.drawLine( (rt1.topLeft() + offPt) , (rt2.topRight() + offPt) );
}
}
for (int c = 0; c < cols + 1; c++)
{
QRect rt1,rt2;
QModelIndex idx1 = printTable->model()->index(0,c);
QModelIndex idx2 = printTable->model()->index(rows - 1,c);
rt1 = printTable->visualRect(idx1);
rt2 = printTable->visualRect(idx2);
painter.drawLine( (rt1.topLeft() + offPt) , (rt2.bottomLeft() + offPt) );
if (c == cols)
{
painter.drawLine( (rt1.topRight() + offPt) , (rt2.bottomRight() + offPt) );
}
}
for (int r = 0; r < rows; r++)
{
irowcount ++;
for(int c = 0; c < cols + 1; c++)
{
QModelIndex idx = printTable->model()->index(r,c);
option.rect = printTable->visualRect(idx);
if (printTable->item(r,c))
{
QRectF rt(option.rect.left() + 3,option.rect.top(),option.rect.width(),option.rect.height());
rt = rt.translated(offPt);
if (r != 0 && c == 0)
{
painter.drawText(rt,Qt::AlignCenter,QString("%1").arg(irowcount));
}
else
{
if (r == 0)
{
painter.drawText(rt,Qt::AlignCenter,printTable->item(r,c)->text());
}
else
{
painter.drawText(rt,Qt::AlignVCenter,printTable->item(r,c)->text());
}
}
}
}
}
//ipage
if (ipage == 0) //设置标题
{
QRectF rttt(area.left(),area.top(),area.width(),50);
painter.drawText(rttt,Qt::AlignCenter,stitile);
}
QRectF rttt(area.left(),area.bottom() - 65,area.width(),25);
painter.drawText(rttt,Qt::AlignCenter,QString::number(ipage + 1));
if (ipage < startRow.size() - 1)
{
printer->newPage();
irowcount--;
}
}
painter.end();
}
打印图片:(打印chuang)
QPrinter printer;
QPainter painter(&printer);
painter.drawPixmap(0, 0, *(m_pZoomWnd->m_pLayoutImage));
painter.end();
有关打印预览:
QTable tb(.....);
QPixmap pix( tb.size() );
bitBlt( &pix, 0, 0, &tb );