Results 1 to 6 of 6

Thread: Refresh Ui...?

  1. #1
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Refresh Ui...?

    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:
    Qt Code:
    1. Second *s = new Second();
    2. s->exec();
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. MainWin *mwin = new MainWin();
    2. mwin->function("Add This!");
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. ui.MainWindow = mwin; //mwin is passed to the main window from second window
    To copy to clipboard, switch view to plain text mode 

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

    Thanks.

  2. #2
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Refresh Ui...?

    I couldn't figure what you exactly want to do (more code please), but you are doing it in a definitely wrong way.

    Qt Code:
    1. ui.MainWindow = mwin;
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. class MainWindow
    2. {
    3. ...
    4. void function(const QString& str);
    5. ...
    6. }
    7.  
    8. class Second
    9. {
    10. ...
    11. Second(MainWindow* mwin) : _mwin(mwin) {}
    12.  
    13. void anotherFunction() { _mwin->function("operations on our MainWindow through this pointer!"); }
    14.  
    15. private:
    16. MainWindow* _mwin;
    17. }
    18.  
    19. int main(int argc, char *argv[])
    20. {
    21. ...
    22. MainWindow mwin;
    23. Second second(&mwin);
    24. second.anotherFunction(); //changes refreshes mwin
    25. ...
    26. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Refresh Ui...?

    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.

  4. #4
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Refresh Ui...?

    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.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Refresh Ui...?

    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)

  6. #6
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Refresh Ui...?

    Thanks !

Similar Threads

  1. Refresh QSqlRelation
    By Banjo in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2018, 10:39
  2. Replies: 2
    Last Post: 14th January 2011, 15:09
  3. Trying to refresh a QGridLayout
    By zlacelle in forum Qt Programming
    Replies: 5
    Last Post: 5th April 2009, 21:21
  4. Refresh Issue
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 14th August 2008, 18:34
  5. mainwindow does not refresh
    By edb in forum Qt Programming
    Replies: 22
    Last Post: 25th January 2006, 16:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.