[英]Using QSharedPointer as Data Model
I have an application with multiple "input widgets" written as inpependent classes, that all store a shared pointer of the respective class that's data thay show.我有一个应用程序,其中包含多个编写为独立类的“输入小部件”,它们都存储了相应的 class 的共享指针,这是显示的数据。 In each of these widgets a user can see and manipulate some different data.
在这些小部件中的每一个中,用户都可以查看和操作一些不同的数据。 All of these data serves as input and also as output of a calculation.
所有这些数据都用作输入,也用作计算的 output。 Two of these widgets share the same data/class, but manipulate different members of this class.
其中两个小部件共享相同的数据/类,但操作此 class 的不同成员。
How can I achieve that manipulating the data in one widget automaticly updates the other widget?如何实现在一个小部件中操作数据会自动更新另一个小部件?
So the question is: Can I share a QSharedPointer between two classes, then connect it to a "this"-slot so that when I manipulate the pointer's value in one of these classes, the other one can do something with this data(as updating it's gui)?所以问题是:我可以在两个类之间共享一个 QSharedPointer,然后将它连接到一个“this”槽,这样当我在其中一个类中操作指针的值时,另一个可以对这些数据做一些事情(如更新它是gui)?
My intention was, since I had to store the actual data in another QSharedPointer, to make a connect on that shared pointer that tells the other widget.我的意图是,因为我必须将实际数据存储在另一个 QSharedPointer 中,所以在那个告诉另一个小部件的共享指针上进行连接。
I thought of something like this:我想到了这样的事情:
#include <QWidget>
#include <QSpinBox>
class MyWidget : QWidget // A template widget to be placed in MainWindow
{
Q_OBJECT
public:
MyWidget()
{
this->spinBox = new QSpinBox(this);
connect(this->spinBox, &QSpinBox::editingFinished, this, &MyWidget::evokedByGuiManipulation);
connect(this->data.data(), &foo::changed, this, &MyWidget::updateGui);
}
virtual ~MyWidget() {delete spinBox;}
public slots:
void setData(QSharedPointer<foo> value)
{
data = value;
}
void evokedByGuiManipulation()
{
data->manipulate(spinBox->value());
}
void updateGui()
{
this->spinBox->setValue(data->getData()->datamember);
}
protected:
QSharedPointer<foo> data;
private:
QSpinBox *spinBox;
};
UPDATE更新
And class foo is actually a wrapper that stores the actual data class baz
.而 class foo 实际上是一个包装器,用于存储实际数据
class baz
。 Like:喜欢:
class foo
{
Q_GADGET
public:
foo(){}
QSharedPointer<baz> getData() {return actualData;}
public slots :
void setActualData(QSharedPointer<baz> value)
{
actualData = value;
emit changed();
}
signals:
void changed();
private:
QSharedPointer<baz> actualData;
};
You can do this, assuming that foo::manipulate
emits a signal that your other widget connects to.您可以这样做,假设
foo::manipulate
发出一个信号,您的其他小部件连接到该信号。
I would also advise ensuring that foo::operator=
emit changed
, so that setData
updates both widgets.我还建议确保
foo::operator=
emit changed
,以便setData
更新两个小部件。
Not strictly related to your question, there are errors in the code you have shown.与您的问题不严格相关,您显示的代码中有错误。
connect(this->spinBox, &QSpinBox::editingFinished, this, &MyWidget::evokedByGuiManipulation);
editingFinished
doesn't have a QSharedPointer<baz>
to pass to evokedByGuiManipulation
. editingFinished
没有QSharedPointer<baz>
传递给evokedByGuiManipulation
。 Did you mean你的意思
data->manipulate(spinBox->value());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.