此时我陷入了一个困境,不知道何时发出信号与直接调用另一个类中的方法(同一线程)之间的区别。例如,在我正在进行的教程中,我将仪器类(模型)的NotifyConnected信号连接到“this”即视图管理器的onConnected插槽中,请参阅SetupViewManager :: WireButtons()中的第三行代码。(我正在使用MVVM设计模式)。在这里,信号和插槽很有意义,因为仪器类(模型)不应该知道有关视图管理器的任何信息。(即向模型传递视图管理器的引用是不可取的,因为它会破坏MVVM设计模式。)太棒了。
我的问题是,在教程中的下一步中,ViewManager的onConnected插槽随后会发出其他信号,然后我必须手动连接到另一个View类(即SetupTab)的插槽中(请参阅代码中的void SetupViewManager :: onConnected和void SetupViewManager :: WireDisplayUpdate())。
我的问题是,为什么不直接用SetupTab的公共函数(信号)调用onConnected插槽中的所有发射?感觉对我来说这样过于复杂了。
采取额外措施发出信号并连接所有内容的优点是什么呢?从另一个我拥有引用的类中简单地调用公共函数(信号)而不必连接所有内容。这不是多线程应用程序(我知道信号和插槽是线程安全的)。
请为我解惑。
谢谢。
setupviewmanager.cpp:
我的问题是,在教程中的下一步中,ViewManager的onConnected插槽随后会发出其他信号,然后我必须手动连接到另一个View类(即SetupTab)的插槽中(请参阅代码中的void SetupViewManager :: onConnected和void SetupViewManager :: WireDisplayUpdate())。
我的问题是,为什么不直接用SetupTab的公共函数(信号)调用onConnected插槽中的所有发射?感觉对我来说这样过于复杂了。
采取额外措施发出信号并连接所有内容的优点是什么呢?从另一个我拥有引用的类中简单地调用公共函数(信号)而不必连接所有内容。这不是多线程应用程序(我知道信号和插槽是线程安全的)。
请为我解惑。
谢谢。
setupviewmanager.cpp:
#include "setupviewmanager.h"
#include "View/setuptab.h"
#include "Model/instrument.h"
#include "Model/settings.h"
#include "utils.h"
namespace Ps
{
SetupViewManager::SetupViewManager(QObject *parent,
SetupTab &tab,
Instrument &inst,
Settings &config) :
QObject(parent),
m_setupTab(tab),
m_instrument(inst)
{
WireSettings(config);
config.ParseJsonData();
WireHostAndPort();
WireMessages();
WireButtons();
WireDisplayUpdate();
m_setupTab.SetHostName(config.getHostName());
m_setupTab.SetPort(config.getPortNumber());
m_setupTab.SetCommands(config.getCommandsAsModel());
auto long_wait = config.getLongWaitMs();
auto short_wait = config.getShortWaitMs();
m_instrument.SetlongWaitMs(long_wait);
m_instrument.SetShortWaitMs(short_wait);
emit NotifyStatusUpdated(tr("Long wait Ms: %1").arg(long_wait));
emit NotifyStatusUpdated(tr("Short Wait Ms: %1").arg(short_wait));
onDisconnected();
}
SetupViewManager::~SetupViewManager()
{
Utils::DestructorMsg(this);
}
void SetupViewManager::WireSettings(Settings &config)
{
connect(&config, &Settings::NotifyStatusMessage, &m_setupTab, &SetupTab::onStatusUpdated);
}
void SetupViewManager::WireHostAndPort()
{
connect(&m_setupTab, &SetupTab::NotifyHostNameChanged, &m_instrument, &Instrument::onHostNameChanged);
connect(&m_setupTab, &SetupTab::NotifyPortChanged, &m_instrument, &Instrument::onPortChanged);
}
void SetupViewManager::WireMessages()
{
connect(&m_instrument, &Instrument::NotifyErrorDetected, &m_setupTab, &SetupTab::onStatusUpdated);
connect(&m_instrument, &Instrument::NotifyStatusUpdated, &m_setupTab, &SetupTab::onStatusUpdated);
connect(this, &SetupViewManager::NotifyStatusUpdated, &m_setupTab, &SetupTab::onStatusUpdated);
}
void SetupViewManager::WireButtons()
{
connect(&m_setupTab, &SetupTab::NotifyConnectClicked,&m_instrument, &Instrument::Connect);
connect(&m_instrument, &Instrument::NotifyConnected, &m_setupTab, &SetupTab::onConnected);
connect(&m_instrument, &Instrument::NotifyConnected, this, &SetupViewManager::onConnected);
connect(&m_setupTab, &SetupTab::NotifyDisconnectClicked,&m_instrument, &Instrument::Disconnect);
connect(&m_instrument, &Instrument::NotifyDisconnected, &m_setupTab,&SetupTab::onDisconnected);
connect(&m_instrument, &Instrument::NotifyDisconnected, this, &SetupViewManager::onDisconnected);
connect(&m_setupTab, &SetupTab::NotifySendClicked,&m_instrument, &Instrument::onSendRequest);
connect(&m_instrument, &Instrument::NotifyDataSent,&m_setupTab, &SetupTab::onDataSent);
connect(&m_setupTab, &SetupTab::NotifyReceiveClicked,&m_instrument, &Instrument::onReceiveRequest);
connect(&m_instrument, &Instrument::NotifyDataReceived,&m_setupTab, &SetupTab::onDataReceived);
}
void SetupViewManager::WireDisplayUpdate()
{
connect (this, &SetupViewManager::NotifyConnectEnabled, &m_setupTab, &SetupTab::onConnectEnabled);
connect (this, &SetupViewManager::NotifyDisconnectEnabled, &m_setupTab, &SetupTab::onDisconnectEnabled);
connect (this, &SetupViewManager::NotifyDirectCommandsEnabled, &m_setupTab, &SetupTab::onDirectCommandsEnabled);
connect (this, &SetupViewManager::NotifyControlTabEnabled, &m_setupTab, &SetupTab::onControlTabEnabled);
}
void SetupViewManager::onConnected()
{
emit NotifyConnectEnabled(false); // HERE. Why not just call method directly with m_setupTab.onConnectEnabled(false); etc...?
emit NotifyDisconnectEnabled(true);
emit NotifyDirectCommandsEnabled(true);
emit NotifyControlTabEnabled(true);
}
void SetupViewManager::onDisconnected()
{
emit NotifyConnectEnabled(true);
emit NotifyDisconnectEnabled(false);
emit NotifyDirectCommandsEnabled(false);
emit NotifyControlTabEnabled(false);
}
}
ViewManager
?难道不应该在那里建立这些连接吗?如果我们对这些类一无所知,那么很难判断。 - thuga