Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resize QTableView so that the area is not scrolled anymore

Tags:

qt

I want the size of the QTableView to be the same as the table it contains (and fixed) so that it does not have a scrollbar

like image 413
yan bellavance Avatar asked Dec 04 '09 23:12

yan bellavance


2 Answers

What you could do is calculate your tableview columns widths according to the data they have (or you can just call resizeColumnToContents for each column to size it to its content). Then change the tableview width to be equal or more then total width of columns + vertical header if shown. You would also need to track model changes and adjust your tableview width + if horizontal header is shown you can track columns resize events and adjust them again. Below is some sample code for this:

initialization:

// add 3 columns to the tableview control
tableModel->insertColumn(0, QModelIndex());
tableModel->insertColumn(1, QModelIndex());
tableModel->insertColumn(2, QModelIndex());
...
// switch off horizonatal scrollbar; though this is not really needed here  
ui->tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    
// adjust size; see code below
adjustTableSize();
// connect to the horizontal header resize event (non needed if header is not shown)
connect(ui->tableView->horizontalHeader(),SIGNAL(sectionResized(int,int,int)), this,
              SLOT(updateSectionWidth(int,int,int)));
// connect to the model's datachange event
connect(ui->tableView->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
                this, SLOT(dataChanged(QModelIndex,QModelIndex)));

adjust tableview size:

void MainWindow::adjustTableSize()
{
    ui->tableView->resizeColumnToContents(0);
    ui->tableView->resizeColumnToContents(1);
    ui->tableView->resizeColumnToContents(2);

    QRect rect = ui->tableView->geometry();
    rect.setWidth(2 + ui->tableView->verticalHeader()->width() +
            ui->tableView->columnWidth(0) + ui->tableView->columnWidth(1) + ui->tableView->columnWidth(2));
    ui->tableView->setGeometry(rect);
}

process model change

void MainWindow::dataChanged(const QModelIndex &topLeft, const QModelIndex   &bottomRight)
{
    adjustTableSize();
}

process horizontal header resize

void MainWindow::updateSectionWidth(int logicalIndex, int, int newSize)
{
    adjustTableSize();
}

hope this helps, regards

like image 115
serge_gubenko Avatar answered Nov 16 '22 08:11

serge_gubenko


sum(item.sizeHint()+headeroffset+border) doesn't work well for me, there's probably spacing between the items, even if grid is off. So I made adjustment this way:

view->resizeRowsToContents();
view->resizeColumnsToContents();
QAbstractItemModel* model = view->model();
QHeaderView* horHeader = view->horizontalHeader();
QHeaderView* verHeader = view->verticalHeader();
int rows = model->rowCount();
int cols = model->columnCount();
int x = horHeader->sectionViewportPosition(cols-1) + horHeader->offset() 
        + horHeader->sectionSize(cols-1) + 1;
int y = verHeader->sectionViewportPosition(rows-1) + verHeader->offset() 
        + verHeader->sectionSize(rows-1) + 1;
QPoint p = view->viewport()->mapToParent(QPoint(x,y));
QRect g = view->geometry();
g.setSize(QSize(p.x(),p.y()));
view->setGeometry(g);

Should work if the last column and last row is visible.

like image 44
mugiseyebrows Avatar answered Nov 16 '22 09:11

mugiseyebrows