0
votes

I have a QTableView with a custom QSortFilterProxyModel for searching and sorting and a QSqlQueryModel for populating the table.

void ProxyModel::searchTable(QString name, QString type, QString date, QString time ){
    if(name_ != name)
        name_ = name;
    if(type_ != type)
        type_ = type;
    if(date_ != date)
        date_ = date;
    if(time_ != time)
        time_ = time;
    invalidateFilter();
}
bool ProxyModel::filterAcceptsRow(int source_row,
                                  const QModelIndex &source_parent) const{
    QModelIndex indName = sourceModel()->index(source_row,
                                               0, source_parent);
    QModelIndex indType= sourceModel()->index(source_row,
                                               4, source_parent);
    QModelIndex indDate = sourceModel()->index(source_row,
                                               2, source_parent);
    QModelIndex indTime = sourceModel()->index(source_row,
                                               3, source_parent);
    if(
              sourceModel()->data(indName).toString().toLower().contains(name_.toLower())
            &&sourceModel()->data(indType).toString().toLower().contains(type_.toLower())
            &&sourceModel()->data(indDate).toString().toLower().contains(date_.toLower())
            &&sourceModel()->data(indTime).toString().toLower().contains(time_.toLower())
       )
    {
        emit adjust();
        return true;
    }
    return false;
}

After a successful search, I emit a signal from my proxy model to a slot where it adjusts the table height to fit the size of the rows. connect(proxyModel, SIGNAL(adjust()), this, SLOT(dataChanged()));

And when the search button is clicked

connect(ui->searchBtn, &QToolButton::clicked, this, &AllVisitedPlaces::getSearchOptions);

I call the proxy model searchTable method with search parameters

void AllVisitedPlaces::getSearchOptions()
{
    proxyModel->searchTable(ui->nameLineEdit->text(),
                            ui->typeLineEdit->text(),
                            ui->dateLineEdit->text(),
                            ui->timeLineEdit->text());
     adjustTableSize();
}

void AllVisitedPlaces::dataChanged()
{
    adjustTableSize();
    this->verticalHeader->setSectionResizeMode(QHeaderView::ResizeToContents);

}

void AllVisitedPlaces::adjustTableSize()
{
     QRect rect = ui->table->geometry();
     int height = 0;
     for (int i =0; i < proxyModel->rowCount() ; i++)
         height+= ui->table->rowHeight(i);
     rect.setHeight(18 + ui->table->horizontalHeader()->height() + height);
         ui->table->setGeometry(rect);
         verticalHeader->setSectionResizeMode(QHeaderView::Stretch);
}

The problem is, when the table re-sizes , I lose scrolling. How can I fix that ?

Before re-sizing : Before re-sizing After re-sizing :

After re-sizing

1

1 Answers

0
votes

Why would you expect scrolling when you've resized the table to fit its contents?

The likely problem is that your Ui is broken in other ways and the table is obscured: it has been resized, but you can't see that because whatever widget the table view sits in doesn't manage the table widget properly. But we can't tell for sure because you didn't minimize your code to provide a complete compileable example of what you're doing. We're talking about <100 lines of code in all - surely it wouldn't be a big deal to just paste such a main.cpp in your question. See e.g. example 1 or example 2.

This is a bad design anyway since you're presuming that the table will fit on screen. Yet it won't: once the resizing works, you'll end up with a vertically huge window that cannot be used as some of its corners will extend past the screen, with no way to reach them to see the contents or to resize the window.

Finally, once you properly use layouts in your Ui design, the setGeometry() call on any widget below top-level is a no-op: it's the layout that controls the child widget geometry. The solution then is not to set the widget's geometry, but to set its minimum size instead.

You're facing an XY Problem: you're dead set on a solution, without telling us what it is that you're trying to achieve and making sure first that what you're after makes sense (as in: that it will actually lead to a usable Ui!).