Use UI elements from static function

这一生的挚爱 提交于 2019-12-12 01:49:23

问题


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:


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.


来源:https://stackoverflow.com/questions/43290753/use-ui-elements-from-static-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!