Important: Please read the Qt Code of Conduct -

QTableView: how to take screenshot (grab widget) of complete content

  • Hello,

    is it possible, to take a screenshot of the complete widget that is contained in a QTableView?
    Think QScrollArea->widget()->grab().

    Background: I want to create a kind of overview scrollbar like SublimeText does.
    I want to display a miniature version of the complete contents of the QTableView at the side of the TableView to be able to

    • get an overview
    • use it to navigate (scroll) in the tableview

    Thus it is important for me, to get a representation of all the content in the QTableView and not just the parts that are visible through the viewport.

    Best regards,

  • Hi, Markus,
    did you finish this function? I also have this problem. anyone can help?

  • There is no "complete contents", only the portion that fits in the viewport is drawn. With model batch loading, the view may not even has access to all the data needed.

  • @ChrisW67
    Let's just change a widget, like Qtablewidget, I add data to it more than a vertical screen, (maybe need 2 vertical screen). the widget will show scrollbar when it cannot show all.
    Now i snap the content, how to see the full content in one picture? do i need scroll and take two picture, and finally combine to one picture? is there a combination algorithm inside Qt? if not , should we use opencv or halcon?

  • Lifetime Qt Champion


    Scrolling and combine to one picture could work but will be messy to get to align up.

    You could place the table in a scroll area so we can freely resize it

    alt text

    and then you can ask it to render to a pixmap like this

    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
        // 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());
        // restore org size

    and you get a pixmap with it all.

    alt text

  • @mrjj seems a great idea, i will try later and thank you.