I have QWidget. It has vertical layout. Inside: QQuickWidget (call it quickWidget
) and QWidget (call it inputWidget).
QQuickWidget has ListView inside.
When I change inputWidget height by setMinimumHeight(...); setMaximumHeight(...);, then the QML quickWidget object covered by this inputWidget. And last QML ListView item covered by inputWidget. Picture for example
I tried to use ListView.positionViewAtEnd() when inputWidget change his height. But there is any opportunity dont move elements inside Qml ListView after change height of inputWidget?
Related
I am using QT4 Designer have a layout that looks basically like this:
QWidget
QVBoxLayout
QFormLayout
QLineEdit
QListWidget
QPushButton
Each control is on a separate line.
I did the layout assignment trick to get everything to fit to the parent, and if I reduce the height the QListWidget shrinks as hoped, but if I increase the height the QWidget.geometry.Height never exceeds 192. The Vertical Policy is set to Expanding and the maximumSize.Height is 16777215, but no other policy seems to allow the QListWidget to grow. Width grows correctly, however.
What setting am I missing? Should I instead anchor the QPushButton to the bottom of the QFormLayout, and if so, how?
Well, I found my own answer - I selected the QFormLayout, right-clicked, and chose Lay out/Lay Out in a Grid and everything snapped into place! So, the new, working hierarchy is:
QWidget
QVBoxLayout
QGridLayout <<-- Changed from QFormLayout
QLineEdit
QListWidget
QPushButton
I am trying to have a widget live freely inside a large scrollable panel. When I scroll around the QGraphics view, the child widget does not draw properly— its sub-rectangles will blank out, and the borders (which are rounded) are not blended with the canvas. I have:
QApplication qapp(argc, argv);
QGraphicsView view;
QGraphicsScene scene;
// a simple QWidget with a gridlayout and some child objects:
MyQWidget mywidget(NULL);
scene.addWidget(&mywidget);
view.setScene(&scene);
view.setSceneRect(0,0,1280,1280);
view.show();
view.resize(512,512);
qapp.exec();
I get the same bogus rendering if I use a QGraphicsProxyWidget and scene.addItem() instead.
If instead I parent my custom widget to the QGraphicsView, it renders properly, but then the widget does not seem to be part of the QGraphicsScene, because it doesn't scroll around anymore and remains fixed to the parent window.
What's going on?
Edit: Images of the blanking problem, with MyWidget replaced by QPushButton:
Before scrolling:
Scrolling a little down and to the right:
Second edit: I have a Retina display. Could this be related? Is there special setup for Retina with Qt?
You should be getting an error when you try to add a widget to the scene which already has a parent:
QGraphicsProxyWidget::setWidget: cannot embed widget which is
not a toplevel widget, and is not a child of an embedded widget
QGraphicsScene::addWidget() is a wrapper around QGraphicsProxyWidget::setWidget which documentation says:
widget must be a top-level widget whose parent is 0.
To have borders blended you need to set widget to be transparent:
mywidget.setAttribute(Qt::WA_TranslucentBackground, true);
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....
In my Qt program, I programmatically generate a modal QDialog. I want to show two widgets in this dialog window: A custom widget showing a camera output and a QTableWidget, showing the pixel coordinates of the corners found in the camera image. I generate a QHBoxLayout and add my custom widget and the QTableWidget into it. Then I set this QHBoxLayout as the Layout of the QDialog window. What I want to achieve is to share the available space in the QDialog's window area equally between my custom QWidget and the QTableWidget, horizontally, by using a QHBoxLayout. But I always end up with QTableWidget occupying the whole QDialog area, by overlapping my custom widget. How can I instruct these two widgets to exactly share the QDialog area?? Note that I first add my custom widget and then the QTableWidget into the QHBoxLayout.
Make sure on your custom widget you've specified a minimumSizeHint and a sizeHint, this instructs the QLayout manager that the widget requires a specific space. To have them split equally you'll be best off detecting the size of the QDialog and then specifying the width for both by removing the boundary sizes (spacing between widgets + space to QDialog edge) and dividing it up.
I'm a bit new to QT but have to work on existing code. Here's the case:
I have a class extending QDialog. the constructor sets a QGridLayout then adding three other widgets to it. One of the widgets is a QScrollArea containing a QGroupBox. this QGroupBox has a QVBoxLayout and there I'm adding a list of widgets at runtime. The size of the scroll area should grow until a given limit is reached before showing the scrollbars so that they are only used when the dialog would grow too high. I've found that the sizeHint of the outer layout doesn't update when the sizeHint of the scroll area updates. How can I refresh this, or is there a better way to resize the parent dialog?
What about using widgetResizable property of QScrollArea? It should try to resize view to avoid using scorllbars.