当值更改时,获取 QTableWidget 中的 QComboBox 的先前值

发布于 2024-09-08 00:11:50 字数 586 浏览 2 评论 0原文

假设我有一个 QTableWidget,每一行都有一个 QComboBox 和一个 QSpinBox。考虑到我存储它们的值是 QMap> theMap;

comboBoxe 的值或旋转框值发生更改时,我想更新 theMap。所以我应该知道组合框以前的值是什么,以便替换为组合框的新值,并且还要注意旋转框的值。

我该怎么做?

PS 我决定创建一个插槽,当您单击表格时,它会存储该行组合框的当前值。但这仅在您按下行标题时才有效。在其他地方(点击comboboxspinboxQTableWidgetitemSelectionChanged()信号不起作用。

所以一般来说,我的问题是存储所选行的组合框的值,并且我将得到 ComboBoxSpinBox 甚至更改,并将处理 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<QString /*Combo box val*/,int /*spin box val*/> 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 comboBox and also take care of the value of the spin box.

How can I do this?

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 combobox or on a spinbox) 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枯寂 2024-09-15 00:11:50

如何创建您自己的派生 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 即可,该类现在另外提供旧的组合框文本。

我希望这有帮助。

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);  
};

And then just connect to myTextChangedSignal instead, which now additionally provides the old combo box text.

I hope that helps.

打小就很酷 2024-09-15 00:11:50

有点晚了,但我遇到了同样的问题并以这种方式解决:

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;
};

A bit late but I had the same problem and solved in this way:

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;
};
寄意 2024-09-15 00:11:50

我的建议是实现一个模型,这将帮助您在数据和编辑数据的 UI 之间进行清晰的分离。然后,您的模型将收到给定模型索引(行和列)更改为新数据的通知,并且您可以更改此时所需的任何其他数据。

My suggestion is to implement a model, which would help you make a clean separation between the data, and the UI editing the data. Your model would then get notified that a given model index (row and column) changed to the new data, and you could change whatever other data you needed to at that point.

网名女生简单气质 2024-09-15 00:11:50

我只是遇到了类似的问题,但对我来说,我需要以前的索引来处理一些非常琐碎的事情,因此定义和实现整个类是不合理的。

所以我所做的就是保留一个名为“previousIndex”的参数,并仅在我完成所需的所有操作后才更新它的值

I was just having a similar issue, but for me i needed the previous index for something very trivial so defining and implementing a whole class for it was unjustified.

So what I did instead was keep an argument called say 'previousIndex' and updated it's value only after I had done everything I needed with it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文