2010-07-06 43 views
4

假設我有一個QTableWidget,並且在每一行中都有一個QComboBox和一個QSpinBox。考慮到我存儲它們的值是一個QMap theMap;獲取QComboBox的值,該值在QTableWidget中,當值更改時

當comboBoxes值或旋轉框值正在改變我想更新「theMap」。所以我應該知道組合框的前一個值是爲了替換組合框的新值,並且還要考慮旋轉框的值。

我該怎麼做?

P.S.我決定創建一個插槽,當你點擊一個表格時,它將存儲該行的組合框的當前值。但是這隻有在按下行標題時纔有效。在其他地方(單擊組合框或旋轉框上),QTableWidget的itemSelectionChanged()信號不起作用。所以一般來說,我的問題是存儲所選行的組合框的值,並且我將更改ComboBox或SpinBox,並且將輕鬆處理「theMap」。

回答

6

如何沿線的創建自己的,派生類QComboBox的東西:

class MyComboBox : public QComboBox 
{ 
    Q_OBJECT 
private: 
    QString _oldText; 
public: 
    MyComboBox(QWidget *parent=0) : QComboBox(parent), _oldText() 
    { 
    connect(this,SIGNAL(editTextChanged(const QString&)), this, 
     SLOT(myTextChangedSlot(const QString&))); 
    connect(this,SIGNAL(currentIndexChanged(const QString&)), this, 
     SLOT(myTextChangedSlot(const QString&))); 
    } 
private slots: 
    myTextChangedSlot(const QString &newText) 
    { 
    emit myTextChangedSignal(_oldText, newText); 
    _oldText = newText; 
    } 
signals: 
    myTextChangedSignal(const QString &oldText, const QString &newText); 
}; 

然後只需連接到myTextChangedSignal相反,現在還提供舊的組合框的文本。

我希望有幫助。

+0

這是好的,當然,但我怎麼能知道哪些行組合框(或旋轉框)已經被編輯? – Narek 2010-07-06 09:24:39

0

我的建議是實現一個模型,它可以幫助您在數據和編輯數據的UI之間進行清晰的分離。然後,您的模型會通知給定的模型索引(行和列)已更改爲新數據,並且您可以更改此時需要的任何其他數據。

4

有點晚了,但我有同樣的問題,並以這種方式解決:

class CComboBox : public QComboBox 
{ 
    Q_OBJECT 

    public: 
     CComboBox(QWidget *parent = 0) : QComboBox(parent) {} 


     QString GetPreviousText() { return m_PreviousText; } 

    protected: 
     void mousePressEvent(QMouseEvent *e) 
     { 
     m_PreviousText = this->currentText(); 
     QComboBox::mousePressEvent(e); 
     } 

    private: 
     QString m_PreviousText; 
}; 
+0

這是一個聰明而簡單的方法。 – user1899020 2013-07-25 01:34:32

相關問題