Qt: Architectural advice needed regarding QAbstractScrollArea - qt

I'd like to design a scrollable "controls container" widget. Meaning, a scrollable view that'll be able to contain live controls (any QWidget derivative). By "live controls" i mean, if a animated QWidget derived is placed in it, i'd like to see the animation, as i scroll up and down, while the sub-control moves up and down.
Would basing such a widget on "QAbstractScrollArea" be the right way to approach it? i'd simply add controls as children? positioning them in a column? will that be enough?
EDIT:
This is the constructor code from my QAbstractScrollArea derived class. Why don't I ever see a scrollbar that can scroll the controls? (not all are visible on the same page based on the height I gave my control)
// add controls
QPushButton *a = new QPushButton(QString("a"), this);
a->setGeometry(QRect(10,10,100,30));
QPushButton *b = new QPushButton(QString("b"), this);
b->setGeometry(QRect(10,40,100,30));
QPushButton *c = new QPushButton(QString("c"), this);
c->setGeometry(QRect(10,70,100,30));
QPushButton *d = new QPushButton(QString("d"), this);
d->setGeometry(QRect(10,100,100,30));
QPushButton *e = new QPushButton(QString("e"), this);
e->setGeometry(QRect(10,130,100,30));
QPushButton *f = new QPushButton(QString("f"), this);
f->setGeometry(QRect(10,160,100,30));
QPushButton *g = new QPushButton(QString("g"), this);
g->setGeometry(QRect(10,190,100,30));
QPushButton *h = new QPushButton(QString("h"), this);
h->setGeometry(QRect(10,220,100,30));
this->addScrollBarWidget(new QScrollBar(this), Qt::AlignRight);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

Basically, that is enough. Use the concrete QScrollArea class and a generic container widget, then position your controls as children of the container.
QScrollArea scrollArea;
QWidget container;
// Create controls and add them to container.
scrollArea.setWidget( &container );

Related

Can we shift widget between the cells of a table widget

I've created a a widget in which I've placed two buttons using a layout and placed it inside a table widget's cell. The thing is that I'm changing the size of the table and I want to shift the placement of the widget without deleting them and re-initializing them from the beginning because I already assigned them actions on click (I think that the application would crash in this situation)
Code:
btn = new QPushButton[horzHeaders.size()];
btn[j].setParent(ui->tableWidget);
btn[j].setIcon(QIcon("./save.png"));
btn[j].setVisible(true);
btn_Load = new QPushButton[horzHeaders.size()];
btn_Load[j].setParent(ui->tableWidget);
btn_Load[j].setIcon(QIcon("./upload.png"));
btn_Load[j].setVisible(true);
lay = new QHBoxLayout[horzHeaders.size()];
lay[j].addWidget(&btn[j]);
lay[j].addWidget(&btn_Load[j]);
QWidget *w = new QWidget[horzHeaders.size()];
w[j].setLayout(&lay[j]);
ui->tableWidget->setCellWidget(j,vertHeaders.size() - 1, &w[j]);
You can add and remove widgets from a layout anytime you want
QWidget *widget = new QWidget();
QPushButton *button = new QPushButton();
QHBoxLayout *Hbox = new QHBoxLayout();
QVBoxLayout *Vbox= new QVBoxLayout();
Hbox->addWidget(button);
// use it till the window is resized
//and then check with an if-statement if the window is resized or not
Hbox->removeWidget(button); // remove from the button from layout
layout()->removeAt(widget); //remove the widget's current layout
Vbox->addWidget(button); // add button widget to vertical layout
widget->setLayout(Vbox); // Give it a new layout
widget->setLayout(Vbox);
If this didn't answer your question then ask in the comments.

QToolButton with text: Overwrite minimal height to minic regular button height

I am displaying QToolButtons with icon plus text (Qt::ToolButtonTextBesideIcon) outside of a tool bar. Each button has a QAction associated with it which determines the used icon and the displayed text. All those buttons are placed inside a QGridLayout. So far so good.
Unfortunately, it looks like that as soon as you add a QAction to a QToolButton, Qt automatically decides to shrink it down to the minimal size, which is not what I want given my QGridLayout. I used the following lines to correct that behavior in the horizontal dimension:
QToolButton* pButton = new QToolButton(0);
pButton->addDefaultAction(pAction);
pButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
pButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
However that still leaves my button with a smaller height than a regular (push) button. I already tried various other size policies without success.
How to best solve this issue? Is there a reliable way to determine the "normal" button height?
One idea I had was to create a regular dummy button, place it in the same layout and then read its size. This size could then be applied to my QToolButton and the dummy button would be destroyed again. Is there a more elegant / reliable way?
I do not understand what do you want to achieve.
Difference between QPushButton and QToolButton is that, QToolButton has implemented PopupMenu ( can be done easily for QPushButton also )
As I understand visual difference is only small arrow in lower right corner of QToolButton, when you use added QActions to QToolButton
This arrow is for me only difference between QToolButton and QPushButton. But maybe I am missing something.
From your examples ( QToolButton with icon + text: How to center both? )
it does not look like you want to use that popup feature. Thats why I do not understand, why to use QToolButton instead of QPushButtons.
In this example shows:
1) Same height of QToolButton and QPushButton
2) PopuMenu for QPushButton
As for me, I do not understand why to use QToolButton and try to make it look like QPushButton when it is simple to use QPushButton as QToolButton
#include <QtGui>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Prepare layout
QMainWindow *window = new QMainWindow;
QWidget *centralWidget = new QWidget(window);
QGridLayout *grid = new QGridLayout(centralWidget);
QTextEdit *textEdit = new QTextEdit();
window->setCentralWidget(centralWidget);
QAction *toolAction = new QAction(window->style()->standardIcon(QStyle::SP_MediaPlay), "ToolButton", window);
QObject::connect(toolAction, &QAction::triggered, [=]() {
qDebug() << "action";
});
QPushButton *pushButton = new QPushButton(toolAction->icon(), "PushButton1", window);
pushButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QPushButton *pushButton2 = new QPushButton(toolAction->icon(), "PushButton2", window);
pushButton2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QPushButton *pushButton3 = new QPushButton(toolAction->icon(), "PushButton2", window);
pushButton3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QObject::connect(pushButton3, &QPushButton::released, [window, pushButton3, toolAction](){
QMenu menu;
menu.addAction(toolAction);
QPoint pos = window->mapToGlobal(pushButton3->pos());
pos += QPoint(0, pushButton3->height());
menu.exec(pos);
});
QObject::connect(pushButton, SIGNAL(pressed()), toolAction, SLOT(trigger()));
QObject::connect(pushButton2, SIGNAL(pressed()), toolAction, SLOT(trigger()));
QToolButton *toolButton = new QToolButton(window);
toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolButton->setText("Popup action");
toolButton->addAction(toolAction);
toolButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QToolButton *toolButton2 = new QToolButton(window);
toolButton2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolButton2->setDefaultAction(toolAction);
toolButton2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
toolButton->setMaximumHeight(pushButton->sizeHint().height());
toolButton->setMinimumHeight(pushButton->sizeHint().height());
toolButton2->setMaximumHeight(pushButton->sizeHint().height());
toolButton2->setMinimumHeight(pushButton->sizeHint().height());
grid->addWidget(textEdit ,0,0,1,2);
grid->addWidget(toolButton ,1,0,1,1);
grid->addWidget(pushButton ,1,1,1,1);
grid->addWidget(toolButton2 ,2,0,1,1);
grid->addWidget(pushButton2 ,2,1,1,1);
grid->addWidget(pushButton3 ,3,0,1,2);
window->show();
return a.exec();
}

Reversing the layout of a QToolButton

I would like to make a QToolButton where the text is the left of its icon. I've tried to find related information, and tried things with:
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
but this sets the text to the right side of the button. I've looked into trying stylesheets, but text-alignment is only for push buttons. Is there any way to do this?
My highest-level goal is to make an interface bar which looks like this:
with a text label and image right beside it. Currently I'm using a toolbar with checkable toolbuttons because of the style (no border unless moused-over, still has the indent with checkable, contains text and an icon...). It's entirely possible that I'm using the wrong type widgets, so if I can't change the layout of this, is there any way to emulate this style?
This is what I currently have:
.
You can use QWidget::setLayoutDirection:
toolbar->setLayoutDirection(Qt::RightToLeft);
Your best bet is probably to create this functionality yourself in a QWidget. It should have a QLabel and a QToolButton (or QPushButton) arranged in a QHBoxLayout. Then you can add this custom widget to the toolbar using QToolBar::addWidget() or QToolBar::insertWidget().
Here's an example of a widget you could use:
#include <QtGui>
class CoolButton : public QWidget
{
public:
CoolButton(QWidget *parent = 0) : QWidget(parent)
{
QLabel *label = new QLabel("Hi!", this);
QToolButton *button = new QToolButton(this);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(label);
layout->addWidget(button);
setLayout(layout);
}
};

How to Add QListView/QListWidget to QGraphicsScene in Qt?

How to Add QListView/QListWidget to QGraphicsScene and add Widgets to ListView
When i Try to add QLisView to QGraphicsScene mouse scroll affects goes from Scene.
I want to add QPushButtons as ListView Items in QgraphicsScene with mouse scroll affect.
Thanks.
What about QGraphicsProxyWidget?
QListView *listView = new QListView;
QGraphicsProxyWidget *proxy = scene.addWidget(listView);
Then (or before that) you can populate the list with anything you want. QPushButton can be added to the list using setIndexWidget(). Also you might rethink the whole idea of having a QListView, and give it a try with QScrollArea and a linear layout containing buttons. That would require a bit more logic to organize items within the scroll area, but it should be more lightweight that QListView with custom widgets.
I second the answer above: ProxyWidget is the answer.
Here is my working code,
Header:
class CenterScreen{
private:
QListWidget* nameListWidget;
QGraphicsProxyWidget* nameProxyWidget;
...
C++ source:
void CenterScreen::addListView()
{
QGraphicsScene* scene = ui.centerGraphicsView->scene();
nameListWidget = new QListWidget();
nameProxyWidget = scene->addWidget(nameListWidget);
...
nameProxyWidget->hide(); // you can control your widget as you like

QScrollArea not respecting contentMargins setting

QScrollArea, for some reason, is ignoring the contentMargins setting when I set QGraphicsView as its widget. Looking at the snippet below, can someone please tell if I'm doing something wrong or it could be a bug in the SDK?
Snippet 1 (works perfect):
QWidget *appWindow = new QWidget;
QScrollArea *sa = new QScrollArea(appWindow);
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setContentMargins(50, 50, 50, 50);
QWidget *widgetToScroll = new QWidget(sa);
widgetToScroll->resize(5000, 5000);
sa->setWidget(widgetToScroll);
QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow);
appWindowLayout->addWidget(sa);
appWindow->setLayout(appWindowLayout);
appWindow->show();
Snippet 2 (It's like setContentMargins() call is ignored completely):
QWidget *appWindow = new QWidget;
QScrollArea *sa = new QScrollArea(appWindow);
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setContentMargins(50, 50, 50, 50);
QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(sa), sa);
widgetToScroll->setAlignment(Qt::AlignLeft | Qt::AlignTop);
widgetToScroll->resize(5000, 5000);
sa->setWidget(widgetToScroll);
QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow);
appWindowLayout->addWidget(sa);
appWindow->setLayout(appWindowLayout);
appWindow->show();
Thanks.
To make the content margins work properly for a QScrollArea widget I subclass it and manually set the viewport margins (which is a protected method in QT 4.7)
// Extended class
class QScrollAreaWithMargins : public QScrollArea
{
public:
virtual void resizeEvent(QResizeEvent *event) override
{
// Define content margins here
setViewportMargins(5, 0, 0, 0); // <<<<< SET MARGINS HERE
QScrollArea::resizeEvent(event);
}
};
// Usage
//...
mEditorScrollArea = new QScrollAreaWithMargins();
//...
It looks like you are confusing the structure of how to nest a QGraphicsView and a QGraphicsScene. (Maybe it was just a typo?)
QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(sa), sa);
should be changed to
QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(), sa);
or
QGraphicsView *widgetToScroll = new QGraphicsView();
sa->setWidget(widgetToScroll);
When you add a QWidget to a layout, you change the widget's parent. When you set a widget (or QGraphicsView) to a QScrollArea, you change that widget's parent. See Object Trees & Ownership for more information. So if you wanted to set up your QGraphicsView inside a QScrollArea your code would look like this:
QWidget *appWindow = new QWidget;
QScrollArea *sa = new QScrollArea(); // No need to specify a parent here if
// you add it to a layout later
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setContentsMargins(50, 50, 50, 50);
QGraphicsView *widgetToScroll = new QGraphicsView();
widgetToScroll->setAlignment(Qt::AlignLeft | Qt::AlignTop);
widgetToScroll->resize(5000, 5000);
sa->setWidget(widgetToScroll); // This sets the parent for widgetToScroll
QVBoxLayout *appWindowLayout = new QVBoxLayout();
appWindowLayout->addWidget(sa); // This sets the parent for sa
appWindow->setLayout(appWindowLayout); // This sets the parent for appWindowLayout
appWindow->show();
As a side note...
When using QGraphicsViews with a QGraphicsScene, instead of setting the margins using a QScrollArea's setContentsMargins, I use the QGraphicsView automatic scrolling and just set the scene rect to have a larger margin that the size of my content like so:
QWidget *appWindow = new QWidget;
QGraphicsView *widgetToScroll = new QGraphicsView();
QGraphicsScene *scene = new QGraphicsScene();
scene->addRect(0,0, 5000, 5000);
widgetToScroll->setSceneRect(-50,-50, 5050, 5050);
widgetToScroll->setScene(scene);
QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow);
appWindowLayout->addWidget(widgetToScroll);
appWindow->setLayout(appWindowLayout);
appWindow->show();
The QGraphicsView includes quite a bit more than just automatic scrolling when needed. You can resize everything inside of it and quite a bit more. It is great for 2D layouts, interactions and animations. See Qt's Graphics View Framework at http://doc.qt.io/qt-5/graphicsview.html for more information.
Here is more information that may be useful when using margins and paddings: The Box Model used by QStyleSheets.

Resources