PDA

View Full Version : Add widget to mainwindow



K4ELO
18th February 2013, 00:41
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.

Coolname007
18th February 2013, 04:34
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.

Santosh Reddy
18th February 2013, 07:20
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.

K4ELO
18th February 2013, 15:33
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.

d_stranz
18th February 2013, 18:01
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.

K4ELO
18th February 2013, 18:31
Thanks d_stranz. Almost there, but not quite.
This code:


Spectrograph* spectrograph = new Spectrograph;
MainWindow::setCentralWidget(spectrograph);

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:


Spectrograph* spectrograph = new Spectrograph;
QGridLayout* layout = new QGridLayout;
layout->addWidget(spectrograph);
layout->setAlignment(spectrograph, Qt::AlignCenter);
spectrograph->setLayout(layout);
MainWindow::setCentralWidget(spectrograph);

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.

d_stranz
19th February 2013, 04:04
MainWindow::setCentralWidget(spectrograph);

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:


setCentralWidget(spectrograph);

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:



MainWindow::MainWindow( QWidget * parent )
: QMainWindow( parent )
{
Spectrograph * spectrograph = new Spectrograph;

// do whatever you need to do to set up the spectrograph instance and its connections
setCentralWidget( spectrograph );
}


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.

K4ELO
19th February 2013, 17:05
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 :)

d_stranz
19th February 2013, 17:27
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.

K4ELO
19th February 2013, 19:15
Thank you, an excellent suggestion. I have built and installed qwt. Now looking at their spectrogram example.

d_stranz
19th February 2013, 23:12
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.