使用来自静态函数的 UI 个元素

Use UI elements from static function

我正在开发一个 qt 应用程序,我必须在其中访问 ui 元素。但我收到错误

invalid use of member 'foo::ui' in static member function

代码太多,这里没法添加。

ui

的声明
private:
Ui::foo *ui;

构造函数中的初始化

foo::foo(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::foo)
{
    ui->setupUi(this);
}

在出现错误的静态函数中访问。

ui->fp->setText("Some Text");

静态函数声明。

static I eventCallback(PVOID i_pv_context,
        T_xyz i_i_command,
        PVOID i_pv_param);

主要代码

  int main(int argc, char *argv[])
  {
    QApplication a(argc, argv);
    BarrierInterfaceModule w;
    w.show();
    return a.exec();
  }

我在网上看过,但没有找到解决办法。如果有办法请告诉我 around.let 我知道你是否需要更多信息 提前致谢

我知道两种可能的解决方案:

  1. foo 使用 singleton class。此方法仅在您只需要 foo 的一个实例化时才有效,这可能是这种情况,因为它是 QMainWindow。现在,您可以按如下方式设置文本:getInstance()->ui->fp->setText("Some Text");
  2. 回调函数通常能够将指针传递给用户提供的数据,但这取决于您使用的库。