0

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

RPK
  • 53
  • 1
  • 13
  • Well, the error does say it. `ui` is not static so you can't use it without an object in a static method. Why do you need that method to be static and can you make `ui` static also? – Sami Kuhmonen Apr 08 '17 at 06:05
  • Your error code is straight forward. every *non-static member-function* must be called via an object. – WhiZTiM Apr 08 '17 at 06:05
  • @SamiKuhmonen its a third party function so have it has to be static. – RPK Apr 08 '17 at 06:08
  • That's the deal with static functions: they can't access non-static members. Does that function need to be static? If so, consider passing `ui` to it. – Sergey Slepov Apr 08 '17 at 06:08
  • @WhiZTiM i will add in question – RPK Apr 08 '17 at 06:09
  • @SergeySlepov The function has to be static and i know we cant access UI but i wanted to know if there is some work around – RPK Apr 08 '17 at 06:25
  • Can you pass `ui` into `eventCallback` through `i_pv_context` or `i_pv_param`? They look like they're void pointers so can point to anything. – Sergey Slepov Apr 08 '17 at 07:30
  • @SergeySlepov actually no because it is called by third party function which has specific format. its a callback function as you cam see. – RPK Apr 08 '17 at 07:39

1 Answers1

0

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.
Community
  • 1
  • 1
m7913d
  • 10,244
  • 7
  • 28
  • 56
  • thanks for the answer i had also come to same conclusion(to use singleton). but my object is created in main as `int main(int argc, char *argv[]) { QApplication a(argc, argv); BarrierInterfaceModule w; w.show(); return a.exec(); }` – RPK Apr 08 '17 at 10:43
  • sorry for bad format of above comment. i don't know how to use singleton in that case. – RPK Apr 08 '17 at 10:50
  • Why do you not rewrite it as: `BarrierInterfaceModule::getInstance()->show ();` – m7913d Apr 08 '17 at 11:38