I have some QDockWidgets (not floating, only closable) inside a single QWidget.
I have some widgets inside each QDockWidget - their heights should be equal.
These inner widgets can be hidden through the context menu.
My inner widgets should have equal height. I done it this way:
void MyDocksPanel::redistributeSpace()
{
QBoxLayout * lay = (QBoxLayout *)layout();
for (int i = 0; i < lay->count(); i++)
{
QWidget * dock = lay->itemAt(i)->widget();
if (dock == NULL)
continue;
int size = 0;
foreach(QWidget * subWidget, dock->findChildren<QWidget*>())
size += subWidget->isVisible() ? 1 : 0;
if (dock->isVisible() && (size == 0))
dock->hide();
lay->setStretch(i, size);
}
}
All works fine until I add some const elements to each QDockWidget: some horizontal scrollbars and some Labels... Now my inner widgets have different sizes. But it is necessary for me to set their heights strongly equal.
QLayout lays out widget sizes on one level of a widget's hierarchy. How can I make height-equal subwidgets?
3 subwidgets vs 2 subwidgets
My first strategy to set stretches 3 and 2:
But, when i have added scroll bars:
Heights of my 5 widgets are equals to 37,37,37,28,28 ... and thats the problem
You're on the right track with the stretch factors, but think in terms of pixel values rather than small proportions. Try setting the stretch factor of each dock widget to this:
dockWidgetStretch = numChildWidgets * childWidgetMinimumHeight + scrollBarHeight;
where childWidgetMinimumHeight and scrollBarHeight are both expressed in pixels, and are both constants.
EDIT: Here is a working example. You might have to experiment a bit to get it to work with your program, but this should be a good start.
header.h
#include <QtGui>
class WidgetWith3Children : public QWidget
{
public:
WidgetWith3Children()
{
QTextEdit *edit1 = new QTextEdit;
QTextEdit *edit2 = new QTextEdit;
QTextEdit *edit3 = new QTextEdit;
QScrollBar *scrollBar = new QScrollBar(Qt::Horizontal);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(edit1);
layout->addWidget(edit2);
layout->addWidget(edit3);
layout->addWidget(scrollBar);
setLayout(layout);
}
};
class WidgetWith2Children : public QWidget
{
public:
WidgetWith2Children()
{
QTextEdit *edit1 = new QTextEdit;
QTextEdit *edit2 = new QTextEdit;
QScrollBar *scrollBar = new QScrollBar(Qt::Horizontal);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(edit1);
layout->addWidget(edit2);
layout->addWidget(scrollBar);
setLayout(layout);
}
};
class OuterWidget : public QWidget
{
public:
OuterWidget()
{
QDockWidget *dockWidget1 = new QDockWidget;
QDockWidget *dockWidget2 = new QDockWidget;
dockWidget1->setWidget(new WidgetWith3Children);
dockWidget2->setWidget(new WidgetWith2Children);
QVBoxLayout *layout = new QVBoxLayout;
// 71 is the height of the minimum size hint for QTextEdit
// 30 is the height of a horizontal scrollbar (on my system)
layout->addWidget(dockWidget1, 71 * 3 + 30);
layout->addWidget(dockWidget2, 71 * 2 + 30);
layout->setMargin(0);
setLayout(layout);
}
};
main.cpp
#include <QtGui/QApplication>
#include "header.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
OuterWidget w;
w.show();
return a.exec();
}
Just to check I understand: you have a QDockWidget which contains multiple children, as well as a label and a horizontal scrollbar. The label and scrollbar should be of fixed height, and the remaining vertical space should be divided between the child widgets.
If that's correct, all you need to do is add a QVBoxLayout to each QDockWidget. Add your widgets as I've done below:
QDockWidget DockWidget;
QVBoxLayout Layout = new QVBoxLayout(DockWidget);
FixedHeightWidget.setFixedHeight(10)
Layout.addWidget(FixedHeightWidget, 0);
Layout.addWidget(FirstVariableHeightWidget, 1);
Layout.addWidget(SecondVariableHeightWidget, 1);
Layout.addWidget(ThirdVariableHeightWidget, 1);
If you were to hide any of the widgets you've added to the layout, the layout will handle resizing the remaining visible children.
Related
I'm trying to create a QScrollArea in a QTabWidget.
Versions :
Qt 5.15.0
Qt creator 4.12.4
MSVC2019 64 bits
First of all, I've created the QTabWidget :
tabWidget = new QTabWidget(this);
tabWidget->setGeometry(10, 15, 1200, 665);
tabWidget->setStyleSheet("font-size : 15px");
tab1Content = new QWidget(tabWidget); tabWidget->addTab(tab1Content, "tab1");
tab2Content = new QWidget(tabWidget); tabWidget->addTab(tab2Content, "tab2");
tab3Content = new QWidget(tabWidget); tabWidget->addTab(tab3Content, "tab3");
tab4Content = new QWidget(tabWidget); tabWidget->addTab(tab4Content, "tab4");
I can add
tabWidget->setEnable(true);
And for all tabs, 0 <= i < tabWidget.count
tabWidget->setTabEnabled(i, true);
Click to change tab doesn't work : https://i.stack.imgur.com/8r1Jg.png
Strange thing : color looks like enabled but i can only change tabs with ← → and when i lost tabWidget focus by clicking on an other thing outside the tabWidget, i can't regain focus.
So i've created temporary button to change tabs and linked to tabWidget like that :
connect(changeTab, &QPushButton::clicked, [&]() {onChangeTab();});
void MainWindow::onChangeTab() {
tabWidget->setCurrentIndex(tabWidget->currentIndex() >= tabWidget->count() - 1 ? 0 : tabWidget->currentIndex() + 1);
}
It works well.
Thus, I've start to create the QScrollArea :
First, it doesn't work, so I've tried to found sth on internet :
QScrollArea not working as expected with QWidget and QVBoxLayout
My result : https://i.stack.imgur.com/jvVol.png
I cannot click on a single button and i can't scroll...
And if i try to force scroll like this, it doesn't scroll
scrollArea->scroll(0, 50);
Last thing, there isn't infinite loop or dead lock things because all things around this cursed tabWidget and scroll Area work perfectly.
I don't know why these objects "don't answer" if somedoby had this kind of experiment could you help me please ?
Thank you very much in advance.
try this code
#include "widget.h"
#include<QTabWidget>
#include<QLabel>
#include<QVBoxLayout>
#include<QScrollArea>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget->setGeometry(10, 15, 1200, 665);
tabWidget->setStyleSheet("font-size : 15px");
QWidget * tab1Content = new QWidget;
//preparing tab1content ( e.g.)
QVBoxLayout * verticalLayout = new QVBoxLayout;
// adding items to vertical layout
for(int i=0;i<100;i++)
verticalLayout->addWidget(new QLabel(QString::number(i)));
// set this vertical layout inside tab1content
tab1Content->setLayout(verticalLayout);
// create new scroll area ...
QScrollArea * scroll = new QScrollArea;
// ... and add tab1content in scroll area
scroll->setWidget(tab1Content);
// and finally add scroll area inside tabwidget
tabWidget->addTab(scroll,"tab1");
QWidget * tab2Content = new QWidget; tabWidget->addTab(tab2Content, "tab2");
QWidget * tab3Content = new QWidget; tabWidget->addTab(tab3Content, "tab3");
QWidget * tab4Content = new QWidget; tabWidget->addTab(tab4Content, "tab4");
}
Widget::~Widget()
{
}
I have a widget, I am using QVBoxLayout with the widget to organise rows of widgets. The first widget is a QLabel followed by multiple QPushButton widgets.
The width of each widget occupies 100% of the width, however the height of the first widget (QLabel) is much larger than the QPushButton widgets that follow it. All the QPushButtons are the same height.
It looks like the first row has been increased in size to pad the layout to fill the parent. If this is the case, is there any way to instruct the layout to use only the height required by the internal widgets and not to pad?
The difference between the behavior of the layout for the QLabel and QPushButton is the sizePolicy, so the solution is to set the policy QSizePolicy::Maximum in the vertical part
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QVBoxLayout *lay = new QVBoxLayout(&w);
QLabel *label = new QLabel("Stack Overflow");
label->setStyleSheet("background-color:salmon;");
QSizePolicy sp = label->sizePolicy();
sp.setVerticalPolicy(QSizePolicy::Maximum);
label->setSizePolicy(sp);
lay->addWidget(label);
for (int i=0; i<4; i++) {
lay->addWidget(new QPushButton(QString("pushbutton-%1").arg(i)));
}
w.show();
return a.exec();
}
Im new to Qt programming and I want to add a scrollbar to a widget which is having child widgets within it.I have seen several questions/posts about this like:
1.How to add a scrollbar to parent QWidget
2.Insert a scrollbar in a qt widget using qtcreator
3.Adding scroll bar to a Qwidget
4.QScrollArea missing Scrollbar
But most of the answers set a layout to the widget for which we add the scrollbar.
My Problem:
The widget for which I need scrollbar has many child widgets within it.But I haven't added any layout to it.The geometry of the child widgets are modifiable and so I haven't added any layout to the parent widget.
Below is my code:
class Absolute : public QWidget {
public:
Absolute(QWidget *parent = 0);
};
Absolute::Absolute(QWidget *parent)
: QWidget(parent) {
QTextEdit *ledit = new QTextEdit(this);
ledit->setGeometry(5, 5, 500, 550);
QTextEdit *lledit = new QTextEdit(this);
lledit->setGeometry(510, 5, 250, 550);
/*QScrollArea* sa = new QScrollArea();
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
auto *widget = new QWidget(this);
sa->setWidget(widget);
auto *l = new QVBoxLayout(this);
l->setMargin(0);
l->addWidget(sa);*/
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Absolute window;
window.setWindowTitle("Absolute");
window.setGeometry(500,500,1500,1000);
window.show();
return app.exec();
}
However without the scrollbar code(commented portion),the UI has those textedits in the given position as set in the setGeometry.
All I need is to bring a scrollbar if the 2nd textedits width is more.
So I tried adding the scrollbar(the commented portion).However I can see only the scrollbar and not the textedits.
Any suggestion/inputs will be really helpful.Thanks in advance!!
Cause
The way you set the parents when you create the widgets and layouts is not correct.
Solution
Create the correct parent/child hierarchy and set the desired size of the QScrollArea's widget. There is no need to set a layout to this widget.
Example
Here is an example I have prepared for you in order to demonstrate how you could fix Absolute:
class Absolute : public QWidget {
public:
Absolute::Absolute(QWidget *parent = nullptr)
: QWidget(parent)
{
auto *sa = new QScrollArea(this);
auto *l = new QVBoxLayout(this);
auto *widget = new QWidget();
auto *ledit = new QTextEdit(widget);
auto *lledit = new QTextEdit(widget);
sa->setWidgetResizable(true);
sa->setWidget(widget);
sa->setAlignment(Qt::AlignLeft | Qt::AlignTop);
ledit->setGeometry(5, 5, 500, 550);
lledit->setGeometry(510, 5, 250, 550);
widget->setFixedSize(lledit->geometry().right(), lledit->geometry().bottom());
l->setMargin(0);
l->addWidget(sa);
}
};
Note: For demonstration purposes the size of widget is set to (lledit->geometry().right(), lledit->geometry().bottom()). You might consider adjusting it according to your specific needs.
I need to scale a QtableWidget (the behaviour should be like a zoom).
But when I reimplement the paintEvent of QtableWidget and set the scale manually, like this:
void MyTableWidget::paintEvent ( QPaintEvent * event )
{
QTableWidget::paintEvent(event);
QPainter p(viewport());
p.scale(m_scale,m_scale);
p.drawRect( 0, 0, width()-1, height()-1);
}
only the border is rescaled :
And I don't see any paintEvent on QTableWidgetItem, so how can I rescale all my table ?
Thank you in advance and have a good day.
EDIT ----------------------------------------
The behaviour may seems strange so here is some explanations:
This QT window is a child of an acrobat reader window. When I unzoom the PDF on acrobat, I resize my QT window to keep the same proportions, but I would like to scale the content of the window.
example: If I unzoom my PDF, I decrease the size of my QT window to keep the same proportions, and I want to scale the content of my QT window to this new size (decrease the display size, like an unzoom). Is it clear ? :o
But for instance the view donesn't fit the window, I have this:
And I want this:
And when I zoom on my PDF, I increase the window size and scale up the content, like this:
Thank you very much for your help and your time.
Use QGraphicsScene with your QTableWidget instead, but it is not very difficult:
QGraphicsScene *scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QTableWidget *wgt = new QTableWidget;
wgt->setColumnCount(10);
wgt->setRowCount(10);
for (int ridx = 0 ; ridx < wgt->rowCount() ; ridx++ )
{
for (int cidx = 0 ; cidx < wgt->columnCount() ; cidx++)
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(QString("%1").arg(ridx));
wgt->setItem(ridx,cidx,item);
}
}
QGraphicsProxyWidget *pr = scene->addWidget( wgt );
pr->moveBy(10,10);
Scale view with:
ui->graphicsView->scale(2,2);
Better way for zooming is zoom in out by wheel. Subclass view or use eventFilter. For example:
Header:
#ifndef MYQGRAPHICSVIEW_H
#define MYQGRAPHICSVIEW_H
#include <QGraphicsView>
class MyQGraphicsView : public QGraphicsView
{
Q_OBJECT
public:
explicit MyQGraphicsView(QWidget *parent = 0);
signals:
protected:
void wheelEvent(QWheelEvent* event);
};
#endif // MYQGRAPHICSVIEW_H
Cpp:
#include "myqgraphicsview.h"
#include <QPointF>
MyQGraphicsView::MyQGraphicsView(QWidget *parent) :
QGraphicsView(parent)
{
}
void MyQGraphicsView::wheelEvent(QWheelEvent* event) {
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
// Scale the view / do the zoom
double scaleFactor = 1.15;
if(event->delta() > 0) {
// Zoom in
scale(scaleFactor, scaleFactor);
} else {
// Zooming out
scale(1.0 / scaleFactor, 1.0 / scaleFactor);
}
// Don't call superclass handler here
// as wheel is normally used for moving scrollbars
}
Usage:
MyQGraphicsView *view = new MyQGraphicsView;
view->setScene(scene);//same scene
view->show();
Result as you want:
Note that user still able to edit data etc, functionality didn't change.
Additional example, like in the Qt books.
(same MyQGraphicsView class)
#include "myqgraphicsview.h"
#include <QGraphicsProxyWidget>//and other needed includes
//just to show that signals and slots works
//with widget which placed in graphicsview
void print(int row, int column)
{
qDebug() << row+1 << column+1;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *widget = new QWidget;//container
QVBoxLayout *lay = new QVBoxLayout;
MyQGraphicsView * view = new MyQGraphicsView;//view as part of UI
QGraphicsScene * scene = new QGraphicsScene;
QTableWidget *wgt = new QTableWidget;//table which will be added to graphicsView
QObject::connect(wgt,&QTableWidget::cellPressed,print);//connection using Qt5 style(and power)
wgt->setColumnCount(10);
wgt->setRowCount(10);
for (int ridx = 0 ; ridx < wgt->rowCount() ; ridx++ )
{
for (int cidx = 0 ; cidx < wgt->columnCount() ; cidx++)
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(QString("%1").arg(ridx));
wgt->setItem(ridx,cidx,item);
}
}
QPushButton *butt = new QPushButton("click");
lay->addWidget(view);
lay->addWidget(butt);
widget->setLayout(lay);
QGraphicsProxyWidget *pr = scene->addWidget( wgt );
pr->moveBy(10,10);
view->setScene(scene);
widget->show();
return a.exec();
}
Result:
As you can see, user can scale table, edit data and signals and slots works. All fine.
I'm trying to put several QPushButton entities inside a QVBoxLayout such that they are centered and expanding. The expanding tag works fine until I tell the QVBoxLayout to use AlignHCenter, after which the QPushButton's all jump to the minimum size and stay there. What am I doing wrong?
QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
setLayout(vBoxLayout);
//Create title and add to layout
QLabel *titleLabel = new QLabel(this);
titleLabel->setText(menuTitle);
titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
titleLabel->setMaximumHeight(35);
titleLabel->setStyleSheet(QString("QLabel { font-size: 16pt; }"));
vBoxLayout->addWidget(titleLabel);
vBoxLayout->setStretchFactor(titleLabel, 1);
//Create buttons and add to layout
QMap<int, QString>::const_iterator it;
for (it = m_buttonMapping.cbegin(); it != m_buttonMapping.cend(); ++it)
{
QPushButton *button = new QPushButton(it.value(), this);
connect(button, SIGNAL(clicked()), sigMapper, SLOT(map()));
sigMapper->setMapping(button, it.key());
button->setMinimumHeight(40);
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
button->setMaximumWidth(800);
button->setMinimumWidth(300);
vBoxLayout->addWidget(button);
vBoxLayout->setAlignment(button, Qt::AlignHCenter); //<-- without this, expanding works fine!
vBoxLayout->setStretchFactor(button, 1);
}
vBoxLayout->setContentsMargins(10, 0, 10, 0);
By specifying the alignment on the layout, you keep your QPushButtons from being able to expand. Available new space will be used to keep the QPushButtons centered, instead of allowing them to resize and for an amount of space around them to be utilized for centering. Stretch factors fulfill your requirement for a proportional resizing and centering of a layout's contents.
To get around this, create a wrapper widget and layout (or just a layout), and add the widget that is laid out by your vBoxLayout to the wrapper layout with a stretch factor applied. Before and after adding your widget, you'll add QSpacerItems to the wrapper layout with QHBoxLayout::addStretch. You can then adjust the stretch factors of your widget and the spacers to get the effect you want.
Here's some sample code that should solve your problem:
MainWindow.cpp
#include "MainWindow.hpp"
#include <QPushButton>
#include <QLabel>
#include <QBoxLayout>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent) {
QWidget* centralWidget = new QWidget(this);
QVBoxLayout* layout = new QVBoxLayout(centralWidget);
// Create a wrapper widget that will align horizontally
QWidget* alignHorizontalWrapper = new QWidget(centralWidget);
layout->addWidget(alignHorizontalWrapper);
// Layout for wrapper widget
QHBoxLayout* wrapperLayout = new QHBoxLayout(alignHorizontalWrapper);
// Set its contents margins to 0 so it won't interfere with your layout
wrapperLayout->setContentsMargins(0, 0, 0, 0);
wrapperLayout->addStretch(1);
QWidget* widget = new QWidget(alignHorizontalWrapper);
wrapperLayout->addWidget(widget, 3);
wrapperLayout->addStretch(1);
QVBoxLayout* vBoxLayout = new QVBoxLayout(widget);
QLabel* titleLabel = new QLabel(this);
titleLabel->setText(QStringLiteral("Menu"));
titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
titleLabel->setMaximumHeight(35);
titleLabel->setStyleSheet(QStringLiteral("QLabel { font-size: 16pt; }"));
vBoxLayout->addWidget(titleLabel);
vBoxLayout->setStretchFactor(titleLabel, 1);
for (int i = 0; i < 3; ++i) {
const QString& value = QStringLiteral("Button ") + QString::number(i);
QPushButton* button = new QPushButton(value, this);
button->setMinimumHeight(40);
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
//button->setMaximumWidth(800);
button->setMinimumWidth(300);
vBoxLayout->addWidget(button);
//vBoxLayout->setAlignment(button, Qt::AlignHCenter); // without this, expanding works fine!
vBoxLayout->setStretchFactor(button, 3);
}
vBoxLayout->setContentsMargins(10, 0, 10, 0);
this->setCentralWidget(centralWidget);
}
MainWindow.hpp
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QMainWindow>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr);
};
#endif // MAINWINDOW_HPP
main.cpp
#include "MainWindow.hpp"
#include <QApplication>
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}