这里只复制文本内容 表格和图片等无法复制
只是为了少写几个报表打印模块,让客户自己去打印
bool JCode::FZTableWidget(const QTableWidget *tableWidget) //复制tableWidget全部内容 { int rows=tableWidget->rowCount(); int column=tableWidget->columnCount(); QString str=""; for (int i=0; i<column; i++){ //ui->TableWidget->horizontalHeaderItem(0)->text();//获取表头第1行第1列的内容 str+=tableWidget->horizontalHeaderItem(i)->text(); str+="\t"; } str+="\n"; for(int i=0; i<rows; i++) // 取出每个格子的内容 { for(int j=0; j<column; j++){ if(tableWidget->item(i,j) != NULL){//一定要先判断非空,否则会报错 str+=tableWidget->item(i,j)->text(); str+="\t"; }else{ str+=""; //空字符弄成空格 str+="\t"; } } str+="\n"; } QClipboard *board = QApplication::clipboard(); board->setText(str); QMessageBox::information(NULL, "信息", "复制成功!"); return true; } bool JCode::FZTableWidgetSelect(const QTableWidget *tableWidget) //复制tableWidget选中内容 { QString str=""; QList<QTableWidgetItem*> selectItems = tableWidget->selectedItems(); //qDebug() << "selectedItems("<<selectItems.size()<<")----------"; int t=0; for(auto item:selectItems) { //qDebug() << item->row() <<", "<< item->column(); int i=item->row(); int j=item->column(); if (j<=t){ str=str.left(str.length()-1); str+="\n"; } if(tableWidget->item(i,j) != NULL){//一定要先判断非空,否则会报错 str+=tableWidget->item(i,j)->text(); str+="\t"; }else{ str+=""; //空字符弄成空格 str+="\t"; } t=j; } QClipboard *board = QApplication::clipboard(); board->setText(str); QMessageBox::information(NULL, "信息", "复制成功!"); return true; }
发表评论