Fixed sized scroll area with layout. - qt

What I want to do is to show new QWidget, which is set in QScrollArea.
The Grid Layout is set in Scroll Area and than VBox Layout is added to the Grid one.
Program allows to add dynamically Labels in this VBox Layout.
I want scroll area has fixed size and when area taken by Labels exceeds scroll area's size (heigth), than scrollbar should appear (and no changes in Scroll Area size).
With my code - adding Labels causes increasing heigth of QWidget (window).
Scrollbar can be seen at the beginning but at some point it disappears... like QScrollArea is no more in there...
QWidget* playlistWindow = new QWidget();
playlistWindow->setAcceptDrops(true);
playlistWindow->resize(200, 300);
QScrollArea* scrollArea = new QScrollArea();
scrollArea->setWidget(playlistWindow);
QVBoxLayout* layout = new QVBoxLayout();
QGridLayout* gridLayout = new QGridLayout();
layout->setAlignment(Qt::AlignTop);
scrollArea->setLayout(gridLayout);
gridLayout->addLayout(layout, 0, 0);
scrollArea->setMaximumHeight(300);
scrollArea->show();

Related

How to calculate QTextEdit height accurately by its contents to get rid of vertical scrollbars?

I need a QTextEdit widget which height grows or shrinks to the size of the inner content. I didn't find built-in solution and decided to make my own with connections to &QTextEdit::textChanged signals that will update size of the widget at runtime. In the code snippet I flattened my custom component to raw QTextEdit for simplicity. The problem is that calculation via QFontMetrics is inaccurate, a vertical scrollbar appears with only some pixels to scroll
I want to get rid of any vertical scrollbars in these text widgets.
However, I don't need to get rid of horizontal scrollbars and here is the second problem arises. Seemingly, horizontal scrolls occupy space in the viewport of the text widget and its internal height shrinks by the height of the scrollbar itself
void TestWindow::setupUi(QWidget *parent)
{
QVBoxLayout *v_box0 = new QVBoxLayout(parent);
v_box0->setContentsMargins(0, 0, 0, 0);
QVBoxLayout *v_scroll_layout = new QVBoxLayout();
v_scroll_layout->setContentsMargins(0, 0, 0, 0);
v_scroll_layout->setSpacing(0);
v_scroll_layout->setAlignment(::Qt::AlignTop);
QWidget *scroll_content = new QWidget();
scroll_content->setLayout(v_scroll_layout);
QScrollArea *v_scroll = new QScrollArea();
v_scroll->setWidgetResizable(true);
v_scroll->setWidget(scroll_content);
v_box0->addWidget(v_scroll);
QSize size;
QTextEdit *edit0 = new QTextEdit();
edit0->setLineWrapMode(QTextEdit::NoWrap);
edit0->setText("Hello World 1\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11");
edit0->setFont(viewer_font);
size = edit0->fontMetrics().size(0, edit0->document()->toPlainText());
edit0->document()->setDocumentMargin(0);
edit0->setMinimumHeight(size.height());
QTextEdit *edit1 = new QTextEdit();
edit1->setLineWrapMode(QTextEdit::NoWrap);
edit1->setText("New item");
edit1->setFont(viewer_font);
size = edit1->fontMetrics().size(0, edit1->document()->toPlainText());
edit1->document()->setDocumentMargin(0);
edit1->setMinimumHeight(size.height());
v_scroll_layout->addWidget(edit0);
v_scroll_layout->addWidget(edit1);
setFixedHeight(300);
}

How to set a center aligned QLabel to toolbar in Qt

I'm new to qt and exploring it .I want to have a text which is center aligned in my Mainwindow's toolbar.Below is my code inside my MainWindow constructor:
QLabel* label=new QLabel("Hello World");
label->setAlignment(Qt::AlignHCenter);
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(label);
QWidget* wid = new QWidget;
wid->setLayout(layout);
ui->mainToolBar->addWidget(wid);
The above code displays the text , but not at the center.It displays at the left.What am I missing?Any help will be really helpful.
label->setAlignment(Qt::AlignHCenter);
This tells the label to (horizontally) center the text in itself.
layout->addWidget(label);
This is expanded by default argument to
layout->addWidget(label, 0);
Where the 0 is the stretch factor of the label within this layout. Zero means your label will be given as much space as it needs to display properly but nothing more. So your label is just as big as your text needs, has it's text centered, but since it's on a QHBoxLayout it's shown on the left side within your bar. If there are no other widgets in your bar's layout, you can set the stretch-factor to 1 to make the label fill the layout, your text will then show in the center.
layout->addWidget(label, 1);

QMainWindow : Set widgets size with respect to screen size

I have a Qt class which inherits from QMainWindow. The constructor of the class creates two widgets which are added to a horizontal layout object as follows:
MyWindow::MyWindow()
{
resize(QDesktopWidget().availableGeometry(this).size());
display = new MyWidget(this);
display->setSizePolicy(QSizePolicy::Expanding,
QSizePolicy::Expanding);
current = new MyWidget(this);
current->setSizePolicy(QSizePolicy::Expanding,
QSizePolicy::Expanding);
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(display);
layout->addWidget(current);
QFrame* frame = new QFrame();
frame->setFrameShape(QFrame::StyledPanel);
frame->setLayout(layout);
setCentralWidget(frame);
show();
}
This currently shows the widget side of side of each other. However, what I would like to do is have one of the widgets take 30% of the horizontal space while the other one occupies the other 70%. I would also like the widgets to expand or contract if one resizes the main window but keeping these ratios.
When you place a widget into a layout you can specify its stretch factor:
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(display, 3);
layout->addWidget(current, 7);
http://doc.qt.io/qt-5/qboxlayout.html#addWidget

Create Qt layout with fixed height

I want to create a Qt window that contains two layouts, one fixed height that contains a list of buttons at the top and one that fills the remaning space with a layout that centers a widget vertically and horizontally as per the image below.
How should i be laying out my layouts/widgets. Ive tried a few options with nested horizontal and vertical layouts to no avail
Try making the pink box a QWidget with a QHBoxLayout (rather than just making it a layout). The reason is that QLayouts don't provide functionality to make fixed sizes, but QWidgets do.
// first create the four widgets at the top left,
// and use QWidget::setFixedWidth() on each of them.
// then set up the top widget (composed of the four smaller widgets):
QWidget *topWidget = new QWidget;
QHBoxLayout *topWidgetLayout = new QHBoxLayout(topWidget);
topWidgetLayout->addWidget(widget1);
topWidgetLayout->addWidget(widget2);
topWidgetLayout->addWidget(widget3);
topWidgetLayout->addWidget(widget4);
topWidgetLayout->addStretch(1); // add the stretch
topWidget->setFixedHeight(50);
// now put the bottom (centered) widget into its own QHBoxLayout
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addStretch(1);
hLayout->addWidget(bottomWidget);
hLayout->addStretch(1);
bottomWidget->setFixedSize(QSize(50, 50));
// now use a QVBoxLayout to lay everything out
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(topWidget);
mainLayout->addStretch(1);
mainLayout->addLayout(hLayout);
mainLayout->addStretch(1);
If you really want to have two separate layouts--one for the pink box and one for the blue box--the idea is basically the same except you'd make the blue box into its own QVBoxLayout, and then use:
mainLayout->addWidget(topWidget);
mainLayout->addLayout(bottomLayout);

Two QTableViews in QLayout

I want to expand QTableViews' size so each QTableView takes the half size of the window! , how can i do it?
screenshot:
http://i.stack.imgur.com/Rh87o.jpg
Add a QHBoxLayout to your widget/window and then add the tables to it.
The following code on a top level widget..
QHBoxLayout *horizontalLayout;
QTableView *tableView;
QTableView *tableView_2;
horizontalLayout = new QHBoxLayout(Widget);
tableView = new QTableView(Widget);
horizontalLayout->addWidget(tableView);
tableView_2 = new QTableView(Widget);
horizontalLayout->addWidget(tableView_2);
Will give you something like..
The two tables share the available space equally and expand and resize with the main window.

Resources