清單工具集 (List Widget):裝置清單
清單工具集顯示裝置清單,此工具及名稱為 listDevices,含連接至位置 currentIndexChanged 的 currentRowChanged 信號,我們宣告了變數 currentIndex 為軸索引,當選定的列更改時 currentRowChanged 即被觸發,而 currentIndexChanged 將執行以將選定軸的索引設至 currentIndex。
以下代碼在 QtGui.cpp
中:
QObject::connect(ui->listDevices, &QListWidget::currentRowChanged, ks, &ksWorker::currentIndexChanged);
以下代碼在 ksworker.cpp
中:
void ksWorker::currentIndexChanged(int index)
{
if (index >= 0)
currentIndex = index;
}
listDevices 使用如下:首先連接 KINGSTAR 信號 sendName 至位置 updateDevices (QtGui.cpp),當 EtherCAT 連結建立後呼叫 setCurrentRow 以將在 listDevices 內的當前列設為零 (QtGui.cpp) 並發送 sendName (ksworker.cpp),一旦 sendName 被觸發,程式即執行 updateDevices (QtGui.cpp),其中 listDevices 呼叫函式 insertItem (QtGui.cpp),此函式將項目插入清單中的位置,因先前已將列設為零,項目將從位置零插入,而當斷開 KINGSTAR 子系統的連結時,使用 clear 以移除所有在 listDevices (QtGui.cpp) 的項目。
QObject::connect(ks, &ksWorker::sendName, this, &QtGui::updateDevices);
ui->listDevices->setCurrentRow(0);
emit sendName(i, ss->Name);
void QtGui::updateDevices(int index, char* name)
{
ui->listDevices->insertItem(index, new QListWidgetItem(name, ui->listDevices));
}
ui->listDevices->clear();