2014-04-14 81 views
1

我遇到了我的QTableViewQItemDelegate類問題。對於一列,我的委託創建一個簡單的組合框,一切正常。對於我的第二列,我需要一個在一個小部件中有兩個組合框的小部件。帶自定義小部件的QItemDelegate

我已經在我的QItemDelegate中編寫了下面的代碼,只是爲了清楚起見,這隻顯示了我的第二列的代碼,這是不起作用的代碼。沒有顯示等簡單的組合框,因爲它工作得很好:

QWidget *UserDefinedUnitsDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem & option ,const QModelIndex & index) const 
{ 
    //set up a simple widget with a layout 
    QWidget* pWidget = new QWidget(parent); 
    QHBoxLayout* hLayout = new QHBoxLayout(pWidget); 
    pWidget->setLayout(hLayout); 

    //add two combo boxes to the layout 
    QComboBox* comboEditor = new QComboBox(pWidget);  
    QComboBox* comboEditor2 = new QComboBox(pWidget); 

    //now add both editors to this 
    hLayout->addWidget(comboEditor); 
    hLayout->addWidget(comboEditor2); 
    return pWidget; 
} 

現在,這顯示就好了,但是當我修改,然後點擊其他地方它不會停止編輯。任何人都可以提供任何指針?

編輯:所以我需要在某個時候調用CommitData()和closeEditor()。任何人都可以提供指向哪裏調用這些指針?

謝謝。

+0

您的問題如何與'QItemDelegate'相關?顯示一些代碼。更新了 –

+0

以顯示此代碼位於QItemDelegate :: createEditor方法內。感謝您指出,它不是很清楚。 – user1818822

+0

你在哪裏調用'QAbstractItemView :: closeEditor' /'commitData'? –

回答

1

您可以將編輯器小部件保留爲類的成員,並在其中一個組合框的當前索引發生更改時發出commitData。因此,您可以將currentIndexChanged(int)連接到一個插槽並從那裏發出commitData:

QWidget *UserDefinedUnitsDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem & option ,const QModelIndex & index) const 
{ 
    //set up a simple widget with a layout 
    pWidget = new QWidget(parent); 
    QHBoxLayout* hLayout = new QHBoxLayout(pWidget); 
    pWidget->setLayout(hLayout); 

    //add two combo boxes to the layout 
    QComboBox* comboEditor = new QComboBox(pWidget);  
    QComboBox* comboEditor2 = new QComboBox(pWidget); 

    connect(comboEditor,SIGNAL(currentIndexChanged(int)),this,SLOT(setData(int))); 
    connect(comboEditor2,SIGNAL(currentIndexChanged(int)),this,SLOT(setData(int))); 

    //now add both editors to this 
    hLayout->addWidget(comboEditor); 
    hLayout->addWidget(comboEditor2); 
    return pWidget; 
} 

void UserDefinedUnitsDelegate::setData(int val) 
{ 
    emit commitData(pWidget); 
} 
+0

嗯,它會工作,但理想情況下,它不會設置數據,直到你完成(可能)編輯兩個組合框。我能想到的只是檢查視圖中其他地方的點擊。 – user1818822