2013-06-11 27 views
0

的第一行我創建基於QTableView中自己的小工具這件事情就像文件對話框(名單)我想憑直覺行事QTableView中 - 將指針(高亮選擇到列表

一個。)與全行

工作

b)中指示器還與全行

c)使用切換進入下級(子目錄)

d)運行程序之後或移動到較低的水平光標工作必須是在第一行上表(第0行)

有問題。我不能強制程序將光標放在第一行。 我嘗試了幾種方法,但沒有成功。 setCurrentIndex。 selectRow等光標總是在別的地方。沒有突出顯示,不在第一行,但是一旦它位於第4位的第10位,等等。它的行爲是不可預測的。

我該怎麼辦?

這裏我的代碼是:

mFileBoxWidget::mFileBoxWidget(QWidget *parent) : 
    QTableView(parent) 
    ,model(new QFileSystemModel()) 
{ 
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 
    this->setShowGrid(false); 
    this->verticalHeader()->setVisible(false); 
    this->installEventFilter(this); 
    model->setReadOnly(true); 
    this->setModel(model); 
    this->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); 
    this->setColumnWidth(1,70); 
    this->setColumnWidth(2,70); 
    this->setColumnWidth(3,110); 
    this->setRootIndex(model->setRootPath("C://")); 
    this->setSelectionMode(QAbstractItemView::SingleSelection); 
    this->setSelectionBehavior(QAbstractItemView::SelectRows); 
    //this->selectRow(0); //Does not work - goto first row 
    //this->setCurrentIndes(Index); //Does not work - goto index x=0 y=0 
} 

謝謝你提前爲您的所有答覆。

回答

2

解決! 問題是模型是異步的。所以讀取另一個線程中的數據。當我試圖將索引設置爲第一行時,基本上不存在。當然,解決方案是等待加載線程。此時發送信號directoryLoaded(QString)。因此,有必要等待信號,然後才設置索引。

connect(myModel, SIGNAL(directoryLoaded(QString)), this, SLOT(onLoaded())); 

void mFileBoxWidget::onLoaded() 
{ 

    QModelIndex index = myModel->index(myModel->rootPath()); 

    this->setCurrentIndex(index.child(0, index.column())); 
} 
+0

解決方案非常簡單!我怎麼沒有想到這一點?它非常明顯! 'directoryLoaded(QString)'位於文檔中的[Signals](http://qt-project.org/doc/qt-5.0/qtwidgets/qfilesystemmodel.html#directoryLoaded)部分。 – dschulz

0

這是未經測試的代碼,但我覺得這樣的事情應該工作

QModelIndex firstRow = QTableView::model()->index(0, 0); 
QTableView::selectionModel()->select(firstRow, 
           QItemSelectionModel::ClearAndSelect | 
           QItemSelectionModel::Rows); 

編輯:(2013年6月19日6時十二分58秒UTC)

簡單(和醜陋的)解決方法迄今爲止對我來說是觸發m_tableView->selectRow(0);從QTimer

這裏的示例代碼:

頁眉:

#ifndef MAINWIDGET_H 
#define MAINWIDGET_H 

#include <QWidget> 

class QTableView; 
class QFileSystemModel; 

class MainWidget : public QWidget 
{ 
    Q_OBJECT 

public: 
    MainWidget(QWidget *parent = 0); 
    ~MainWidget(); 

private: 

    void layoutWidgets(); 

    QFileSystemModel *m_model; 
    QTableView *m_tableView; 

private slots: 

    void selectFirstRow(); 

    // for debugging only 
    void selectionChanged(); 
}; 

#endif // MAINWIDGET_H 

實現:

#include "mainwidget.h" 
#include <QTableView> 
#include <QHBoxLayout> 
#include <QFileSystemModel> 
#include <QHeaderView> 
#include <QTimer> 

MainWidget::MainWidget(QWidget *parent) 
: QWidget(parent) 
{ 

    m_tableView = new QTableView(this); 
    m_model = new QFileSystemModel(this); 
    m_model->setReadOnly(true); 

    m_tableView->setModel(m_model); 

    m_tableView->setRootIndex(m_model->setRootPath(QDir::homePath())); 
    m_tableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 
    m_tableView->setShowGrid(false); 
    m_tableView->verticalHeader()->setVisible(false); 
    m_tableView->setColumnWidth(1,70); 
    m_tableView->setColumnWidth(2,70); 
    m_tableView-> setColumnWidth(3,110); 
    m_tableView->setSelectionMode(QAbstractItemView::SingleSelection); 
    m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows); 
    //m_tableView->->setSectionResizeMode(0, QHeaderView::Stretch); // Qt 5? 

    layoutWidgets(); 

    connect(m_tableView->selectionModel(),  SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged())); 

    // This works 
    QTimer::singleShot(1000, this, SLOT(selectFirstRow())); 

    // Direct invocation - doesn't works 
    // selectFirstRow(); 
} 

void MainWidget::layoutWidgets() 
{ 
    QHBoxLayout *mainLayout = new QHBoxLayout; 
    mainLayout->addWidget(m_tableView); 
    setLayout(mainLayout); 
    setFixedSize(500,500); 
} 

void MainWidget::selectFirstRow() 
{  
    m_tableView->selectRow(0); 
} 

void MainWidget::selectionChanged() 
{ 
    qDebug("Selection changed"); 
} 

MainWidget::~MainWidget() 
{} 

奇怪的是,如果QTimer::singleShot()需要與至少延遲觸發〜 25毫秒。,否則它不會在我的系統中工作。

這裏的選擇,繼承QTableView

#include "mytableview.h" 
#include <QFileSystemModel> 
#include <QHeaderView> 
#include <QTimer> 


MyTableView::MyTableView(QWidget *parent) : QTableView(parent) 
{ 

    QFileSystemModel *myModel = new QFileSystemModel; 

    setModel(myModel); 
    setRootIndex(myModel->setRootPath(QDir::homePath())); 


    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 
    setShowGrid(false); 
    verticalHeader()->setVisible(false); 
    //installEventFilter(this); 
    myModel->setReadOnly(true); 

    //setSectionResizeMode(0, QHeaderView::Stretch); // Qt 5 

    setColumnWidth(1,70); 
    setColumnWidth(2,70); 
    setColumnWidth(3,110); 

    setSelectionMode(QAbstractItemView::SingleSelection); 
    setSelectionBehavior(QAbstractItemView::SelectRows); 



    QTimer::singleShot(100, this, SLOT(selectFirstRow())); 

    connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged())); 

} 

void MyTableView::selectFirstRow() 
{ 
// qDebug("Selecting first row"); 
// QModelIndex firstRow = QTableView::model()->index(0,0); 

// if(firstRow.isValid()){ 

//  selectionModel()->select(firstRow, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows ); 
// }else{ 
//  qDebug("Invalid index"); 
// } 

    selectRow(0); 
} 

void MyTableView::selectionChanged() 
{ 
    qDebug("Selection changed."); 
} 
+0

嗯..感謝您的回覆,但這是我的一個功能失常的測試:-)。 如果我使用 - > model() - >索引(0,0),程序獲取錯誤: '((mFileBoxWidget *)this) - > mFileBoxWidget :: model'不能用作函數。當我使用 - > model-> index(0,0)without()時,程序工作,但不能將光標放在第一行上:-(。 QT 5.0中的下一個不是函數原型的索引(int row ,int column)。只有索引(int row,int column,const QModelIndex&parent = QModelIndex())const儘管構建成功了, – exo

+0

是'mFileBoxWidget'的一個'QTableView'的實例,對吧? – dschulz

+0

我會試試與'QTableView'子類作爲你的,看看我是否可以複製這個問題。 – dschulz

0

你不應該命名您的成員變量模型。 QTableView具有函數model(),編譯器認爲this-> model應該是this-> model(),因此你會得到你提到的錯誤。

+0

嗯......這是事實。我將該模型重命名爲myModel。但是,即使這種改變也沒有幫助。在我看來,該模型與QTableView沒有正確連接。但我找不到它在哪裏。 – exo