Results 1 to 20 of 22

Thread: Problems with setCentralWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    Quote Originally Posted by wysota View Post
    What is the point of hiding Qt architecture if you are immediately passing the pressumably non-Qt component to a Qt-expecting method?

    Qt Code:
    1. Comp* c = new Comp;
    To copy to clipboard, switch view to plain text mode 
    The above works fine. Using CompView won't work because the object is aligned differently, so the compiler generates wrong offset for calling QWidget's methods.
    first, this is just an example, using the whole factory does seperate QT classes
    from the rest of the application.

    does someone know of a way around it ?
    can't I separate logic and gui when using Qt ?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    I didn't read all the posts, just the last question.

    can't I separate logic and gui when using Qt ?
    Of course you can... Qt does it with the MVC architecture, for example. There are other ways too.

    I'm not sure what CompView is supposed to do, but it doesn't look good. Having an object inheriting from a GUI class and a class that implements app logic is not called "separating logic".
    Actually, there's a word for that: "mess". In the end you'll end up with an object that exposes both things anyway.

  3. #3
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    Quote Originally Posted by marcel View Post
    I didn't read all the posts, just the last question.

    Of course you can... Qt does it with the MVC architecture, for example. There are other ways too.

    I'm not sure what CompView is supposed to do, but it doesn't look good. Having an object inheriting from a GUI class and a class that implements app logic is not called "separating logic".
    Actually, there's a word for that: "mess". In the end you'll end up with an object that exposes both things anyway.
    can you give me an example ? for the short program I have ?
    how can I have a widget but hide all Qt related ?

    I don't know what you call a mess, but in java this is what we do and
    it separates perfectly so we can work with multiple views (swing, swt, web ..)

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    I can't(not yet) because I still don't see the point... Are you writing a library? Or what?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with setCentralWidget

    Quote Originally Posted by Rockem View Post
    can you give me an example ? for the short program I have ?
    how can I have a widget but hide all Qt related ?
    Currently you are not hiding anything. You still need a QApplication object, you'll need to run QApplication::exec() eventually, etc. This is certainly not the way to go, regardless of the language used.

    If you want to hide Qt, hide all of it and not a part of it - have a function(s) that will do everything starting from making sure there is a QApplication object available, through instantiating the object, showing it, manipulating it up to destroying it when it's not needed anymore. Currently you're just handling instantiation and what about the rest?

    There are for instance Photoshop plugins written in Qt and they handle the situation very well, so obscuring the technology behind the interface is possible, you are just doing it the wrong way.

    Here is a trivial example (not tested - and you still need QApplication::exec() somewhere):

    Qt Code:
    1. class Iface {
    2. public:
    3. virtual void initialize() = 0;
    4. virtual void show() = 0;
    5. virtual void destroy() = 0;
    6. };
    7.  
    8. class Implementation : public Iface {
    9. public:
    10. void initialize() {
    11. if(!QApplication::instance()){
    12. widget = new QMainWindow;
    13. }
    14. }
    15. void show() { widget->show(); }
    16. void destroy() { delete widget; }
    17. private:
    18. QMainWindow *widget;
    19. };
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Posts
    371
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    Plain old casting and doubble inheritance do not work as expected. Look at this line:

    Qt Code:
    1. mw.setCentralWidget((QWidget*)c);
    To copy to clipboard, switch view to plain text mode 

    Try using qobject_cast or dynamic_cast:
    Qt Code:
    1. mw.setCentralWidget( qobject_cast<QWidget*>(c) );
    2. mw.setCentralWidget( dynamic_cast<QWidget*>(c) );
    To copy to clipboard, switch view to plain text mode 

    or just no cast at all:
    Qt Code:
    1. mw.setCentralWidget( c );
    To copy to clipboard, switch view to plain text mode 

    And do try to put the QObject first in the double inheritance.

  7. The following user says thank you to elcuco for this useful post:

    Rockem (8th March 2008)

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with setCentralWidget

    qobject_cast won't work because CompView is not a QObject. dynamic_cast seems to work.

  9. #8
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    yea, dynamic_cast works great !!!

    thanx

Similar Threads

  1. Replies: 2
    Last Post: 8th March 2007, 22:22
  2. Utf8 problems
    By cristiano in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2006, 00:14
  3. Problems building mysql plugin for Qt 4.1.2 on windows XP
    By Philip_Anselmo in forum Installation and Deployment
    Replies: 3
    Last Post: 17th May 2006, 15:38
  4. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39
  5. [Win32/VC++ 8.0] Strange problems with qrc_*.cpp files
    By mloskot in forum Installation and Deployment
    Replies: 6
    Last Post: 6th March 2006, 10:28

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
  •  
Qt is a trademark of The Qt Company.