QT c++ list of charts - c++

I am creating dynamic list of charts and pushing them into ui.verticalLayout->addWidget(chartView,Qt::AlignCenter); But as more items i add - than smaller they become, because of fitting to the size of vertical layout. I was trying to find more information about adding to list or smth like that - but didn't realized nothing related with charts. Here is code:
for (int i = 0; i < 5; i++) {
QPieSeries* serie_pie = new QPieSeries();
double free_serie = 1.33;
double used_serie = 3.1;
serie_pie->append("Free", free_serie);
serie_pie->append("Used", used_serie);
QChart* chart_for_pie = new QChart();
chart_for_pie->addSeries(serie_pie);
chart_for_pie->setMargins(QMargins(0, 0, 0, 0));
chart_for_pie->legend()->setAlignment(Qt::AlignRight);
QChartView* chartView = new QChartView(chart_for_pie);
chart_for_pie->setBackgroundVisible(false);
chartView->setRenderHint(QPainter::Antialiasing);
ui.verticalLayout->addWidget(chartView,Qt::AlignCenter);// add into another place :)
}
Thanks for any help.

QChart inherits by QGraphicsWidget. So you can try to use
1) QGraphicsWidget::resize(qreal w, qreal h)
or
2) QGraphicsWidget::resize(const QSizeF &size)
And one more for beauty QGraphicsLayoutItem::setGeometry(const QRectF &rect)
https://doc.qt.io/qt-5/qgraphicswidget.html
If you want to try resize QChartView
try it:
1) QWidget::setMinimumSize(int minw, int minh)
2) QWidget::setMaximumSize(int maxw, int maxh)
https://doc.qt.io/qt-5/qwidget.html#setMaximumSize-1

Create a QScrollArea and add your charts to that

Related

Drawing border around image QT.

I am having a few issues with this code to draw a border around an image in QT, can anyone tell me what i am missing:
void imageLabel::paintEvent(QPaintEvent *event)
{
QLabel::paintEvent(event);
if (!m_qImage.isNull())
{
QImage qImageScaled = m_qImage.scaled(QSize(width(),height()),Qt::KeepAspectRatio,Qt::FastTransformation);
double dAspectRatio = (double)qImageScaled.width()/(double)m_qImage.width();
int iX = m_iX*dAspectRatio;
int iY = m_iY*dAspectRatio;
int iWidth = m_iWidth*dAspectRatio;
int iHeight = m_iHeight*dAspectRatio;
QPainter qPainter(this);
qPainter.drawImage(0,0,qImageScaled);
qPainter.setBrush(Qt::NoBrush);
qPainter.setPen(Qt::red);
qPainter.drawRect(iX,iY,iWidth,iHeight);
}
}
You can use QFrame to simplify the task of adding a frame around a widget like QLabel.
In QtCreator, simply select the label and scroll down until you see the turquoise section of the properties editor and play with the values there.
The result looks like this:
Hope this helps you along!

Spacing between widgets in QHBoxLayout

I'm trying to create a GUI with QtCreator. For this GUI, I need to display several images with different sizes next to each other. These images should be touching each other.
I use a QWidget with a QHBoxLayout, where I add the labels (with different sizes) containing the images.
According to related questions, I should use setSpacing and setContentsMargin to remove these spaces, but that won't work; I tried several times.
Here's the code:
QWidget *widget = new QWidget(ui->tagcloud);
QHBoxLayout * l = new QHBoxLayout(widget);
ui->tagcloud->setWidget(widget);
for(int i=0;i<list.size();++i)
{
QLabel *lab = new QLabel;
QPixmap pic((list[i].imgPath).c_str()); //This fetches the image
int sizeChange = 50 + (2*list[i].percent); //Calculates the size of the image
lab->setFixedSize(QSize(sizeChange, sizeChange));
lab->setPixmap(pic);
lab->setScaledContents(true);
l->addWidget(lab);
l->setSpacing(0);
}
However, when I run this, the spacing remains the same (i.e. definitely not zero).
If I add more labels to the layout, the spacing seems to get smaller.
Can anyone explain or help me? Thanks!
Setting spacing to 0 and adding stretch before and after works for me :
l->addStretch();
for(int i = 0; i < list.size(); ++i)
{
QLabel *lab = new QLabel;
QPixmap pic((list[i].imgPath).c_str()); //This fetches the image
int sizeChange = 50 + (2*list[i].percent); //Calculates the size of the image
lab->setFixedSize(QSize(sizeChange, sizeChange));
lab->setPixmap(pic);
lab->setScaledContents(true);
l->addWidget(lab);
}
l->addStretch();
l->setSpacing(0);
Also this works I think
l->setSizeConstraint(QLayout::SetMaximumSize);

QT 2-dimensional array graphical grid

I am making a 2-dimensional grid (2-d Cellular Automaton). So I made full code for console. But now I need to implement that with GUI. I am using linux, so I read out that QT Creator will be the best choice.
Can somebody give me a small tip. How is better to start. Which Views/widjets you recommend to use for that? Any help would be useful.
In c++ code I made a 2-dimensional char array of size 15/15 elements which have elements of type '1' or '0'.
I will be very grateful for any help! Thank you in advance.
One of ways: create a QImage and fill it with your data using setPixel. Convert it to QPixmap and use QLabel to display it.
Below is the two simple examples of how to show arrays on GUI. There are possible other variations too, of course, depending on your needs.
Widget with a grid layout
QWidget *mainWidget = new QWidget;
QGridLayout *layout = new QGridLayout;
for (int r = 0; r < 15; r++) {
for (int c = 0; c < 15; c++) {
QLabel *label = new QLabel("1", mainWidget); // Text could be 1 or 0.
layout->addWidget(label, r, c);
}
}
mainWidget->setLayout(layout);
mainWidget->show();
Using table view
QTableWidget *table = new QTableWidget(15, 15);
for (int r = 0; r < 15; r++) {
for (int c = 0; c < 15; c++) {
QTableWidgetItem *item = new QTableWidgetItem("1"); // Text could be 1 or 0.
table->setItem(r, c, item);
}
}
table->show();

Delete A Row From QGridLayout

All, I am maintaining a QGridLayout of QLabels which show the coefficients of a polynomial. I represent my polynomial using QList<double>.
Each time I update my coefficients, I update my labels. When changing the size of the list, my method does not works well. QGridLayout::rowCount() doesn't update correctly. I am wondering if there's a way to remove rows from a QGridLayout.
Code follows, updating the QGridLayout size with more (or less) QLabels
int count = coefficients->count(); //coefficients is a QList<double> *
if(count != (m_informational->rowCount() - 1)) //m_information is a QGridLayout
{
SetFitMethod(0);
for(int i = 0; i < count; ++i)
{
QLabel * new_coeff = new QLabel(this);
new_coeff->setAlignment(Qt::AlignRight);
m_informational->addWidget(new_coeff, i+1, 0);
QLabel * param = new QLabel(this);
param->setAlignment(Qt::AlignLeft);
param->setText(QString("<b><i>x</i><sup>%2</sup></b>").arg(count-i-1));
m_informational->addWidget(param, i+1, 1);
QSpacerItem * space = new QSpacerItem(0,0,QSizePolicy::Expanding);
m_informational->addItem(space, i+1, 1);
}
m_informational->setColumnStretch(0, 3);
m_informational->setColumnStretch(1, 1);
m_informational->setColumnStretch(2, 1);
}
The SetFitMethod (it's an initial mockup)
void SetFitMethod(int method)
{
ClearInformational();
switch(method)
{
case 0: //Polynomial fit
QLabel * title = new QLabel(this);
title->setText("<b> <u> Coefficients </u> </b>");
title->setAlignment(Qt::AlignHCenter);
m_informational->addWidget(title,0,0,1,3, Qt::AlignHCenter);
}
}
The Clearing Method:
void ClearInformational()
{
while(m_informational->count())
{
QLayoutItem * cur_item = m_informational->takeAt(0);
if(cur_item->widget())
delete cur_item->widget();
delete cur_item;
}
}
The issue is that QGridLayout::rowCount() doesn't actually return the number of rows that you can see, it actually returns the number of rows that QGridLayout has internally allocated for rows of data (yes, this isn't very obvious and isn't documented).
To get around this you can either delete the QGridLayout and recreate it, or if you're convinced that your column count won't change, you can do something like this:
int rowCount = m_informational->count()/m_informational->columnCount();
I solved this by creating a QVBoxLayout (for rows) and within this I was adding QHBoxLayout (for columns). In the QHBoxLayout I was then inserting my widgets (in one row). This way I was able to nicely remove rows - overall row count was working as it should be. Additionally to this I got also an insert method, thanks to which I was able to insert new rows into specific locations (everything was correctly reordered/renumbered).
Example (only from head):
QVBoxLayout *vBox= new QVBoxLayout(this);
//creating row 1
QHBoxLayout *row1 = new QHBoxLayout();
QPushButton *btn1x1 = new QPushButton("1x1");
QPushButton *btn1x2 = new QPushButton("1x2");
row1->addWidget(btn1x1);
row1->addWidget(btn1x2);
//adding to vBox - here you can use also insertLayout() for insert to specific location
vBox->addlayout(row1);
//creating row 2
QHBoxLayout *row2 = new QHBoxLayout();
QPushButton *btn2x1 = new QPushButton("2x1");
QPushButton *btn2x2 = new QPushButton("2x2");
row2->addWidget(btn2x1);
row2->addWidget(btn2x2);
//adding to vBox - here you can use also insertLayout() for insert to specific location
vBox->addlayout(row2);
Well, my solution was to also delete the QGridLayout in ClearInformational

Segfault when adding Qwt plot to layout

I try to build chart demo using Qwt and C++. I've added the following code to button click handler:
QwtPlot *plot = new QwtPlot(QwtText("Demo"));
plot->setGeometry(0, 0, 100, 100);
QwtPlotCurve curve("Sine");
QVector<double> xs;
QVector<double> ys;
for (double x = 0; x < 100; x++)
{
xs.append(x);
ys.append(sin(x));
}
QwtPointArrayData *series = new QwtPointArrayData(xs, ys);
curve.setData(series);
curve.attach(plot);
plot->show();
QLayout *lay = ui->centralWidget->layout();
lay->addWidget(plot);
and it segfaults at addWidget(plot);.
What am I doing wrong?
The layout of centralWidget is probably NULL or you haven't initialized the ui (e.g. calling setUp()). Check the first case with if(lay == NULL) and the second by looking at your code.
If the first case is correct have a look at your UI file in QDesigner and add a layout.