Results 1 to 11 of 11

Thread: Add widget to mainwindow

  1. #1
    Join Date
    Nov 2012
    Location
    Kentucky, USA
    Posts
    46
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Add widget to mainwindow

    Sorry, this is a newbie question, but I have never done it before and can't find any hints in the doc or forum.

    I have a mainwindow that was created with designer. Now I have a custom widget that I would like to add in the mainwindow.cpp, but I can't find anything that tells me how to do this. Is this possible, and how?

    Thanks for any advice.

  2. #2
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add widget to mainwindow

    Quote Originally Posted by K4ELO View Post
    Sorry, this is a newbie question, but I have never done it before and can't find any hints in the doc or forum.

    I have a mainwindow that was created with designer. Now I have a custom widget that I would like to add in the mainwindow.cpp, but I can't find anything that tells me how to do this. Is this possible, and how?

    Thanks for any advice.
    It depends on what the widget is for, and where you want it in the window. QMainWindow has several built-in functions for adding widgets to the main window, such as setCentralWidget() (which sets the main widget of the main window), and addToolBar(), and addDockWidget(), etc. Just check them out in the documentation to get an idea how to use them.

    Cheers.

  3. #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: Add widget to mainwindow

    I have a mainwindow that was created with designer. Now I have a custom widget that I would like to add in the mainwindow.cpp, but I can't find anything that tells me how to do this. Is this possible, and how?
    If you wanted to add the widget from the designer, there are two ways..

    1. Write a Qt Designer plugin (which may be not simple for newbie)
    or
    2. Add a QWidget in the designer and promote (right-click > promote to) it to the custom widget. Note that you cannot see the costom widget in the disigner, but will be visible only at runtime. If you wanted to see it in the designer then go with first method.
    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.

  4. #4
    Join Date
    Nov 2012
    Location
    Kentucky, USA
    Posts
    46
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add widget to mainwindow

    Thanks folks, but what I am asking is if there is a QWidget or QMainWindow function that can be used in the mainwindow.cpp code to add a custom widget to the designer created form. I can't find that there is. But maybe I am missing something.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add widget to mainwindow

    A QMainWindow-derived main window can contain anything as its central widget (including nothing). As Coolname007 said, there are methods to add things to the "decorations" surrounding a main window, but how could there be a generic way to add something to a central widget whose contents could be anything at all?

    Your form probably contains one or more layouts that you added using the designer, right? What you need to do is to retrieve the pointer to the correct layout instance, then use the methods of the layout class to insert your custom widget in the right place in the layout.

    How you do that is entirely a function of the layouts you use, which specific QLayout subclass it is, and how a particular location in a layout is addressed in code. For example, if it is a QGridLayout instance, you call the addWidget() method with the correct row and column indexes. For QHBoxLayout or QVBoxLayout you can call addWidget() to put it on the end, or insertWidget() to put it in the middle somewhere.

    This is pretty much the same thing that happens when you call setupUi() in your main window constructor, where Qt reads the compiled ui description and instantiates the widgets to build it.

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

    K4ELO (18th February 2013)

  7. #6
    Join Date
    Nov 2012
    Location
    Kentucky, USA
    Posts
    46
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add widget to mainwindow

    Thanks d_stranz. Almost there, but not quite.
    This code:
    Qt Code:
    1. Spectrograph* spectrograph = new Spectrograph;
    2. MainWindow::setCentralWidget(spectrograph);
    To copy to clipboard, switch view to plain text mode 
    Puts the custom widget in the upper left corner of the mainwindow, however I can't get it to change position with move or position functions.

    This code:
    Qt Code:
    1. Spectrograph* spectrograph = new Spectrograph;
    2. QGridLayout* layout = new QGridLayout;
    3. layout->addWidget(spectrograph);
    4. layout->setAlignment(spectrograph, Qt::AlignCenter);
    5. spectrograph->setLayout(layout);
    6. MainWindow::setCentralWidget(spectrograph);
    To copy to clipboard, switch view to plain text mode 
    The application starts (no error messages) but the mainwindow never opens.

    I did not create any explicit layouts in the designer.

    EDIT: ok, I figured it out. Since there were no other layouts defined, the central widget was consuming all of the mainwindow space under the menu bar. So I defined dock widgets in all 4 locations and I will put the other required items in those docks.
    Last edited by K4ELO; 18th February 2013 at 23:22.

  8. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add widget to mainwindow

    Qt Code:
    1. MainWindow::setCentralWidget(spectrograph);
    To copy to clipboard, switch view to plain text mode 

    This line is nonsense, anyway, unless it is in the constructor of your MainWindow, in which case the MainWindow:: scoping qualifier is superfluous. Just do this:

    Qt Code:
    1. setCentralWidget(spectrograph);
    To copy to clipboard, switch view to plain text mode 

    I am not sure what you are doing in your code, because if I create a QMainWindow-derived class as my main widget and set *anything* as the central widget (with or without a layout, toolbars, dock widgets, or any other code), the main window will automatically resize it to fill the entire space.

    So if what you want is a main window where you fill it at run time, delete the ui file, remove the Ui::MainWindow member variable (or inheritance, however you do it), remove the setupUi(), and simply make a new instance of your widget in the MainWindow constructor and set it as the central widget. No layout or ui form needed:

    Qt Code:
    1. MainWindow::MainWindow( QWidget * parent )
    2. : QMainWindow( parent )
    3. {
    4. Spectrograph * spectrograph = new Spectrograph;
    5.  
    6. // do whatever you need to do to set up the spectrograph instance and its connections
    7. setCentralWidget( spectrograph );
    8. }
    To copy to clipboard, switch view to plain text mode 

    and that's it. If you want to add menus, toolbars, and whatever, you can use the designer for that (or implement them directly in the constructor code), but don't specify anything as the central widget in the designer. If you do use the designer for that, then you'll need to keep all the Ui:: related stuff.

    I tend to create my main windows in code rather than use the designer. I don't like looking in two or three places to figure out what my menu items, actions, and connections do. For dialogs and forms, I use the designer simply because it is easier to get the layout right.

  9. The following user says thank you to d_stranz for this useful post:

    K4ELO (19th February 2013)

  10. #8
    Join Date
    Nov 2012
    Location
    Kentucky, USA
    Posts
    46
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add widget to mainwindow

    Thanks again d_stranz for your helpful advice. I understand about the central widget now. I'm going to use the designer with a QGraphicsView class to display the spectrum. At least I'm going to try that design

  11. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add widget to mainwindow

    Um, good luck with that. I thought your goal was to learn about audio handling in Qt. If that is true, you would be better off using an out-of-the-box widget like QwtPlot to show your spectrogram instead of spending a lot of time implementing your own. There plenty of Qwt examples to get you started quickly.

  12. #10
    Join Date
    Nov 2012
    Location
    Kentucky, USA
    Posts
    46
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add widget to mainwindow

    Thank you, an excellent suggestion. I have built and installed qwt. Now looking at their spectrogram example.

  13. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add widget to mainwindow

    The Qwt "spectrogram" is a 2.5D image of (x,y,z) data - has nothing to do with the "spectrogram" you are talking about.. If what you want to plot is an audio response, that's just a simple QwtPlotCurve with x- and y- data arrays.

Similar Threads

  1. Replies: 2
    Last Post: 7th March 2012, 07:47
  2. Replies: 0
    Last Post: 6th November 2011, 09:22
  3. QMenubar in a non mainwindow widget
    By Tito in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 16th August 2010, 15:24
  4. Replies: 10
    Last Post: 29th May 2010, 18:42
  5. QwtPlot widget in MainWindow
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2007, 11:48

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.