Results 1 to 6 of 6

Thread: QMainWindow setCentralWidget from ui widget, Qt4

  1. #1
    Join Date
    Feb 2006
    Location
    de
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QMainWindow setCentralWidget from ui widget, Qt4

    Sorry for a question with an obvious (?) answer (seems not to be asked so far).

    I would like to create a program based on QMainWindow and have created a quite complex QWidget in Qt-Designer (Qt4) which I would now like to set as the CentralWidget in the QMainWindow.

    I'm using the ui.setup approach, but this gives me always the error, that a 'Ui::Xy&' is not a 'QWidget*': & != * .

    Can somebody point to a simple example where a *.ui file is used to create the CentralWidget of a QMainWIndow, please?

    The Qt4 example about QMainWindow does not do it for me...

    Or is the correct approach to create this main widget as a QMainWindow instead of a QWidget?
    (But what would then be the CentralWidget?)

    (I use cmake rather than qmake, but this should not make any difference, I assume.)
    The widget to be used as the CentralWidget compiles OK, so far I managed to see it outside of the QMainWindow.

    Help is much appreciated.

    Helge

    P.S.: Using Qt 4.4.0-rc1-5 on debian sid amd64, if that matters.
    Last edited by alan; 3rd May 2008 at 00:46.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow setCentralWidget from ui widget, Qt4

    It shudnt be as complex as it sounds,,,,

    what all u need is myWindow->setCentralWidget(myWidget);
    thats it.
    I'm using the ui.setup approach, but this gives me always the error, that a 'Ui::Xy&' is not a 'QWidget*': & != * .
    how are u setting the widget, can we see ??

  3. The following user says thank you to aamer4yu for this useful post:

    alan (3rd May 2008)

  4. #3
    Join Date
    Feb 2006
    Location
    de
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: (solved) - QMainWindow setCentralWidget from ui widget, Qt4

    Thanks, aamer4yu, but this is what is not working.

    Let's assume I have the ui file: myproj.ui which generates ui_myproj.h (and is based on the QWidget class, called Cw).

    In myproj.h I have:
    Qt Code:
    1. #include ui_myproj.h
    2. private:
    3. Ui::Cw uicw;
    4. QWidget wCw;
    To copy to clipboard, switch view to plain text mode 
    In myproj.cpp, in the constructor, I do (myproj is a QMainWindow):
    Qt Code:
    1. uicw.setupUi( &wCw);
    2. setCentralWidget(uicw );
    To copy to clipboard, switch view to plain text mode 
    Then I get:
    error: no matching function for call to 'Myproj::setCentralWidget(Ui::Cw&)'
    note: candidates are: void QMainWindow::setCentralWidget(QWidget*)
    It wants a pointer, not a reference ? I assume I need a pointer to uicw or wCw (the widget), but I don't know how to get it.

    ***** Update:
    Good that we talked about it... the following code works, not sure whether it is 'good', but it works:

    In myproj.h I have:
    Qt Code:
    1. #include ui_myproj.h
    2. private:
    3. Ui::Cw uicw;
    4. QWidget wCw;
    To copy to clipboard, switch view to plain text mode 
    In myproj.cpp, in the constructor, I do (myproj is a QMainWindow):
    Qt Code:
    1. uicw.setupUi( &wCw);
    2. QWidget * wCw2 = &wCw; // creating the pointer
    3. setCentralWidget( wCw2 );
    To copy to clipboard, switch view to plain text mode 
    This finally sets the ui QWidget as central widget.
    Last edited by jpn; 13th May 2008 at 12:58. Reason: reformatted to look better

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow setCentralWidget from ui widget, Qt4

    You could very well use -
    Qt Code:
    1. setCentralWidget(&uicw );
    To copy to clipboard, switch view to plain text mode 

    couldnt you ?? &uicw is the address of the object and thats what setCentralWidget(QWidget*) needs
    Last edited by jpn; 13th May 2008 at 12:59. Reason: missing [code] tags

  6. The following user says thank you to aamer4yu for this useful post:

    alan (3rd May 2008)

  7. #5
    Join Date
    Feb 2006
    Location
    de
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMainWindow setCentralWidget from ui widget, Qt4

    aamer4yu,

    I just tried this and it produced the same error as before:

    error: no matching function for call to 'Myproj::setCentralWidget(Ui::Cw*)'
    note: candidates are: void QMainWindow::setCentralWidget(QWidget*)

    But thanks for trying. I thought too, that creating this extra pointer was a bit strange (but would have stick with it).
    Well, I'm not a good coder, so may be I made a mistake elsewhere...

    But wait, (huch!),
    >setCentralWidget( &wCw );
    does the trick.
    However, it does need the line:
    >uicw.setupUi( &wCw);
    before to work
    Yeah, much nicer!

    Thanks again !

    For reference, in case somebody has the same problem: 2 lines in the constructor of myproj.cpp are enough:
    >uicw.setupUi( &wCw);
    >setCentralWidget( &wCw );

  8. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QMainWindow setCentralWidget from ui widget, Qt4

    Do NOT allocate child widgets on the stack. QMainWindow takes ownership of the central widget and deletes it later when the main window is deleted. You will get a crash because of the double free attempt.
    J-P Nurmi

Similar Threads

  1. Replies: 4
    Last Post: 25th August 2014, 18:05
  2. make a widget paint itself even thought it is not shown
    By momesana in forum Qt Programming
    Replies: 7
    Last Post: 28th April 2008, 11:57
  3. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  4. paint central widget of a QMainWindow???
    By Shuchi Agrawal in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 08:02
  5. Central Widget of QMainWindow
    By sumsin in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2006, 18:32

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.