Use UI elements from static function

2019-09-14 12:07发布

I am building a qt application in which i am have to access ui elements. but i am getting error as

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

The code is big so cant add here.

Declaration of ui

private:
Ui::foo *ui;

Initialization in Constructor

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

Accessing in static function where it is giving error.

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

Static function declaration.

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

main code

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

I have looked on Internet but did not get solution. please let me know if there is a way around.let me know if you need any more info Thanks in advance

1条回答
啃猪蹄的小仙女
2楼-- · 2019-09-14 12:48

I'm aware of two possible solutions:

  1. Use a singleton class for foo. This method only works if you need only one instantiation of foo, which is probably the case because it is a QMainWindow. Now, you can set the text as follows: getInstance()->ui->fp->setText("Some Text");
  2. Often callback function are able to pass a pointer to user supplied data, but this depends on the library you are using.
查看更多
登录 后发表回答