I'm trying to use QtCharts with X-Axis and Y-Axis in the center of chart.
Therefore the center point will locate in the middle of my QtChart... There are options to alignment axis to the top/bottom or left/right (addAxis(QValueAxis*,Qt::AlignTop)). But, there is no option to align it to the center. When I use AlignCenter as addAxis argument, I get an error.
Any Help?
Tanks.
Edit (Providing code and error):
QChart *BScopeChart = new QChart();
QLineSeries *BScopeSerie = new QLineSeries(this);
BScopeChart->setMargins(QMargins(0, 0, 0, 0));
QValueAxis *BScopeAxisX = new QValueAxis;
BScopeAxisX->setRange(-50, 50);
QValueAxis *BScopeAxisY = new QValueAxis;
BScopeAxisY->setRange(-5, 5);
BScopeChart->addAxis(BScopeAxisY, Qt::AlignLeft); // I need this Line change to something like: BScopeChart->addAxis(BScopeAxisY, Qt::AlignCenter)
BScopeChart->addAxis(BScopeAxisX, Qt::AlignBottom); // I need this Line change to something like: BScopeChart->addAxis(BScopeAxisX, Qt::AlignCenter);
ui.widgetBScopeQChart->setChart(BScopeChart);
and when I change those two lines to:
BScopeChart->addAxis(BScopeAxisY, Qt::AlignCenter);
BScopeChart->addAxis(BScopeAxisX, Qt::AlignCenter);
I face this error:
ErrorScreenshot
Related
I want to draw a line to connect two circles (QGraphicsEllipseItem), but I find that I don't get the desired result with this way of writing.
//they have been initialized to the correct place
QGraphicsEllipseItem* nodeu;
QGraphicsEllipseItem* nodev;
this->addLine(nodeu->x(), nodeu->y(), nodev->x(), nodev->y());
The result of executing these codes is that only two circles appear, but no lines appear.
like this
My rough inference is the problem of coordinate transformation, but I just can't solve it.
thank you!
you should first add one QGraphicsView in your UI or :
QGraphicsView *graphicsView;
QGridLayout *gridLayout;
gridLayout = new QGridLayout(centralwidget);
gridLayout->setSpacing(0);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
graphicsView = new QGraphicsView(centralwidget);
graphicsView->setObjectName(QString::fromUtf8("graphicsView"));
gridLayout->addWidget(graphicsView, 0, 0, 1, 1);
then :
QGraphicsScene *_scene = new QGraphicsScene(this);
ui->graphicsView->setScene(_scene);
ui->graphicsView->setRenderHints(QPainter::Antialiasing);
QGraphicsEllipseItem *nodeu = new QGraphicsEllipseItem;
nodeu->setRect(20, 10, 20, 20);
_scene->addItem(nodeu);
QGraphicsEllipseItem *nodev = new QGraphicsEllipseItem;
nodev->setRect(80, 60, 20, 20);
_scene->addItem(nodev);
QGraphicsLineItem *_lineItem = new QGraphicsLineItem;
_lineItem->setLine(nodeu->rect().x() + nodeu->rect().width() / 2.0, nodeu->rect().y() + nodeu->rect().height() / 2.0,
nodev->rect().x() + nodev->rect().width() / 2.0, nodev->rect().y() + nodev->rect().height() / 2.0);
_scene->addItem(_lineItem);
this is the output:
I am making a QScatterSeries. I have some points plotted in 3 different series (red, green, blue). It all plots perfectly when I use chart->createDefaultAxes(). All points are where they should be. The issue is that when I change the X Axis and Y Axis in my chart, all my points become incorrectly plotted. None of them are where they should be. This is strange because I have hard-coded my points.
I have tried adding my series to the chart before and after adding the X and Y Axis to my chart, thinking that may have caused my points to become confused. But that did not fix it.
Chart when I use the default axes:
char *myargv[2];
int myargc = 1;
myargv[0] = strdup("");
// First, create QApplication
QApplication a(myargc, myargv);
// Create our red, green, blue acceleration series
redSeries = new QScatterSeries;
redSeries->setMarkerSize(15.0);
redSeries->append(1, 20);
redSeries->append(2, 30);
redSeries->append(3, 44);
redSeries->setColor(Qt::red);
greenSeries = new QScatterSeries;
greenSeries->setMarkerSize(15.0);
greenSeries->append(1, 10);
greenSeries->append(2, -9);
greenSeries->append(3, 20);
greenSeries->setColor(Qt::green);
blueSeries = new QScatterSeries;
blueSeries->setMarkerSize(15.0);
blueSeries->append(1, -20);
blueSeries->append(2, -10);
blueSeries->append(3, 0);
blueSeries->setColor(Qt::blue);
// Customize our chart
QChart *chart = new QChart();
chart->createDefaultAxes();
chart->setTitle("Example Plot");
QValueAxis *axisX = new QValueAxis;
axisX->setRange(0, 130);
axisX->setTickCount(10);
QValueAxis *axisY = new QValueAxis;
axisY->setRange(-50, 50);
axisY->setTickCount(10);
// Add series to chart
chart->addSeries(redSeries);
chart->addSeries(greenSeries);
chart->addSeries(blueSeries);
// Create chartView
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
//chartView->chart()->setAxisX(axisX);
//chartView->chart()->setAxisY(axisY);
// Render everything within our Window
QMainWindow w;
w.setCentralWidget(chartView);
w.resize(400, 300);
w.show();
return a.exec();
}
When I try to use my own X and Y axis, I simply comment out the chart->createDefaultAxes() and comment back in the:
//chartView->chart()->setAxisX(axisX);
//chartView->chart()->setAxisY(axisY);
This causes me to get the incorrectly plotted chart:
My question is, what am I doing wrong? Also, in other parts of my code, I plan to dynamically receive data and use that data to plot points on my graph, such as this:
while (receivingData) {
redSeries->append(x, y); // Will not work because my hard-coded points (above) do not work
greenSeries->append(x, y);
blueSeries->append(x, y);
}
What do I have to do to ensure that all my points are plotted correctly with my custom X and Y Axis? I am assuming that I will most likely have to do some math within my append to match my QChart's new dimensions but how do I achieve that?
the answer is ,add call to
QAbstractSeries::attachAxis
somewhere,
I suspect.
I found the answer to my question in another post.
Posting here in case it helps someone
QtCharts add custom axis
I can`t understand how to align form layout in groupbox by center.
This is how i want it to work. (Correct)
This is how it works now. (Wrong)
Here is the code.
mainWinGroupBox = new QGroupBox(tr("Window Settings"));
mainWinGroupBox->setAlignment(Qt::AlignHCenter);
auto fWinLayout = new QFormLayout;
fWinLayout->setFormAlignment(Qt::AlignHCenter);
fWinLayout->addRow(tr("&Tray:"), trayCheckBox);
WindowWidthSB->setFixedSize(42, 20);
WindowWidthSB->setRange(0, 1920);
WindowWidthSB->setSingleStep(10);
fWinLayout->addRow(tr("&Window Width:"), WindowWidthSB);
WindowHeightSB->setFixedSize(42, 20);
WindowHeightSB->setRange(0, 1080);
WindowHeightSB->setSingleStep(10);
fWinLayout->addRow(tr("&Window Height:"), WindowHeightSB);
mainWinGroupBox->setLayout(fWinLayout);
You can align your labels with this property QFormLayout::labelAlignment.
I am currently trying to get my head around Line Charts in Qt. For some reason, my charts seem to show really weird behavior. When I am using the createDefaultAxis it will set the minimum and maximum values according to the minimum and maximum values of the Series behind the chart. While this seems alright at first it's already different from the behavior in this example. The minimum Y-Value there is 1, the minimum value on the axis, however, is 0.
At first, I thought this might just be due to a change to the QChart class, so I created my own Axis and tried again. This time I made the y-Axis range from 0 to 100 and the x-Axis range from 1 to 52. Also, I changed the tick counts to 4 and 52. However, the chart still looked like before and didn't seem to be affected by the changes to the axis.
I included a screenshot of that program here
I hope you can help me fix that. The goal would be that the values of the series match the values on the axes.
Edit: Here is the main.cpp:
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLineSeries *series = new QLineSeries();
series->append(0, 6);
series->append(2, 4);
series->append(3, 8);
series->append(7, 4);
series->append(10, 5);
*series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
QChart *chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
QValueAxis *axisX = new QValueAxis();
axisX->setRange(1, 52);
axisX->setMin(1);
axisX->setMax(52);
axisX->setTickCount(52);
QValueAxis *axisY = new QValueAxis();
axisY->setRange(0, 100);
axisY->setMin(0);
axisY->setMax(100);
axisY->setTickCount(4);
chart->setAxisX(axisX);
chart->setAxisY(axisY);
chart->setTitle("Simple line chart example");
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(400, 300);
window.show();
return a.exec();
}
The series isn't attached to any axis, it will by default scale to utilize the entire plot area of the chart.
You should attach the series to axis created as :
//... After setting up your axis X and Y
chart->setAxisX(axisX);
chart->setAxisY(axisY);
//attach the series to the specific axis.
series->attachAxis(axisX);
series->attachAxis(axisY);
to add axis on chart use following technique.
addAxis(xAxis,postion);
addAxis(yAxis,postion);
and then attached series to it
I am building a custom widget based on QGraphicsWidget
Inside the widget, I need various other widgets in line with the size of the window. For this I am using
Layout = new QGraphicsLinearLayout();
Layout->setOrientation(Qt::Horizontal);
setLayout(Layout);
So I have a layout, and on each side of the layout I have a widget. So a widget on the left side, and a widget on the right side.
QGraphicsLinearLayout() below
500 px wide x
|--------------------------------|
| |
|left widget right widget| 100 px tall y
|--------------------------------|
I want the behaviour that when the window gets bigger, the widgets stay within x pixels of the edge. So if the window were to be resized to 10,000 pixels wide, left widget would always be 1 pixel from the left edge and right widget would always be 1 pixel from the right edge.
10000 px wide x
|---------------------------------------------------|
| |
|left widget right widget| 100 px tall y
|---------------------------------------------------|
The current behaviour is that left widget will stay in place and never move, and right widget will move away from the right edge.
To achieve this behaviour, I have tried the following:
Layout->setAlignment(leftWidget, Qt::AlignLeft);
That does absolutely nothing.
I also tried
Layout->addItem(leftWidget);
Layout->setStretchFactor(leftWidget, 0);
Layout->addItem(rightWidget);
Which gives the desired effect but overlaps the rightWidget with the border like so
500 px wide x
|--------------------------------|
| |
|left widget right widget 100 px tall y
|--------------------------------|
So how can I get my desired behaviour? QLayouts seem very confusing and the API's misleading thus far.
Solution
Use QGraphicsLinearLayout::addStretch between the left and right widgets in order to keep them to the sides and QGraphicsLayout::setContentsMargins to add a space between the widgets and the edges:
auto *l = new QGraphicsLinearLayout();
l->addItem(leftProxy);
l->addStretch();
l->addItem(rightProxy);
l->setContentsMargins(25, 1, 1, 1);
l->setSpacing(1);
Example
I have prepared a minimal example for you of how to use the proposed solution:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
auto *view = new QGraphicsView(this);
auto *widget = new QWidget();
auto *leftWidget = new QPushButton(tr("Left"));
auto *rightWidget = new QPushButton(tr("Right"));
auto *leftProxy = new QGraphicsProxyWidget();
auto *rightProxy = new QGraphicsProxyWidget();
auto *l = new QGraphicsLinearLayout();
auto *sizeGrip = new QSizeGrip(widget);
leftWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
rightWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
widget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
widget->setMinimumSize(sizeGrip->sizeHint().width()
+ leftWidget->sizeHint().width()
+ rightWidget->sizeHint().width(),
leftWidget->sizeHint().height());
leftProxy->setWidget(leftWidget);
rightProxy->setWidget(rightWidget);
l->addItem(leftProxy);
l->addStretch();
l->addItem(rightProxy);
l->setContentsMargins(25, 1, 1, 1);
l->setSpacing(1);
view->setScene(new QGraphicsScene(this));
view->scene()->addWidget(widget)->setLayout(l);
widget->resize(400, 200);
setCentralWidget(view);
resize(640, 480);
}