Results 1 to 16 of 16

Thread: Set widget as central widget in a QMainWindow

  1. #1
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    9
    Platforms
    Windows

    Default Set widget as central widget in a QMainWindow

    Hi

    Suppose i have w (a QWidget) and W (a QMainWindow).
    This means i have a w.h+w.cpp+w.ui and W.h, W.cpp and W.ui, as well as the rest of the project files.

    How can i use the w widget as the W central widget, as well as keep changing from w to other similar widgets (... x, y, z , ...)?
    Does this apply to QDialogs instead of QWidgets as well?

    Summarizing, i want to write several QDialogs or QWidgets and show them inside a QMainWindow, one at a time, in it's central widget.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Set widget as central widget in a QMainWindow

    The easiest approach would be to put all widgets (w,x,y,z...) in a QStackedWidget. Then set that stacked widget as central widget. This way you can easily change between the widgets.

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

    gtnoob (8th January 2013)

  4. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Set widget as central widget in a QMainWindow

    Do you want it do it from QtCreator/QtDesigner? If Yes, then you need to use the "promote widget" feature.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    gtnoob (8th January 2013)

  6. #4
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    9
    Platforms
    Windows

    Default Re: Set widget as central widget in a QMainWindow

    I'm doing this from QtCreator/QtDesigner.

    I used the Lykurg approach and did something like this (see detailed description): http://qt-project.org/doc/qt-4.8/qstackedwidget.html

    Then i created a private slot associated to an action on the QMainWindow menu.
    But it is not changing the central widget.
    I used:
    Qt Code:
    1. myComboBox->setCurrentIndex(1);
    To copy to clipboard, switch view to plain text mode 

    Showldn't this be enought?

  7. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Set widget as central widget in a QMainWindow

    That should work. Did the slot execute?

    BTW, reply to first post: Only QWdigets can be set on QMainWindow (not QDialogs)
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  8. The following user says thank you to Santosh Reddy for this useful post:

    gtnoob (8th January 2013)

  9. #6
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    9
    Platforms
    Windows

    Default Re: Set widget as central widget in a QMainWindow

    Yess ... used a cout to let me know the index.

    ops ...
    BTW, reply to first post: Only QWdigets can be set on QMainWindow (not QDialogs)
    This might be the problem ... i will let you know ...

  10. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Set widget as central widget in a QMainWindow

    Quote Originally Posted by gtnoob View Post
    Then i created a private slot associated to an action on the QMainWindow menu.
    But it is not changing the central widget.
    I used:
    Qt Code:
    1. myComboBox->setCurrentIndex(1);
    To copy to clipboard, switch view to plain text mode 
    Is myComboBox your stacked widget?

  11. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Set widget as central widget in a QMainWindow

    I assume a connection is made between QComboBox and QStackedWidget as mentioned in the documentaion referred.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  12. #9
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    9
    Platforms
    Windows

    Default Re: Set widget as central widget in a QMainWindow

    Well ... i must be missing something really obvious!
    I attach a sample code.
    Could someone please take a look?
    Attached Files Attached Files

  13. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Set widget as central widget in a QMainWindow

    You changing the QComboBox item programatically, this is emit currentIndexChanged(int) , not activated(int). Do the changes below
    Qt Code:
    1. //connect(opComboBox, SIGNAL(activated(int)), myStack, SLOT(setCurrentIndex(int))); //<<<<<<<<<<<<<<<< Change to next line
    2. connect(opComboBox, SIGNAL(currentIndexChanged(int)), myStack, SLOT(setCurrentIndex(int)));
    To copy to clipboard, switch view to plain text mode 

    Note that your QComboBox is hidded, if you show it and change the item directly in combo box then the activated(int) signal will be emited (and also currentIndexChanged(int) signal)
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  14. The following user says thank you to Santosh Reddy for this useful post:

    gtnoob (8th January 2013)

  15. #11
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    9
    Platforms
    Windows

    Default Re: Set widget as central widget in a QMainWindow

    I see ... thanks.

    One final "open" question
    Could this be a valid approach for a larger scale application?

  16. #12
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Set widget as central widget in a QMainWindow

    Yes and No.

    Yes for using QStakedWidget, and signalling it from QComboBox is just fine.

    No for
    1. Hiding QComboBox (that too floating, with out parent)
    2. Most the objects in the example code does not have default parent objects, this will lead to memory leaks once the application grows. (I understand this was just an example, but don't do in real application)
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  17. #13
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    9
    Platforms
    Windows

    Default Re: Set widget as central widget in a QMainWindow

    Most the objects in the example code does not have default parent objects
    What exactly does this mean?
    Are you refering to w1 and w2?

  18. #14
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Set widget as central widget in a QMainWindow

    Yes, they should look like
    Qt Code:
    1. widget1 = new w1(this);
    2. widget2 = new w2(this);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  19. The following user says thank you to Santosh Reddy for this useful post:

    gtnoob (8th January 2013)

  20. #15
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    9
    Platforms
    Windows

    Default Re: Set widget as central widget in a QMainWindow

    Does this ( http://qt-project.org/doc/qt-5.0/qtc...jecttrees.html ) explain the "because..." of your point of view?

  21. #16
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Set widget as central widget in a QMainWindow

    Yes, I mean the same
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 2
    Last Post: 29th June 2011, 15:45
  2. QMainWindow central widget minimum size
    By dima in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2010, 07:24
  3. Replies: 2
    Last Post: 7th June 2008, 13:12
  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.