×

Discussion Board

Results 1 to 15 of 15
  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    272

    Question How to use two .ui files within one class?

    I would like to have do two separate forms in Qt Creator. One for landscape mode and the other one for portrait mode. But since the code would be exactly the same, all I want to change is the define which form file to use. Is this possible? If yes, then how? Or is there a better method?

    Thanks

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: How to use two .ui files within one class?

    I think it's not doable. But you can do perform required actions in QWidget::resizeEvent() handler.
    http://doc.trolltech.com/4.6/qwidget.html#resizeEvent

  3. #3
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to use two .ui files within one class?

    I think in general it's possible, but Designer may fight you. With Creator at least you have a slight advantage over Carbide, since it adds an extra level of indirection between "ui" and the things it "owns". So swapping "ui" pointers should accomplish what you want.

    The trick is to convince the C compiler that the two "ui" objects are derived from the same class -- you'd have to create a common superclass that exactly matched the layout of the two "ui" classes, then coerce the two "real" pointers to that class. Keeping things synced is the biggest challenge.

  4. #4
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: How to use two .ui files within one class?

    Quote Originally Posted by danhicksbyron View Post
    I think in general it's possible, but Designer may fight you. With Creator at least you have a slight advantage over Carbide, since it adds an extra level of indirection between "ui" and the things it "owns". So swapping "ui" pointers should accomplish what you want.

    The trick is to convince the C compiler that the two "ui" objects are derived from the same class -- you'd have to create a common superclass that exactly matched the layout of the two "ui" classes, then coerce the two "real" pointers to that class. Keeping things synced is the biggest challenge.
    Since it's now your word against my word, you are supposed to provide proof of the concept. Otherwise you are giving a false hope.

  5. #5
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to use two .ui files within one class?

    As I said, it's going to fight you, and keeping things synced would be a challenge.

    A better approach is probably to create a common proxy class definition, and initialize two instances of that from the "ui" definitions. Eg:

    class proxy {
    public:
    QPushButton* button1;
    QPushButton* button2;
    }
    ...
    proxy1 = new proxy();
    proxy1.button1 = ui1.button1;
    proxy1.button2 = ui1.button2;
    proxy2 = new proxy();
    proxy2.button1 = ui2.button1;
    proxy2.button2 = ui2.button2;

    currentProxy = proxy1;
    ...
    currentProxy.button1 ->setText("This is Button 1");

    Also add methods to proxy to handle close, raise, etc, as needed.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    272

    Re: How to use two .ui files within one class?

    Hmm... There doesn't seem to be any easy way to do it. Currently simply resizing and moving widgets seem to be the less error prone solution. The proxy class is not bad either, but to do it in existing code is rather cumbersome.

    The best would be if in the "ui" file itself there could be two settings, one for landscape and the other one for portrait mode. Perhaps we could have this in some future version of Qt Creator...

    Thanks for the suggestions

  7. #7
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: How to use two .ui files within one class?

    Quote Originally Posted by Sasler View Post
    Currently simply resizing and moving widgets seem to be the less error prone solution.
    If your UI is good, then you don't need to do any resizing and moving. Layouts will do it for you.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    272

    Re: How to use two .ui files within one class?

    Quote Originally Posted by divanov View Post
    If your UI is good, then you don't need to do any resizing and moving. Layouts will do it for you.
    Layouts? I don't understand what you mean. Can you point me to some more info?

  9. #9
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to use two .ui files within one class?

    He means using a layout manager: http://doc.qt.nokia.com/4.6-snapshot/layout.html

  10. #10
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: How to use two .ui files within one class?

    Quote Originally Posted by danhicksbyron View Post
    As I said, it's going to fight you, and keeping things synced would be a challenge.

    A better approach is probably to create a common proxy class definition, and initialize two instances of that from the "ui" definitions. Eg:

    class proxy {
    public:
    QPushButton* button1;
    QPushButton* button2;
    }
    ...
    proxy1 = new proxy();
    proxy1.button1 = ui1.button1;
    proxy1.button2 = ui1.button2;
    proxy2 = new proxy();
    proxy2.button1 = ui2.button1;
    proxy2.button2 = ui2.button2;

    currentProxy = proxy1;
    ...
    currentProxy.button1 ->setText("This is Button 1");

    Also add methods to proxy to handle close, raise, etc, as needed.
    This is pretty much insane. You need to keep state of both UIs in sync.
    How do you show your UI when geometry changes? At least your need to remove ui1 widgets and replace them with ui2 widgets.

  11. #11
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to use two .ui files within one class?

    This is pretty much insane. You need to keep state of both UIs in sync.
    Yeah, if you want to be able to freely switch back and forth, you need to keep both up to date. But that's probably not a requirement, and there are several ways to crack that one if need be.

    No need to "remove" widgets for one view and replace them with others -- just switch views.

  12. #12
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: How to use two .ui files within one class?

    Quote Originally Posted by danhicksbyron View Post
    No need to "remove" widgets for one view and replace them with others -- just switch views.
    How this "just switch" will look in a code?

  13. #13
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to use two .ui files within one class?

    Code:
    void TestWindowing::switchToWindow1() {
    	window2 ->hide();
    	window1 ->show();
    }

  14. #14
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: How to use two .ui files within one class?

    Quote Originally Posted by danhicksbyron View Post
    Code:
    void TestWindowing::switchToWindow1() {
    	window2 ->hide();
    	window1 ->show();
    }
    If those are actually two windows, why do you need that proxy? Now it's simply application with two windows shown depending on device orientation.

  15. #15
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to use two .ui files within one class?

    Two windows presenting the same information. The proxy prevents most code from having to know which one is active.

Similar Threads

  1. Replies: 1
    Last Post: 2010-02-09, 19:24
  2. Storing class files in MIDP RMS??
    By java-dev in forum Mobile Java General
    Replies: 2
    Last Post: 2009-02-16, 20:41
  3. C++ Symbols class(), class()->,class. ????
    By PACALA_BA in forum Symbian
    Replies: 7
    Last Post: 2008-09-15, 20:57
  4. Replies: 2
    Last Post: 2007-03-14, 17:15
  5. Exceptions while loading application
    By avinash_rs in forum Mobile Java General
    Replies: 0
    Last Post: 2003-06-09, 10:53

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×