一般 tablewidget和tableview都有滚动条,如果完整的截屏是个麻烦事。
首先想到的是滚动几次,然后每次截屏,最后把几个图片拼一起。
学过halcon和opencv的童靴,应该很简单,洒洒水了。
然而还有另一种方法,是我去qt论坛询问得到的,分享给大家:
1、把qtablewidget拖到一个scrollarea中
2、根据内容,改变qtablewidget的大小,显示全部数据,此时的滚动条是scrollarea的滚动条,最后让tablewidget渲染到一个qpixmap即可:
void MainWindow::on_pushButton_pressed() { // for readability auto table = ui->tableWidget; auto vheader = ui->tableWidget->verticalHeader(); auto hheader = ui->tableWidget->horizontalHeader(); // ask it to resize to size of all its text vheader->setSectionResizeMode( QHeaderView::ResizeToContents ); hheader->setSectionResizeMode( QHeaderView::ResizeToContents ); // tell it we never want scrollbars so they are not shown disabled vheader->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); hheader->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); // loop all rows and cols and grap sizes int iWidth = 0; int iHeight = 0; for (int i = 0; i < table->columnCount(); i++) { iWidth += hheader->sectionSize(i); } iWidth += vheader->width(); for (int i = 0; i < ui->tableWidget->rowCount(); i++) { iHeight += vheader->sectionSize(i); } iHeight += hheader->height(); QSize oldSize = table->size(); // now resize it to the size we just summed up table->resize(iWidth, iHeight); // ask it to renader to a pixmap QPixmap pixmap(ui->tableWidget->size()); table->render(&pixmap); pixmap.save("e:/test.png"); // restore org size table->resize(oldSize); }
3、恢复qtablewidget的大小