QCustomPlot Discussion and Comments

QCPTextItem QTransform::translate with NaN calledReturn to overview

Hi i have this simple code

    m_customPlot->setObjectName("mainPlot");

    m_ItemText = new QCPItemText(m_customPlot);
    m_ItemText->setText("m_ItemText");
    m_ItemText->position->setTypeX(QCPItemPosition::ptViewportRatio);
    m_ItemText->position->setTypeY(QCPItemPosition::ptPlotCoords);
    m_ItemText->setPositionAlignment(Qt::AlignRight | Qt::AlignVCenter);

and i get output error
QTransform::translate with NaN called

what i found out is that

m_ItemText->position->setTypeX(QCPItemPosition::ptViewportRatio);

calls
setPixelPosition(pixel);

inside that we have
switch (mPositionTypeY) case case ptPlotCoords: y = mValueAxis.data()->pixelToCoord(y);

then
double QCPAxis::pixelToCoord(double value) const
   else // orientation() == Qt::Vertical
  {
    if (mScaleType == stLinear)
    {
      if (!mRangeReversed)
        return (mAxisRect->bottom()-value)/(double)mAxisRect->height()*mRange.size()+mRange.lower;
[code]
...

this will produce error as height is y2 - y1 + 1 and this is zero so we get devision by zero.

Even after i have data, and i set proper coordinates, i keep getting this error after 
[code]
void QCPItemText::draw(QCPPainter *painter)

is called. From what i found out value is generated as nan somewhere.

Are you calling this from the constructor of the QMainWindow (or any other parent of the QCustomPlot)?

Qt has the peculiar property that it doesn't define window/widget geometry in constructors, but only after the first iteration of the event loop. So if you use a coordinate system that depends on the widget geometry (like viewportratio), you must make sure that the QCustomPlot geometry is already properly defined and set up.

Hi, yes you are right, i was totally on wrong way of searching a problem. What i have is

This is called on button click

MainChart *mainChart = new MainChart(this);
int tabIndex = ui->tabWidgetCharts->insertTab(0, mainChart, icon, "chart");

// Mainchart.cpp

MainChart::MainChart(QWidget* parent) : QCustomPlot(parent)
{
    m_customPlot = this;
}

// Mainchart.h

class MainChart : public QCustomPlot
{
}

look like this is wrong passing a MainWindow as parent to QCustomPlot, becouse the parent is then tab widget i guess. Not sure how to solve this now.

I'm lost, if i do

Mainchart::Mainchart(QWidget* parent) : QCustomPlot(parent)
{
    qDebug() << "parent " << parent->geometry();

    m_customPlot = this;
    m_customPlot->setObjectName("mainPlot");

    m_ItemText = new QCPItemText(m_customPlot);
    m_ItemText->setText("m_ItemText");
    m_ItemText->position->setTypeX(QCPItemPosition::ptViewportRatio);
    m_ItemText->position->setTypeY(QCPItemPosition::ptPlotCoords);
    m_ItemText->setPositionAlignment(Qt::AlignRight | Qt::AlignVCenter);
}

I don't get it, as i pass parent to and geometry is good i get
parent QRect(0,0 995x305)
then why i keep getting error, but if i promote widget in qt creator there is no error.

How do you set the geometry of the m_customPlot in its parent? it seems like you're just creating it without ever setting geometry or using a QLayout to make it extend to the parent geometry. In that case parent->geometry() is irrelevant. Check what m_customPlot->geometry() says, after creation. It's probably zero height or something.

    qDebug() << "m_customPlot geometry " << m_customPlot->geometry();

this returns
m_customPlot geometry QRect(0,0 100x30)

This is what i have

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    Mainchart *mainChart = new Mainchart(ui->tabWidget->widget(0));

    int tabIndex = ui->tabWidget->insertTab(10, mainChart, QIcon(""), "Trolol");

    ui->tabWidget->setCurrentIndex(tabIndex);
}

Mainchart(ui->tabWidget->widget(0));

is actually

Mainchart *mainChart = new Mainchart(this);

so here i'm wrong, i was thinking it will get size from main gui.

If your QCustomPlot widget has height of only 30 pixels, it may well be that the axis rect has height zero due to the margins and labels, etc.
I guess this is the problem. You need to give the widget a proper sizing, either by explicitly setting it, or preferably by using a QLayout (That's a Qt GUI class, don't confuse with QCPLayout).

Okey, now i get it, i had hard time becouse everything was working, except the error message, the plot is displayed correctly and take full size. I will try to add layout now, and see it, thanks for help!