2015-11-06 77 views
0

我想QTableWidget下一個行爲來選擇:如何使`QTableWidget`項目,只有當第一列被點擊

  1. 應該排可選,只有一個行可以選擇,我可以做它與setSelectionBehavior(QAbstractItemView::SelectRows); setSelectionMode(QAbstractItemView::SingleSelection)

  2. 現在我想只有當用戶點擊第一列中的項目時選擇行。當用戶點擊其他欄目中的項目時,選擇不應改變。我應該怎麼做?

回答

0

你繼承QTableWidget這樣

#include <QMouseEvent> 
#include <QTableWidget> 

class Table : public QTableWidget { 
    virtual void mousePressEvent(QMouseEvent * event) { 
    //the selectable column index 
    const int SELECTABLE_COLUMN = 0; 

    //retrieve the cell at pos 
    QModelIndex i = indexAt(event->pos()); 

    //check if the item is in the desired column 
    if (i.column() == SELECTABLE_COLUMN) { 
     //behave as normal 
     QTableView::mousePressEvent(event); 
    } 
    //else nothing (ignore click event) 
    } 
}; 

注意

  • 重新實現mousePressEvent Works的QTableView
+0

同樣不就足夠了改變e QTableWidgetItem標誌排除Qt :: ItemIsSelectable? –

+0

不,它不會保留'setSelectionBehavior(QAbstractItemView :: SelectRows)'行爲 –

相關問題