So I am in the works of making my first GUI. So far I have made a groupbox and then made a layout and added buttons widgets to that layout(and then added that layout to the group).
All this works fine but when I then compile it, it fills the whole width of the groupbox and something like "50" in height.
I then tried to use setGeometry on the buttons but this does nothing, I can set a size with setFixedSize() but not a positions, why is this? and how can i set a position on the buttons?
You should try to add horizontal and vertical spacers in your layout to position your widget in a proper place in layout. For instance you can place two horizontal spacers on the left and right side of your button and two vertical spacers at the bottom and top. This way your button always stays at the center of layout with an appropriate size.
If you are using Qt Designer you can find them in the Spacers section and add them to your layout. If not you can use QSpacerItem like:
QSpacerItem *horizontalSpacer;
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
gridLayout->addItem(horizontalSpacer, 0, 0, 1, 1);
Related
I've created a custom widget containing a few widgets in a horizontal layout:
And the goal is to display several of these in a list so I've added them dynamically to a parent vertical layout. The problem I'm having is that there's too much space in between my custom widgets when they're added to the vertical layout:
I want them to be tightly packed so that there's only a small space in between. I've added a spacer at the bottom and played around with the size policies etc but to no avail. Below is the code to add the widgets. Any and all help appreciated.
// Draw the nodes area
QVBoxLayout* nodeVLayout = new QVBoxLayout;
NodeWidget* node1 = new NodeWidget;
NodeWidget* node2 = new NodeWidget;
QSpacerItem* spacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding);
nodeVLayout->setSpacing(1);
nodeVLayout->addWidget(node1);
nodeVLayout->addWidget(node2);
nodeVLayout->addSpacerItem(spacer);
ui->scrNodes->setLayout(nodeVLayout);
In the layout options check that the margins and spacing are set appropriately.
By default the margins on the top and bottom were set to 9 pixels which was causing the problem.
I'm working on Qt interface now and I have a widget in the center of the window, the widget has fixed size. when I resize the window with mouse the widget stays at the left of the window. I don't want this, but I want to stay it in the center.
I tried this;
listWidget->setFixedSize(640,480);
listWidget->adjustSize();
layout->addWidget(listWidget,Qt::AlignCenter);
but it doesn't do that.
can you help me?
This code works fine for me :
QGridLayout * layout = new QGridLayout(this);
QListWidget * listWidget = new QListWidget(this);
listWidget->setFixedSize(640,480);
listWidget->adjustSize();
layout->addWidget(listWidget,0, 0,Qt::AlignCenter);
But an other way is to put two vertical spacers and two horizontal spacers in the layout. One should be placed at top most, the other one at the bottom, one on the left and one on the right :
I am currently learning to use wxWidgets in Microsoft Visual Studio C++, and I think that I have my head wrapped around the concept of Sizers. However, I can't seem to get them to behave how I want them to while resizing. I am following a tutorial (the link to which I seem to have misplaced, but it is pretty simple), which aims to create a dialog window with an expandable text box and two buttons beneath it which should stay approximately centered. My code for the window is below, found within the constructor of my dialog window:
vertSizer = new wxBoxSizer(wxVERTICAL); // Create vertical (parent) sizer, will contain text box and child sizer
vertSizer->Add(
new wxTextCtrl(this, -1, "My text", wxDefaultPosition, wxSize(100,80), wxTE_MULTILINE),
1, // vertically stretchable
wxEXPAND | wxALL, // horizontally stretchable, borders all around
10); // Add text box to parent sizer
horizSizer = new wxBoxSizer(wxHORIZONTAL); // Make child sizer, will contain buttons
wxSizerFlags ButtonFlags(1); // Make controls stretch hoizontally (to cover entire sizer area)
ButtonFlags.Expand().Center().Border(wxALL,10); // Make controls expand vertically, add border
horizSizer->Add(new wxButton(this,wxID_OK,"OK"), ButtonFlags); // Add first button
horizSizer->Add(new wxButton(this, ID_Cancel,"Cancel"), ButtonFlags); // Add second button
vertSizer->Add(horizSizer); // Add child sizer to parent sizer
SetSizerAndFit(vertSizer);
The displayed window looks correct (a large text box on the top portion of the window, with two properly spaced buttons, side-by-side, below it). However, when the window is resized, the text box expands both vertically and horizontally (expected behavior), but the buttons stay in place, left-aligned (unwanted behavior). (I apologize for not being able to post a picture; I have a couple prepared, but I can't post them with as little reputation as I have.)
The formatting on initialization great, however I would to maintain that formatting while resizing. Meaning, I want the two buttons beneath the text box to stay in the middle of the window, still side-by-side (although, it's acceptable for the distance between them to increase as the window gets bigger). Alternatively, I would like the buttons to increase in size horizontally, so that each button takes up half of the area beneath the text box.
I apologize if a similar question has already been asked, but I haven't found a satisfactory answer on this site in my research.
Any help with this would be greatly appreciated. Thank you!
You need to tell the outer box sizer that it should expand the horizontal child sizer as well:
vertSizer->Add(horizSizer, 0, wxEXPAND);
The second parameter, 0, tells the vertical sizer to expand the child sizer only horizontally. For box sizers, passing 0 as the second parameter and wxEXPAND as (part of) the third will make sure that the added child control or sizer is expanded in the "other" direction, i.e., for a vertical sizer, the child is expanded horizontally whereas for a horizontal sizer, the child is expanded vertically.
Passing 1 as the second parameter will, as you already know from the text box, expand in both directions.
Not sure if this is what you're looking for but the key is making a sizer and adding the panel inside that you want to stretch in this format:
Sizer->Add(Panel, 1, wxEXPAND | wxALL);
It stretched the panel out with the width of the window (the key here is wxAll) as per the documentation. Hope this helps someone and future me when I forget
I have a QGraphicsView that renders my game. This view fills the entire screen at all times. In the top right I have another QGraphicsView which I'm using as a mini-map; it sits over the game view. I want this mini-map to be anchored to the top right of the screen, always maintaining its size. This code almost works, except that the left side of the mini-map never changes (which is to be expected).
void MainWindow::resizeEvent(QResizeEvent *event)
{
mainWindow->graphicsView->resize(event->size().width(), event->size().height());
QRect newRect(mainWindow->miniMapGraphicsView->geometry());
newRect.setRight(event->size().width() - 20);
mainWindow->miniMapGraphicsView->setGeometry(newRect);
}
How can I do this?
When you want to position widgets in a certain way, layouts are usually the best way to do it.
Add a QFormLayout to your graphicsView and set its layoutDirection to Qt::RightToLeft. Then add your miniMapGraphicsView to the layout. Editing the properties of the mini map, set its horizontal and vertical sizePolicy to Fixed and set its minimumSize and maximumSize to the dimensions you would like it to be.
An alternative would be to use a QGridLayout and use horizontal and vertical spacers to push the mini map to any corner of the view.
NOTE: Layouts have margins set by default so if you want your widgets to align snugly at the edges, zero them out.
Hello everyone, here is my code:
myplot *p = new myplot(gao.structpayloadgraph,
gao1.structpayloadgraph,
gao.structcol-2, "payload");
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->scrollArea->setWidgetResizable(false);
p->resize(ui->scrollArea->size().width(), ui->scrollArea->size().height());
ui->scrollArea->setWidget(p);
I want p to take up the full available space of the scrollbar area and fit itself. However, the appearance looks 'squeezed' even though I called the resize function. What should I do to achieve the desired effect?
You have to treat the scroll area content widget as a normal QWidget. If you want automatic resize and you must use layouts in Qt. Try the following :
QVBoxLayout layout = new QVBoxLayout( ui->scrollAreaContent);
layout->setMargin(0);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
ui->scrollAreaContent->setLayout( layout);
layout->addWidget(p);
NOTE: ui->scrollAreaContent is a guess, but I think you are using ui files and default content widget is named like that ...
Go to the top right of the QT creator designer screen (Object, Class), right click on the QScrollArea row and select the "Lay Out" menu item, choose a layout (eg vertical or horizontal layout), make sure that your QWidget has a minimum or above size policy. Your scroll widget should now resize with the layout.