获取QComboBox的上一个值,该值在QTableWidget中更改时 [英] Get previous value of QComboBox, which is in a QTableWidget, when the value is changed

查看:1486
本文介绍了获取QComboBox的上一个值,该值在QTableWidget中更改时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个QTableWidget,在每一行有一个QComboBox和一个QSpinBox。考虑我存储他们的值是一个QMap theMap;

Say I have a QTableWidget and in each row there is a QComboBox and a QSpinBox. Consider that I store their values is a QMap theMap;

当comboBoxes值或spin box值被更改时,我想更新theMap。因此,我应该知道为了用组合框的新值替换组合框的前一个值,并且处理旋转框的值。

When comboBoxes value or spin boxes value is being changed I want to update "theMap". So I should know what was the former value of the combo box in order to replace with the new value of the combo box and also take care of the value of the spin box.

我该如何做?

PS我决定创建一个插槽,当您单击一个表,它存储该行的组合框的当前值。但这只有当你按行标题。在其他地方(点击组合框或旋转框)的QTableWidget的itemSelectionChanged()信号不工作。所以一般来说,我的问题是存储所选行的组合框的值,并且我将获得ComboBox或SpinBox更改,并将轻松处理theMap。

P.S. I have decided to create a slot that when you click on a table, it stores the current value of the combo box of that row. But this works only when you press on row caption. In other places (clicking on a combo box or on a spin box) itemSelectionChanged() signal of QTableWidget does not work. So in general my problem is to store the value of the combo box of selected row, and the I will get ComboBox or SpinBox change even and will process "theMap" easily.

推荐答案

如何创建自己的,派生的QComboBox类,如下:

How about creating your own, derived QComboBox class, something along the lines of:

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

我希望对您有帮助。

这篇关于获取QComboBox的上一个值,该值在QTableWidget中更改时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆