2017-03-31 53 views
0

我有一個用於搜索和排序的自定義QSortFilterProxyModel以及用於填充表的QSqlQueryModel的QTableView。動態調整大小後QTableView滾動停止

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; 
} 

搜索成功後,我發出信號,從我的代理模式,它調整工作臺的高度以適合行的大小的槽。 connect(proxyModel,SIGNAL(adjust()),this,SLOT(dataChanged()));

而當搜索按鈕被點擊

連接(UI-> searchBtn,& QToolButton ::點擊的,這一點,& AllVisitedPlaces :: getSearchOptions);

我打電話與搜索代理模型searchTable方法參數

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); 
} 

的問題是,當表重新大小,我失去了滾動。 我該如何解決這個問題? Before re-sizing 調整大小後:

After re-sizing

回答

0

爲什麼你會想到,當你調整大小以適應其內容的表格滾動

重新上漿之前?

可能的問題是您的用戶界面被其他方式破壞,表格被遮擋:它已被調整大小,但您無法看到,因爲無論窗口視圖所在的任何小部件都不能正確管理表格窗口小部件。但是我們無法確定,因爲您沒有最小化您的代碼以提供完整的可編譯的示例。我們在討論的是100行代碼 - 當然,在你的問題中粘貼這樣一個main.cpp並不是什麼大問題。見例如example 1example 2

無論如何,這是一個糟糕的設計,因爲您認爲桌子適合放在屏幕上。然而,它不會:一旦調整大小的作品,你會最終得到一個垂直巨大的窗口,因爲它的一些角落將延伸到屏幕上,所以無法使用,而無法讓他們看到內容或調整大小窗口。

最後,一旦在UI設計中正確使用佈局,調用頂層下面的任何窗口小部件的setGeometry()是無操作的:它是控制子窗口小部件幾何的佈局。那麼解決方案不是設置控件的幾何圖形,而是設置而不是

你正面臨一個XY問題:你已經死定了一個解決方案,而沒有告訴我們你想要達到什麼目標,並確保首先你知道什麼是有意義的(如:它實際上會導致一個可用的UI!)。

+0

我的TableView已經在gridLayout中。 但是你是對的,setGeometry()是什麼導致了這個問題。 我在表格佈局下添加了一個垂直間隔,並將佈局拉伸爲[10:1],然後我使用setMaximumHeight()來代替它。 我還刪除了冗餘/錯誤代碼,以更改模型數據中的每個更改的大小,並在所有新數據加載後調用adjustTableSize()。 謝謝,請原諒我,我只是一個初學者。 –