How to expand layout in scrollarea with added content - qt

I have a ScrollArea which contains a frame to hold a layout. By pressing a button (Plusbutton below) I add new Widgets to the layout in the ScrollArea.
After adding several Items the Widgets get smaller and cut off. I want the layout to get bigger with added Widgets and want be able to scroll further down to see the added content.
[Edit]
I have constructed those widgets in Qt designer so i cant provide that much code. But i hope this will help you understand my problem.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->scrollArea->setWidgetResizable(true);
ui->verticalLayout_6->insertWidget(0,ui->blueframe,Qt::AlignTop);
}
void MainWindow::on_addwidget_pressed()
{
ui->verticalLayout_6->insertWidget(2,new Toolbar);
}

Related

Qt Mdi Area get size not including scroll area

I have some issue with Qt MdiArea where I created a sub-window which fit inside MdiArea, and then I check the size of MdiArea got correct result. But if I move the window to the end of MdiArea which will make scroll bar appear, and check the MdiArea size again, but getting the same size as window without scrollbar. I think the scroll area not considering while getting size, but only the viewport size returning.
How I can get the total size including scrollable area of the MdiArea.
Code:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->mdiArea->setOption(QMdiArea::DontMaximizeSubWindowOnActivation);
// Set Mdi Area as the central widget
setCentralWidget(ui->mdiArea);
QWidget *widget = new QWidget(ui->mdiArea);
QMdiSubWindow *w1 = ui->mdiArea->addSubWindow(widget);
w1->resize(320, 240);
// And show the widget
widget->show();
}
void MainWindow::wheelEvent(QWheelEvent *event)
{
qDebug()<<"mdiArea size: "<<ui->mdiArea->size();
}
Correct Result without scroll bar.
Wrong Result with scroll bar.

QListWidget inside QGraphicsScene not scrolling text items

I have a QListWidget inside a QGraphicsScene. I add new items with QLineEdit inside. When QListWidget fills and scrollbars are active on the scroll, text does not scroll, current item representation does.
Complete code Git: code
EDIT:
I included QCheckBox inside a Horizontal layout to show why I need the setItemWidget function.
Answer:
The text does not scroll because setItemWidget is:
void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget)
Sets the widget to be displayed in the given item. This function should only be used to display static content in the place of
a list widget item. If you want to display custom dynamic content or
implement a custom editor widget, use QListView and subclass
QItemDelegate instead.
it's nothing about QGraphicsScene.
Solution:
If you want to make the text editable. it's much simpler then you customize the QItemDelegate.
First, set the list widget with an edit trigger, tell the widget when to start editing.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
...
ui->listWidget->setEditTriggers(QAbstractItemView::DoubleClicked);
}
Then when you create & insert the QListWidgetItem, make sure each item is editable.
※Replace you whole on_pushButton_clicked function as below:
void MainWindow::on_pushButton_clicked()
{
QListWidgetItem* item = new QListWidgetItem("name");
item->setFlags(item->flags() | Qt::ItemIsEditable);
ui->listWidget->insertItem(ui->listWidget->currentRow() + 1, item);
}

QDockWidget will not show contents

I am attempting to create a specialised QDockWidget, with some contents, that I can use in other Qt5 UIs. To do this, I have created a QDockWidget subclass complete with a UI form, called SpecialDockWidget. The contents (a QTreeWidget) are added in the UI form.
In my main UI, I have added SpecialDockWidget as a QDockWidget promotion, have promoted the desired dock widget, and everything compiles OK. However, when I run the application my promoted dock widget contains the title of the SpecialDockWidget in its title bar, but the contents remain empty. This only happens if the dock widget is made part of the main UI (whether floating or attached), and the dock widget displays the contents successfully if it is instantiated on its own as a top-level widget.
The Qt documentation says the following with regards to QDockWidget::setWidget():
If the dock widget is visible when widget is added, you must show() it explicitly.
Note that you must add the layout of the widget before you call this function; if not, the widget will not be visible.
I have tried calling show() on both the SpecialDockWidget and the contents, and have set the layout myself, but the contents still don't show up.
Can anyone tell me what I might be doing wrong?
EDIT: Some photos showing what's going on:
Dock widget code:
#include "specialdockwidget.h"
#include "ui_specialdockwidget.h"
#include <QVBoxLayout>
SpecialDockWidget::SpecialDockWidget(QWidget *parent) :
QDockWidget(parent),
ui(new Ui::SpecialDockWidget)
{
ui->setupUi(this);
}
SpecialDockWidget::~SpecialDockWidget()
{
delete ui;
}
Main window code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// This produces the top-level dock widget
// SpecialDockWidget* w = new SpecialDockWidget();
// w->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
The QDockWidget is created in the Designer so it's not very clear how the tree is added to the QDockWidget. If the tree is simply a child of the QDockWidget it's not correct. You must add the tree to the QDockWidget using QDockWidget::setWidget().
I suggest to not use the Designer to create a subclass of QDockWidget because it seems not possible to add the content correctly. You can do something like this instead.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDockWidget *dw = new QDockWidget(this);
QTreeWidget* tw = new QTreeWidget(dw);
dw->setWidget(tw);
addDockWidget(Qt::LeftDockWidgetArea, dw);
dw->show();
}
Of course, if you need to put inside a dock widget a more complex widget, you can create the content with the Designer as a simple QWidget subclass and add it to the dock widget in the same way.
I think your main window's constructor should look like, for example:
MainWindow::MainWindow(QWidget *parent)
:
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
SpecialDockWidget *w = new SpecialDockWidget();
// Add dock widget to the specified area of main window.
addDockWidget(Qt::LeftDockWidgetArea, w);
}

Have to push QPushbutton twice

I want to make a list of toolbuttons in QT. The toolbuttons should appear in a scollarea. This list should appear when a pushbutton is clicked. I have made the code and it works, exept that I have to push the pushbutton twice in order to let the list appear. Here is my code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
find_btn.setParent(ui->centralWidget);
find_btn.setGeometry(480,250,130,132);
viewport.setLayout(&scrollLayout);
scrollArea.setParent(ui->centralWidget);
scrollArea.setGeometry(0,116,339,404);
scrollArea.setWidget(&viewport);
connect(&find_btn,SIGNAL(clicked()),this,SLOT(import()));
}
void MainWindow::import()
{
button.setCheckable(true);
button.setMinimumSize(317,60);
button2.setCheckable(true);
button2.setMinimumSize(317,60);
scrollLayout.addWidget(&button);
scrollLayout.addWidget(&button2);
viewport.adjustSize();
}
So when I push the "find_btn", the scrollarea with the buttons inside should appear. At the moment the scrollarea with buttons appears, but only after I click the "find_btn" twice.
I guess I have to update the scrollarea or something like that. Maybe the connect is causing problems? Can anybody help?
There are a couple of options you could try:
viewport.update();
or
scrollArea.viewport()->update();
or
QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
or any combination of them.
Probably the GUI is not redrawn until a redraw is forced by pressing the button again.

Programmatically scroll QScrollArea

I have a Widget with QScrollArea in it and I want it to be scrolled down right after the widget containing it is shown. I tried:
scrollArea->ensureVisible(0,100, 20, 20);
It works only when invoked by user (pushing button for example). Putting it in widget contstructor or showEvent doesn't work. Can it be done automatically?
I believe you can scroll the QScrollArea content by setting positions to its horizontal and vertical scrollbars. Smth, like this:
scrollArea->verticalScrollBar()->setValue(scrollArea->verticalScrollBar()->value() + 10);
scrollArea->horizontalScrollBar()->setValue(scrollArea->horizontalScrollBar()->value() + 10);
code above should scroll contents of the scroll area 10 pixels down and 10 pixels right each time it gets called
hope this helps, regards
Edit0: extra code snippet showing how to scroll the area in the form's constructor:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QLabel *imageLabel = new QLabel;
QImage image("my_large_image_file.JPG");
imageLabel->setPixmap(QPixmap::fromImage(image));
ui->scrollArea->setBackgroundRole(QPalette::Dark);
ui->scrollArea->setWidget(imageLabel);
ui->scrollArea->horizontalScrollBar()->setValue(100);
ui->scrollArea->verticalScrollBar()->setValue(100);
}
I have spent some time with the debugger and find out that scriollArea has 0 sizes in the constructor, so it looks like it is possible to scroll something only when all the widgets are created and visible. Scrolling in the showEvent of window works fine.

Resources