问题
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:
- Use a singleton class for
foo. This method only works if you need only one instantiation offoo, which is probably the case because it is aQMainWindow. Now, you can set the text as follows:getInstance()->ui->fp->setText("Some Text"); - 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