QCustomPlot Discussion and Comments

QCustomPlot widget in several QTabWidget - How do I plot in the active tab?Return to overview

I'd like to have a QTabWidget with a single QCustomplot widget in every tab. I have managed to achieve that with the following function:

void MainWindow::on_actionAddWorskpace_triggered()
{
    int WorkspaceNumber = ui->tabWidget->count();
    QWidget *WSCustomPlot = new QWidget();
    QCustomPlot *workspaceCustomPlot = new QCustomPlot(WSCustomPlot);
    workspaceCustomPlot->setObjectName(QStringLiteral("CPworkspace%1").arg(WorkspaceNumber));

    QString totalWorkspaces = QString("Workspace ") + QString::number(WorkspaceNumber + 1);
    ui->tabWidget->addTab(workspaceCustomPlot, totalWorkspaces);

    wsCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}

The function produces new tabs with fully functional QCustomPlot widgets as the interactions work.
But now I haven't been successful to produce the code to add plots to the QCustomPlot widget at the active tab, can someone help me, please?

You just need to cast the active widget as a QCustomPlot :

((QCustomPlot *)ui->tabWidget->currentWidget())

With this widget, you'll be able to manage the current QCustomPlot as you want!

I just had to add:
ui->tabWidget->setCurrentWidget(workspaceCustomPlot);
in order to made it work, at the end it worked great!
Thank's for the answer, Olivier!

Just another thing: when I try to connect a signal like:

//
//Update QLCDs with (x, y) coordinates as mouse pointer moves
//
connect(((QCustomPlot *)ui->tabWidget->currentWidget()), SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(xyLCDUpdate(QMouseEvent*)));
//


it seems like no signal is sent because the function xyLCDUpdate() is never executed .
Am I using it the right way? Thank's in advance.

If you need to use connect, then you should make your QCustomPlot a member of you class and initialize it.

EXAMPLE :

(in your class .h file)
QCustomPlot *mWorkspaceCustomPlot;

(then in the .cpp file of the class )
mWorkspaceCustomPlot = new QCustomPlot(WSCustomPlot);

(and finally, do your connection}
connect((mWorkspaceCustomPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(xyLCDUpdate(QMouseEvent*)));