Results 1 to 7 of 7

Thread: I need idea for code desing

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default I need idea for code desing

    Hi all,

    I'm making Qt GUI application with about 5 windows and I need idea how to desing the code. Lets say I have two forms, the first is the main application window the second is some other window. Now here's the deal - I will initalise the main window from the main() function as an object not pointer, after that in the MainWindow class I will put pointer from the other(secondary) window. I will be able to change the UI in to the secondary window from the main window but I will not be able to change things on the main window via the secondary window because he will not have a pointer to the main window. So I ask how I can update both windows UI's from each other classes without to use global pointers.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I need idea for code desing

    Use signals and slots mechanism, but first design interfaces of all window classes.

  3. #3
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I need idea for code desing

    How I can use signals and slot when I don't have the pointer of the other window to connect it?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I need idea for code desing

    If the object exist, you can always get a pointer to it.

  5. #5
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I need idea for code desing

    Thanks, I will try my best and I will post if I have problems.

    Quote Originally Posted by jacek View Post
    If the object exist, you can always get a pointer to it.
    How exactly, if I'm not able to see the object in the code that I need to get it's pointer?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I need idea for code desing

    Quote Originally Posted by The Storm View Post
    How exactly, if I'm not able to see the object in the code that I need to get it's pointer?
    You can pass it there from the point in which you created the object. If you have a window that does something useful, then there must exists some interface between that window and the rest of your code. That's the place where you use signals and slots.

  7. #7
    Join Date
    Aug 2008
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I need idea for code desing

    From what I understand your code is something like this right:

    /* inside main() */

    QMainWindow window1;
    QMainWindow *window2 = new QMainWindow();

    Is that right?
    There are a few ways to deal with this:

    1. Pass the second window pointer to the main window, something like this:

    window1.setWindow2(window2);

    then on the window1 class (subclass from QMainWindow) just do with window2 what you want.

    2. Use signals ands slots. Like this:

    /* on main() */
    connect(&window1, SIGNAL(backgroundChanged(QColor)), window2, SLOT(changeBackground(QColor)));

    3. Make window2 a singleton. That way you only have an instance of it, which you can use as you like.

    You can also use tcp/ip to comunicate between both of than but it doesn't seem like a good solution for this case.

    I hope this helps

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.