Programmatically scroll QScrollArea - qt

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.

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.

How to expand layout in scrollarea with added content

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);
}

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);
}

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.

QSplitter does not correctly set sizes

I have a horizontal QSplitter with two widgets. I want to replace the right hand widget with a new one in a way that the proportions the user has set are maintained. Below is a simplified version of the code I currently have:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
splitter = new QSplitter(this);
splitter->setOrientation(Qt::Horizontal);
leftWidget = new QPushButton("left", splitter);
rightWidget = new QPushButton("right", splitter);
splitter->addWidget(leftWidget);
splitter->addWidget(rightWidget);
setCentralWidget(splitter);
}
void MainWindow::swapLayout()
{
QList<int> sizes = splitter->sizes();
rightWidget->deleteLater();
splitter->update();
rightWidget = new QPushButton("new right", splitter);
splitter->addWidget(rightWidget);
splitter->setSizes(sizes);
}
swapLayout() saves the sizes, removes the right widget, adds a new right hand widget and attempts to reset the sizes. However the left hand widget occupies 100% of the space. Without trying to restore the sizes the widgets both take up 50% of the space.
I think the actual order of operations is:
saving sizes of 2 widgets;
adding 3rd widget.
setting size for 3 widgets (and sizes.at(2) is 0 by default.
Deleting 2nd widget.
It caused by the fact that deleteLater() only schedules deleting, and actual deleting is processed after you exit swapLayout() method. Try delete rightWidget; instead of rightWidget->deleteLater(); if it possible. Or process events between deleting rightWidget and adding new one.

Resources