PDA

View Full Version : Refresh Ui...?



steve.bush
20th March 2011, 08:37
Hi,

So I have a main window. I also have a second window. Lets call them:
main window is MainWin
second window is Second

inside main window, I am doing:


Second *s = new Second();
s->exec();


Now I can see the second window.

I am trying to execute a function inside second window which modifies the main window. So, I want to refresh the main window.

My function in second window:


MainWin *mwin = new MainWin();
mwin->function("Add This!");


Now, this is not reflected in the main window because I am adding it to a new instance of main window.

So, I tried doing this in the main window:



ui.MainWindow = mwin; //mwin is passed to the main window from second window


This gives me an error saying: invalid use of Ui::MainWindow::MainWindow
How do I get around this??

Thanks.

lipk
20th March 2011, 09:11
I couldn't figure what you exactly want to do (more code please), but you are doing it in a definitely wrong way.


ui.MainWindow = mwin;

MainWindow is a type, you can't assign a value to it. This is why the compiler mentions "invalid use".

I suggest something like this:



class MainWindow
{
...
void function(const QString& str);
...
}

class Second
{
...
Second(MainWindow* mwin) : _mwin(mwin) {}

void anotherFunction() { _mwin->function("operations on our MainWindow through this pointer!"); }

private:
MainWindow* _mwin;
}

int main(int argc, char *argv[])
{
...
MainWindow mwin;
Second second(&mwin);
second.anotherFunction(); //changes refreshes mwin
...
}

steve.bush
20th March 2011, 10:01
Ok, so I have 2 standalone apps. Main and Second.

When you click on a button in Main, Second app opens. This is done by creating a new object/instance of Second.

Inside Second, I want to execute a function which is in Main. Qt does not allow me to call a function of Main from Second. So, I am creating a new instance of Main inside Second (this is what I am trying to avoid). Because the function would run and add elements to the new instance of Main instead of the original Main.

lipk
20th March 2011, 10:39
Still unclear...

What do you mean by standalone app? What's the base class of Main and Second? How do you mean "Qt doesn't allow"? Did you try the solution described above? What was the problem with it?

Again: pass a pointer to Main to Second's constructor, and access Main's members through that.

squidge
20th March 2011, 10:45
I'd say your design is incorrect. You should never need to call functions in main from another class. If you need to update some data, then a much better way would be to use signals and slots, or possibly call back functions. Then second will just say "I have some updated data for you" and main renders it.

If you have functions in main that both main and second use, then these really need to be in some other class which both main and second have an instance pointer to (which can be shared between main and second if the data in that class needs to be sharable)

steve.bush
21st March 2011, 05:21
Thanks !