I am a newbie to Qt and facing issue in my application. So here is what I am trying to do.
I have a class with QWidget as parent. This class has a grid layout on it. I set this widget as a central widget to main window. (this thing worked fine though this widget is not centered on Main Window.)
Now I wanted to rotate this widget in 90 degrees so that it can bee shown on device in landscape mode. So I created a graphics view and added this widget to it. (created a scene added widget to that and assigned scene to view.) then this graphics view was rotated and made central widget. this worked amazingly and it was very well centered, looked good on device as well.
But in final integration we want to have all QWidgets so Graphics View is not an option. To tackle this I created a Qwidget member inside my class. applied the layout to it. added this widget to graphicsView and my class was made parent to it.
This also works but has the similar issue I faced in first step, it sits in the top left corner of MainWindow and does not adjust to center. To make things even worse, when deployed on device it was not applied to entire screen. Widget was sitting in some part of top left area and had scroll bars to it! I even tried the set Window State to maximized but had no effect on it.
Here is what I tried
The widget is created inside my class and been added to Graphics View. this view has my class as parent and it will be rotated by using rotate api.
m_gridContainer = new QWidget();
m_gridContainer->setAutoFillBackground(true);
m_gridContainer->setPalette(blackPalette);
m_gridContainer->setLayout(m_grid);
m_gridContainer->setMinimumSize(480,265);
m_scene = new QGraphicsScene(0, 0, 480, 265);
m_window = new QGraphicsView(m_scene,this);
m_scene->setBackgroundBrush(brush);
m_window->setAlignment(Qt::AlignCenter);
m_scene->addWidget(m_gridContainer,Qt::Widget);
To use it from main Window
m_window = new CMyWidget(label, m_txtBox->text());
m_window->getRotatedWidget(90);
setCentralWidget(m_window);
I tried whatever I can but this widget is not being shown full screen (in just a small area) and still have scroll bars to it. I have no idea what exactly is happening there.
m_gridContainer being a graphics item part of a graphics scene, it doesn't receive the resize events of you main window.
You have to handle the resizeEvent() on your CMyWidget and adjust the size of your m_gridContainer accordingly, e.g.
void CMyWidget::resizeEvent ( QResizeEvent * event )
{
m_gridContainer->resize(event->size());
}
BTW, if you are handling the rotation for use on a mobile device, it is managed by Qt itself (by simply resizing your main window). You don't have to do it in your code (you'll actually end up with a widget looking like it has been rotated twice).
Related
I am working on a GUI that at one point should display a long horizontal ROI of a camera feed. For that I am writing a widget consisting of an ImageView that is using a ViewBox and an ImageItem and then add that widget to a constrained space on a layout of the GUI. My problem is that the image from the camera is not visible upon opening the window but 'hidden' outside of the field of view of the widget (a little bit down). Upon resizing the window, it appears centered.
This is how it looks after resizing before that it is where the arrow indicates
This is a minimal version of the display widget in question:
class CameraViewerWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.viewport = GraphicsLayoutWidget()
self.view = self.viewport.addViewBox(lockAspect=False, enableMenu=True)
self.img = pg.ImageItem()
self.view.addItem(self.img)
self.imv = pg.ImageView(view=self.view, imageItem=self.img)
layout = self.layout()
layout.addWidget(self.imv)
def update_image(self, image, auto_range=True, auto_histogram_range=False):
self.imv.setImage(image, autoRange=auto_range, autoHistogramRange=auto_histogram_range)
It is then added to the GUI via addWidget() and update_image() is called repeatedly using a QTimer.
Obviously the auto_range argument does not help here. I also tried to use the setLimits() and setRange() methods of the ViewBox but that also did not change the situation. I am suspecting that as I add this Widget to a contrained size on the layout of the main GUI (maximumSize = 100) the ImageView does not know how much space it has but I am not sure how to test that and a bit confused by the different coordinate systems used here. Did anyone encounter similar issues or is able to see my error here?
I'm new with Qt and I want to implement a scrollable widget which can grow dynamically, e.g. by adding buttons into it when another button is pressed. I try to implement it using the following layout and code:
scrollArea = new QScrollArea(ui->outerWidget);
scrollArea->setWidget(ui->innerWidget);
layout = new QVBoxLayout(ui->outerWidget);
ui->innerWidget->setLayout(layout);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// code for PushButton to add buttons in innerWidget
void MainWindow::on_pushButton_clicked()
{
QPushButton *button = new QPushButton("button"+QString::number( nameCounter ));
nameCounter ++;
ui->innerWidget->layout()->addWidget(button);
}
This implementation has two problems when I run the program, first the innerWidget appears out of place (I define its position in Qt Creator's Design mode) and second after many widgets are added in the layout the scroll bar is doesn't grow, but instead the widgets are become smaller to fit into the layout:
In another thread it was suggested to set widgetResizable to true:
scrollArea->setWidgetResizable(true);
The scroll bar seems to work now but the new problem is that the innerWidget becomes very small so it is barely visible:
So, how can I achieve a scrollable widget that will respect the size and position of the design?
have no environment to verify but good luck.
to place scrollArea the target position,
Manually call SetGeometry, or
Place a QScrollArea in ui form, (suggested if it's static), or
Place a widget in your target position, and set QScrollArea's parent to it, and add QScrollArea to its layout, and Set QScrollArea to be expanding.
manually create a QWidget innerWidget and assign it to scrollArea via QScrollArea::setWidget(*QWidget), try different size policy to innerWidget, such as "Preferred".
Also be aware of Qt's reference mentioned : void QScrollArea::setWidget ( QWidget * widget )
Sets the scroll area's widget.
....
Note that You must add the layout of widget before you call this function; if you add it later, the widget will not be visible - regardless of when you show() the scroll area. In this case, you can also not show() the widget later.
Above list solutions, below are reasons to the problems you mentioned:
scrollArea->setWidget(ui->innerWidget); Setting a widget to ScrollArea will change the parenting and layouting of ui->innerWidget, so the geometry values written in ui form (in Qt Creater) will no longer take effect, this is the reason of innerWidget out of place. ui->innerWidget is no longer a child of outerWidget, it's geometry will follow its new parent (but not scrollArea, there's some tricky layouting inside QScrollArea". To be clear, innerWidget is not helpful to locate scrollArea in such scenario.
In your first clip of code, widget 'scrollArea' is created with parent outerWidget, again no size policy or layout or geometry is specified, so scrollArea will by default be placed at the left top corner of the parent "outerWidget". To place scrollArea to your target geometry, you can " set geometry manually " or " assign innerWidget as scrollArea's parent and expand scrollArea". Obviously the latter method cannot assign ui->innerWiget to scrollArea->setWidget().
scrollArea->setWidgetResizable(true); makes the scrollArea "shrink" at left top corner of outerWidget. This is because, QScrollArea does not increase along with it's contents, it can scroll to display all of it contents so the required size of QScrollArea can be as small as possible. Once the 'Resizable' property is set to "true", QScrollArea decides to shrink to its minimum necessary size, thus the size to display its scroll bar and scroll buttons....
I am working on a project where I have to display a pretty large (vertically) main Widget.
In the initial Version of my GUI it was just added as the central Widget of a QMainWindow, which caused the Problem that on small screen resolutions the controls on the Bottom of the Widget are unreachable.
To solve this i wrapped a QScrollArea around the main Widget, but now the main window is always relatively small even if it doesn't have to.
What do i need to change so the Main Windows (vertical) size is large enough to show all the contents unless it would be too large for the screen resolution? Also I don't want it to be stretched, so simply always using the whole vertical screen resolution is not an option. Ideally the size should be fixed to the size needed by the contents (w/o the scroll area) and only smaller where needed.
Overriding the sizeHint method did only resulted in a small enlargement of the Window and setting the minimal height brings me back to the beginning where some of the controls are not assessable on small resolutions.
Since i am new to QT I am actually out of ideas how to google the solution because most Solutions I can find are about sizing components inside a Window and not the Window itself.
By default a QScrollArea will not attempt to expand to fit its contents. In order to do this you will need to re-implement QScrollArea's sizeHint() to return the size of QScrollArea's child widgets.
In your question it sounds like you were trying to re-implement MainWindow's sizeHint? re-implementing sizeHint on the top-level window will have no effect as sizeHint designed for use with widgets inside layouts.
My application window can be resized to zero-size. I do not want to restrict the minimum window size. There are several widgets (QLineEdits, QLabels, QPushButtons) layoutet by a HBoxLayout.
At first, all widgets in the HBoxLayout were resized without respecting their sizeHint when the window was very small.
Then I used setFixedSize(sizeHint) on them. Now they do not get shrunk below their sizeHint, but instead they start overlapping when the window is very small.
What I want is what Thunderbird does (screenshots attached):
It smoothly hides the widgets by moving them out of the window border. Their size is unchanged.
How can I achieve that with the Qt Layouting system?
my application: normal window
http://i.stack.imgur.com/WLgLy.png
my application: small window
http://i.stack.imgur.com/IIhhs.png
Thunderbird: normal window
http://i.stack.imgur.com/AYJ83.png
Thunderbird: small window
http://i.stack.imgur.com/ZyQou.png
Put your widgets into a QScrollArea
Set the vertical and horizontal scroll bar policy's to AlwaysOff
Layout your Scroll area as you wish
Done & done
Probably that's not what you want but consider this:
Put your all stuff in a widget (named just 'widget' here), do not put this 'widget' into any layout just leave it free and add this event function to your window class:
void MainWindow::resizeEvent(QResizeEvent *event){
ui->widget->move(QPoint(event->size().width()-ui->widget->width()-20,ui->widget->y()));
QMainWindow::resizeEvent(event);
}
it works!
I am new to QT. I'm trying to understand the layout mechanism by trying to implement this small window seen below. It has the following elements under the QWidget that's the main window:
One big QWidget that stretches on all the client area.
Two QWidget containers on the top of the window. Both should have the same height, but the right one stretches horizontally, as the window grows/shrinks.
one button container widget on the top right, with fixed height and width
Large QWidget container filling the rest of the client area, that should resize as the window resizes.
The parent window itself is resizeable.
I'm looking for hints as to what layout I should use. How do I achieve this programatically? define what stretches automatically, what stays with a fix size? and how the proportions are kept where they need to be kept.
I'd appreciate any pointer you may have.
The easiest, and IMHO best, way to accomplish this is via the QHBoxLayout and QVBoxLayouts. You can do this via the designer in QtCreator, but I find it doesn't work perfectly if you need to adapt things over time. If it's a static set of widgets, I do suggest designing it using the QtCreator designer as it'll greatly simplify your life.
If you're going to do it programatically, the main window should be set to use a QVBoxLayout and then two sub-QVBoxLayout's after that, where the bottom one is configured to take any space it can get. Then in the top QVBoxLayout, add a QHBoxLayout with your two upper components.
to set a widget to fixed size in code you call setFixedSize( int h, int w ) on the widget. To do it in Designer click on the widget and look in the property editor in the QWidget section. open the sizePolicy thingy and set horizontal and/or vertical to fixed. Then open Geometry and set the width and Height.
To make them stretch at different ratios in code you use a separate argument when using a box layout. eg layout->addWidget( button1, 1 ); layout->addWidget (button2, 2); this would cause button2 to expand at twice the rate of button1. To do this in designer, open the sizePolicy property of the widgets and set the HorizontalStrech and/or VerticalSretch. Note that the size policy needs to not be Fixed in this case for the direction you want to set the stretch on. Also it will never let a widget shrink below its minimum size (it would rather mess up the ratio than shrink something too small).