You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在GNU Radio中自定义QT GUI Sink的实现方法咨询

自定义GNU Radio QT GUI块配置方法

问题背景

我正在基于GNU Radio开展SDR项目,需要实现一种特殊绘图功能,现有内置QT GUI Sink无法满足需求。多方查找后,未找到说明如何用Python自定义QT GUI Widget并让GNU Radio像对待内置块一样显示它的API文档或教程。目前我已实现一个可作为独立QT应用运行的类(代码如下),推测需在块的yaml文件中进行相关设置,让GNU Radio识别这是GUI块并获取Widget,想了解相关设置方法及对应文档。

import pmt
import time
import pyqtgraph as pg
from gnuradio import gr
from collections import namedtuple
from PyQt5 import QtWidgets, QtCore


class event_plot(gr.sync_block):

    def __init__(self, gui_hint: str):
        gr.sync_block.__init__(self,
                               name="event_plot",
                               in_sig=None,
                               out_sig=None)
        self._create_gui_widget()

    def _create_gui_widget(self):
        """ Generate the Widget for the GUI and update timer.
        """
        # GUI setup
        self.widget = pg.PlotWidget(title="Tracer Detections")
        self.widget.setLabel('bottom', 'Time', 's')
        self.plot_item = self.widget.getPlotItem()
        self.stem_items = []

        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self._update_plot)
        self.timer.start(1000)  # Update every 200ms

    ...

    def _update_plot(self):
       ...

    def get_widget(self):
        return self.widget

    def work(self, input_items, output_items):
        return len(output_items[0])


if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    plot = event_plot("").get_widget()
    plot.show()
    app.exec_()

配置方法与关键要点

1. YAML文件核心配置

要让GNU Radio识别为GUI块,需在块的.block.yml文件中添加以下关键配置:

  • category:设置为"QT GUI",GNU Radio Companion(GRC)会将块归类到GUI面板中
  • flags:添加"gui"标记,明确告知GRC这是一个GUI块
  • attributes:若需传递GUI布局提示,添加gui_hint参数(对应代码中的初始化参数)
  • provides_widget:设为true,声明该块提供QT Widget实例
  • widget_method:指定获取Widget的方法名,这里对应你代码中的get_widget

示例YAML配置片段:

id: event_plot
label: Event Plot
category: "QT GUI"
flags: [gui]
attributes:
- id: gui_hint
  label: GUI Hint
  dtype: string
  default: ""
provides_widget: true
widget_method: get_widget

2. 代码适配注意事项

  • 确保get_widget()方法存在且名称与YAML中widget_method一致,这是GRC获取Widget的核心入口
  • __init__方法需接收gui_hint参数,与YAML中的属性配置对应
  • 独立测试代码(if __name__ == "__main__"部分)仅用于单独运行验证,GRC会统一管理QT事件循环,无需在块内启动QApplication

3. 内置机制说明

GNU Radio识别GUI块的逻辑:

  • 通过flags: [gui]标记识别为GUI类型块
  • 读取provides_widget: true后,自动调用widget_method指定的方法获取Widget实例
  • 将Widget自动嵌入GRC主窗口布局,无需手动处理窗口显示逻辑

内容的提问来源于stack exchange,提问作者PeterO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 10:03:29