• 5243阅读
  • 5回复

[提问]命令数据交互有什么比较好的实现方法 [复制链接]

上一主题 下一主题
离线jemyzhang
 

只看楼主 正序阅读 楼主  发表于: 2013-03-22
准备做一个东西,用串口或者telnet,界面上有很多button,每个button表示一个寄存器数据,按下后要通过串口或者telnet发送命令到目标版,但是目标版比较慢,运行一个命令后大概要1-2s才有数据返回,然后我解析数据,获取寄存器数据。
为了防止一下子发出很多命令(不停的点其他button),有没有比较好的办法可以实现?

目前使用disable界面的办法,但是经常disable的话,界面会忽闪忽闪的,很难看。
离线jemyzhang

只看该作者 5楼 发表于: 2013-09-29
现在发送用同步,接收用异步的方法,接收到的数据统一分析后emit signal返回结果,效果比较好。感谢各位的帮助
离线jeffreylee

只看该作者 4楼 发表于: 2013-03-25
回 3楼(jemyzhang) 的帖子
QSignalMapper 可能就是你想要的

The QSignalMapper class is provided for situations where many signals are connected to the same slot and the slot needs to handle each signal differently.
Suppose you have three push buttons that determine which file you will open: "Tax File", "Accounts File", or "Report File".
In order to open the correct file, you use QSignalMapper::setMapping() to map all the clicked() signals to a QSignalMapper object. Then you connect the file's QPushButton::clicked() signal to the QSignalMapper::map() slot.
    signalMapper = new QSignalMapper(this);
    signalMapper->setMapping(taxFileButton, QString("taxfile.txt"));
    signalMapper->setMapping(accountFileButton, QString("accountsfile.txt"));
    signalMapper->setMapping(reportFileButton, QString("reportfile.txt"));

    connect(taxFileButton, &QPushButton::clicked,
        signalMapper, &QSignalMapper::map);
    connect(accountFileButton, &QPushButton::clicked,
        signalMapper, &QSignalMapper::map);
    connect(reportFileButton, &QPushButton::clicked,
        signalMapper, &QSignalMapper::map);
Then, you connect the mapped() signal to readFile() where a different file will be opened, depending on which push button is pressed.
    connect(signalMapper, SIGNAL(mapped(QString)),
        this, SLOT(readFile(QString)));
离线jemyzhang

只看该作者 3楼 发表于: 2013-03-22
回 2楼(jeffreylee) 的帖子
好吧,我现在就是这么做的,用QMutex+tryLock。。。但是总觉得不太QT
离线jeffreylee

只看该作者 2楼 发表于: 2013-03-22
使用互斥变量(或者用公共变量代替也可),动作前置位,动作结束清零,按钮事件首先判断互斥变量,置位状态立即返回,否则置位执行处理动作。
离线yuncode.net

只看该作者 1楼 发表于: 2013-03-22
能不能disable按钮?
云代码 http://yuncode.net
快速回复
限100 字节
 
上一个 下一个