Widget not shown in QMainWindow - c++

Good evening,
the aim is to have a mainwindow (created without Designer but by coding) with three sections next to each other:
a list of data points (vector)
statistics about the data points
a graphical summary (histogram)of the data
I have started to create my own widget do draw a diagram (just a line for test purposes so far).
However, while the tableview widget is shown, the "diagram" widget (instance of class histogram) is not shown (should be a third vertical column).
A very similar problem in Stackoverflow gave me some direction. But while it helped me to display the widget with the table, I did not figure out how to show my custom widget.
post: widgets not shown in qt main window
I have also checked literature (Summerfield and Qt4 Hui Entwicklung mit C++ by Jürgen Wolff), but they only have examples with only one central widget.
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
MainWindow::MainWindow(QMainWindow *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags)
{
mainWidget = new QWidget(this);
setCentralWidget(mainWidget);
tableWidget = new QTableWidget(mainWidget); // QTableWidget to display the data vector
//...
// here comes code to fill the table...
//...
// result labels
lbl_sampleSize = new QLabel("sample size");
lbl_meanValue = new QLabel("mean");
lbl_sigma = new QLabel("sigma");
lbl_andersonDarling = new QLabel("Anderson Darling");
lbl_pValue = new QLabel("p-value for Anderson-Darling");
rightLayout = new QVBoxLayout(); // a vertical layout to contain labels
rightLayout->addWidget(lbl_sampleSize);
rightLayout->addWidget(lbl_meanValue);
rightLayout->addWidget(lbl_sigma);
rightLayout->addWidget(lbl_andersonDarling);
rightLayout->addWidget(lbl_pValue);
rightLayout->addStretch();
//diagram
diagram = new Histogram(mainWidget);
mainLayout = new QHBoxLayout(mainWidget);
mainLayout->addWidget(tableWidget,0);
mainLayout->addLayout(rightLayout,0);
mainLayout->addWidget(diagram, 0);
//mainWidget->setLayout(mainLayout);
}
screenshot:
remark:
with this code
//diagram
diagram = new Histogram();
mainLayout = new QHBoxLayout(mainWidget);
mainLayout->addWidget(tableWidget,0);
mainLayout->addLayout(rightLayout,0);
//mainLayout->addWidget(diagram, 0);
//mainWidget->setLayout(mainLayout);
diagram->show();
I was able to create a separate Widget with the test diagram.
(removed the parent information and called diagram->show())

Just set the minimum size (QWidget::setMinimumSize()):
diagram->setMinimumSize(100, 100);
More control over the widget sizing can be achieved through its size policy.

Related

How can I stretch a QLayout or QFrame, nested within a bigger layout?

I currently have this code set up to create a sidebar and I'm not entirely sure how to stretch it so that the left, top, and bottom sides touch the edge of the window.
QFrame *sidebar = new QFrame;
QLabel *sideItemA = new QLabel("Item A");
QLabel *sideItemB = new QLabel("Item B");
QVBoxLayout *sidebarLayout = new QVBoxLayout;
sidebarLayout->addWidget(sideItemA);
sidebarLayout->addWidget(sideItemB);
sidebarLayout->addStretch();
sidebar->setLayout(sidebarLayout);
sidebar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
sidebar->setStyleSheet("background-color:#FFFFFF");
sidebar->setMinimumWidth(150);
mainLayout->addWidget(sidebar);
Here is a screenshot of what the above code looks like:
I've tried doing this using nested layouts too and I get the same result. Any pointers? Is this even the best way to do it?

How can I align Widgets left horizontally using Qt Creator's Designer

I want to create new QPushButtons and add them to my horizontal layout by pressing a "create Button"-button. I also want them to align left, so every new button should be right after the last added with a little spacing in between.
But here is what I get when I start my application and add create three buttons
First of all, I don't like my "create new Button"-Button to be centric. When I create one Button, both of them align left. But when I click a second time and a third time, the buttons are created with large space in between. I tried using spacer, but they only helped with the alignment problem of the createButton. Is there no simple way to just add buttons one button after the other like a horizontal stack?
This is my code i am using to generate buttons:
QPushButton *newCategory = new QPushButton(ui->category);
newCategory->setGeometry(0,0,140,60);
newCategory->setMinimumSize(140,60);
newCategory->setMaximumSize(140,60);
newCategory->setText("Test");
ui->horizontalLayout->addWidget(newCategory,0,Qt::AlignLeft);
You should use QBoxLayout::addStretch to push your buttons to the left.
An example:
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
btnLayout = new QHBoxLayout(this);
QPushButton *createBtn = new QPushButton("Create button");
btnLayout->addWidget(createBtn);
btnLayout->addStretch(1);
connect(createBtn, SIGNAL(clicked()), this, SLOT(addButton()));
}
void Widget::addButton()
{
// btnLayout->count() is equal to number of added buttons plus
// one QSpacerItem implicitly added by QBoxLayout::addStretch
int pos = btnLayout->count() - 1;
QPushButton *btn = new QPushButton;
btn->setText(QString("Button #%1").arg(pos));
btnLayout->insertWidget(pos, btn);
}

Qt setGeometry to negative value doesn't work

Im working with QT and I have a form with a QLabel in a QFrame. I want to set the QLabel's geometry so the bottom part of the QLabel is in the same place of the bottom of the frame. Since the label is longer than the frame, it's y coordinate should be negative.
int pos = ui->imageFrame->height() - ui->imageLabel->pixmap()->height();
ui->imageLabel->setGeometry(0, pos, ui->imageFrame->width(), p.height());
Although when printing the QLabel's geometry, the y coordinate is correct, the label is showing on the upper part of the frame.
Help is much appreciated.
You can set the label's alignment with setAlignment. Here's a working example:
#include <QtWidgets>
#include "MyWidget.h"
MyWidget::MyWidget()
{
setFixedSize(200,200);
QLabel *label = new QLabel;
label->setPixmap(QPixmap("/some/image/file.jpg"));
label->setAlignment(Qt::AlignBottom);
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(label);
hbox->setContentsMargins(0,0,0,0);
setLayout(hbox);
}

QT GridLayout add Stacked QLabel

I am creating an image gallery, i've implemented the reading in of Files and showing them in a resizable scroll-Area. We've decided to add meta-tags / Buttons and i am searching for a convenient way not to change too much but add this little features.
Any suggestion how i can achieve this? Can i add two Qlabels to each other? I tried to stuck two labels in a new layout and push this to the scrollWidgetLayout, but then i have only one Thumbnail.
//Create new ThumbNail-Object
thumbNail = new Thumbnail(ui->scrollArea);
scrollWidgetLayout->addWidget(thumbNail);
In the picture you can see what i have already and what i need (yellow).
You create a widget that acts like a container and put the labels inside it. Set a layout to this widget, I used QVBoxLayout. A better design would be to create a custom widget by subclassing QWidget, but I just used QFrame to make the example quick and simple.
centralWidget()->setLayout(new QVBoxLayout);
QScrollArea *area = new QScrollArea(this);
area->setWidgetResizable(true);
area->setWidget(new QWidget);
QGridLayout *grid = new QGridLayout;
area->widget()->setLayout(grid);
centralWidget()->layout()->addWidget(area);
for(int row = 0; row < 2; row++)
{
for(int column = 0; column < 5; column++)
{
QFrame *container = new QFrame; // this is your widget.. you can also subclass QWidget to make a custom widget.. might be better design
container->setStyleSheet("QFrame{border: 1px solid black;}"); // just to see the shapes better.. you don't need this
container->setLayout(new QVBoxLayout); // a layout for your widget.. again, if you subclass QWidget do this in its constructor
container->layout()->addWidget(new QLabel("TOP")); // the top label.. in your case where you show the icon
container->layout()->addWidget(new QLabel("BOTTOM")); // the bottom label.. in your case where you show the tag
grid->addWidget(container, row, column); // add the widget to the grid
}
}

QScrollArea with multiple QWidgets only shows empty box

I am trying to create a widget that would display some information. Each information would be a QWidget that contains multiple QLabel with text (the information). My idea is to put multiple (array of these) into a QScrollArea so that the user can view them scrolling up and down. The following code:
InfoWidget::InfoWidget(QWidget* parent) : QWidget(parent){
widgets = new QVector<MarkerInfoWidget*>();
csv_data = 0;
csv_velocity = 0;
labels = 0;
infoWidgetLayout = new QVBoxLayout(this);
setLayout(infoWidgetLayout);
scrollArea = new QScrollArea(this);
scrollWidgetLayout = new QVBoxLayout(scrollArea);
scrollArea->setLayout(scrollWidgetLayout);
infoWidgetLayout->addWidget(scrollArea);
//Test
QString name = "TEST";
for(int i=0; i<10; i++){
MarkerInfoWidget* markerWidget = new MarkerInfoWidget(name, scrollArea);
scrollWidgetLayout->addWidget(markerWidget);
widgets->append(markerWidget);
}
}
Both MarkerInfoWidget and InfoWidget extends QWidget. What I am getting is simply a box that has very small text:
If I drag it out and re-size it, it display correctly:
What I have noticed is that if I re-size it too small, it does not generate scrolls. What do I need to fix this?
I guess changing:
scrollArea->setLayout(scrollWidgetLayout);
to sth like:
QFrame* frame = new QFrame(scrollArea);
frame->setLayout(scrollWidgetLayout);
scrollArea->setWidget(frame);
As far as i know you have to put widget into QScrollableArea to make it really scrollable. Setting its layout is probably not the thing you want to do.