I thought that layout is just a widget that keeps more widgets inside.
But now I find that I can't add it to other widgets using addWidget.
For instance how to add a layout to QSplitter?
QWidget has built in support for layouts through the layout() and setLayout(...) functions. The layout object controls the positioning of different child widgets that may be added to the layout. In addition, it makes sure to correctly size its parent widget based on the constraints specified. If the layout does not yet have a parent widget, then as soon as the layout is attached to a widget with setLayout(), it will set the size of that parent widget.
But, some widgets are more like a layout manager than a widget, such as QSplitter and QTabWidget. Consider, for example, QSplitter. Although a single widget, it presents two different areas that may be worked with. In this case, a single layout manager for two different areas doesn't really make sense. Like QSplitter, QTabWidget has some behaviors which make a single layout not only unnecessary but not useful.
I think it's the above melding of layout and widget that makes the separation of layout and widget sometimes confusing.
No, a layout is not a widget or a container. A layout is more like a "helper" that's attached to a window and figures out the best place to put each widget.
This example from the Qt docs should help (http://qt.nokia.com/doc/4.2/layout.html):
QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
window->setLayout(layout);
window->show();
QLayouts are layout managers. They manage the positioning and resizing of widgets inside a parent widget. From the docs:
Layouts are an elegant and flexible way to automatically arrange child widgets within their container. Each widget reports its size requirements to the layout through the sizeHint and sizePolicy properties, and the layout distributes the available space accordingly.
For example, setting the layout of a widget to QHBoxLayout will result in its child widgets being laid out horizontally.
You can read more about it here.
Related
I'm trying to render some controls myself in the compile time. Meaning that i'm trying to create for instance a Xamarin.Forms.Button and get it's native Windows.UI.Xaml.Control.Buttonimage (or FormsButton to be accurate)all in compile time.
I was trying to apply this on a ContentView so by applying this theory, this is the code to accomplish that:
var contentView = Element.Content;
var renderer = contentView.GetOrCreateRenderer();
var nativeControl = renderer.ContainerLayout;
By doing so i get a native UWP FrameworkElement with all the properties of the ContentView like the background and so on. However, i don't get the children rendered. That actually makes sense because basically you render just the ContentView. But i have no idea how to render all it's children. Hope that the solution applies to any XamarinForms control that has a Children (or a Content) property
I Creating GUI i stetic editor (GTK#) for MonoDevelop. And i can't place widgets (like buttons) where i want. When i put button in to GUI it take all place on screen and i cant change it and place on correct spot, how to do that? I looking for something like layout manager in Java Swing.
So what component i need to use to create correct background for placing widgets?
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
}
This is how look my MainWidows constructor.
Gtk+ layout is done using containers. Basically you need to place containers (Gtk.HBox, Gtk.VBox, ...) inside containers and then fill them with widgets. You can set the padding, the spacing between siblings, the relative weight when expanding, how to expand/fill and so on. Very Css-ish.
Apart from simple boxes, you have some containers that are a bit more specialized and allow you to place child widgets in special ways. For example Gtk.Table let you arrange children spanning rows and columns of the table. Gtk.Fixed allow for absolute positioning of children.
If you're using MonoDevelop look at the toolbox and experiment with the Containers widgets.
If you build a custom UIView, and integrate it inside of a parent view/view controller in interface builder, the graphical element representing your custom view is invisible, if you don't specify a background color (I don't).
Is there any way, solely during development, to identify different custom views? Any hacks/tricks to distinguish them?
The closest I could come up with is setting the background color in IB, then removing the background in the implementation of the custom view.
Bounds Rectangles
You might find bounds rectangles useful. You can turn them on by going to the menu bar and choosing Editor > Canvas > Show Bounds Rectangles.
Here's an example. I have a view (a UICollectionViewCell subclass) laid out in a nib. It has a single-line label, a two-line label, and a custom subview. The custom subview itself contains a smaller custom subview. Here's the nib with bounds rectangles off:
Here's the same nib with bounds rectangles on:
Background Color Override
Here's another technique that builds on the idea of setting the background color. This technique requires your deployment target to be iOS 5.0 or later.
As you described, set the background color to make the view visible in the nib:
Then switch to the Identity Inspector and add backgroundColor in the User Defined Runtime Attributes section. Set it to the background color you want the view to have at runtime. For example, if you want it to be white at runtime:
If you want the background color to be clear, you can set backgroundColor to a color with opacity 0, or you can set it to “Nil” instead of any color:
That approach of setting the background color in Interface Builder, but resetting it in code is a simple, but effective technique. Two refinements:
If you have multiple custom views on a single storyboard scene, you can save yourself from having to programmatically clear the background color for all of them individually by using IBOutletCollection. So, in Interface Builder, give them all background colors and then add all of your custom views for a given scene to a collection. You then can set the background color for all of them in a single statement. So, for example, if you have a dozen controls on one scene all in a single IBOutletCollection is named viewsCollection:
#property (strong, nonatomic) IBOutletCollection(UIView) NSArray *viewsCollection;
you can clear the background color of all of them in a single statement:
[self.viewsCollection setValue:[UIColor clearColor] forKey:#"backgroundColor"];
You can also make the identification of your custom views in Interface Builder a little easier by setting the "Label" in the "Document" properties on the "Identity inspector":
Once you've done that, when you look at the document outline in the left side of the main panel, you'll see your labels show up:
Then, using the document outline makes it easier to identify your individual views in the scene. You can use a random label like I did here, or you could use the name of your custom view class, or whatever.
If i create WinForms / Qt / Gtk application i use so called "layout managers" (or "geometry managers") to automatically layout my UI according to text inside widgets and my instructions. For example, if i layout a window with big edit field and 2 buttons below it aligned right i write following code "Create a window with vertical layout manager. Add edit widget as first item and horizontal layout manager as second item. For horizontal layout manager add spacer as first item, button as second and button as third". My window will be automatically resized according to button labels and edit field size. Also, if i resize my window all items will be resized automatically.
Is it something like layout managers for OSX? I have found that NSView can be added into hierarchy, but i can't find any ways to instruct parent NSView something like "arrange child NSViews vertically".
You should read about Cocoa Auto Layout, new in OS X 10.7.
I'm trying to create a simple application that draws a grid in a custom view.
The custom view size is fixed (it doesn't depend on the size of the window).
The custom view is embedded in a scroll view to be able to explore the grid when the scroll view can't display the entire custom view.
Now i want to add sliders for controlling the grid parameters (nb raws, nb columns, tile width, tile height, ...), and these parameters influence the size of the custom view.
As an experiment, i'm trying to bind one slider's value to the width of my custom view but fail to find a way to do it.
How am i supposed to do this sort of things ?
Is it possible to do it in InterfaceBuilder ? I expected to find a width binding in Bindings Inspector Window but it's not there, curiously ;-)
Thanks.
You can't bind the width of a plain NSView, and binding to a property of a view is always a bad idea. View properties are seldom observable. Moreover, there is no width property; it's one member of the structure that is the value of the frame property, which you must set all at once or not at all.
As for exposing bindings in your custom view, you can do that, provided you keep the properties observable (which consists of little more than only changing the property's value using its setter). You'll need to expose the bindings in your view class's initialize method, and you'll need to write an IBPlugin.
See also the Cocoa Bindings Programming Topics.