En este ejemplo de código se crean dos botones: el primero muestra una gráfica en una nueva ventana, el segundo imprime una gráfica en otro hilo para hacer un uso concurrente y no gastar el hilo principal (no es el caso del ejemplo pero podría demorar mucho en generar los datos de la gráfica) .
La cuestión es que en la misma ejecución podemos hacer uso del primer botón o del segundo repetidamente sin ningún problema, pero si los alternamos producimos un error. A pesar de instanciar el objeto independientemente a cada botón y tener un alcance local del mismo se produce el error:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTextDocument(0x562988b91200),
parent's thread is QThread(0x562987bc9810),
current thread is QThread(0x562987e68ac0))
¿Alguien sabe como solucionarlo?
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QtConcurrent/QtConcurrent>
#include <QHBoxLayout>
#include <QChartView>
#include <QLineSeries>
#include <QValueAxis>
#include <QChart>
#include <QDebug>
#include <QPrintEngine>
class ChartWidget : public QWidget {
Q_OBJECT
public:
ChartWidget(QWidget *parent = nullptr) : QWidget(parent) {
QtCharts::QChart *chart = new QtCharts::QChart;
QtCharts::QLineSeries *series = new QtCharts::QLineSeries;
for (int i = 0; i < 10; ++i)
series->append(i, qrand() % 100);
chart->addSeries(series);
QtCharts::QValueAxis *axisX = new QtCharts::QValueAxis;
QtCharts::QValueAxis *axisY = new QtCharts::QValueAxis;
chart->addAxis(axisX, Qt::AlignBottom);
chart->addAxis(axisY, Qt::AlignLeft);
series->attachAxis(axisX);
series->attachAxis(axisY);
QtCharts::QChartView *chartView = new QtCharts::QChartView(chart);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(chartView);
setLayout(layout);
}
~ChartWidget() {
close();
}
void printChart() {
QPrinter *Printer=new QPrinter();
QPainter *Painter=new QPainter();
Printer->setOutputFormat(QPrinter::PdfFormat);
Printer->setOutputFileName("/tmp/test.pdf");
Painter->begin(Printer);
this->render(Painter);
Painter->end();
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QPushButton *button1=new QPushButton("Main Th");
QPushButton *button2=new QPushButton("Concurrent Th");
QWidget *window=new QWidget;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
window->setLayout(layout);
window->show();
QObject::connect(button1, &QPushButton::clicked, []() {
ChartWidget *W=new ChartWidget();
W->show();
});
QObject::connect(button2, &QPushButton::clicked, []() {
QtConcurrent::run([](){
ChartWidget *W=new ChartWidget();
W->printChart();
});
});
return app.exec();
}
#include "main.moc"
Parece que realmente el problema esta en el QChartView, en este simple ejemplo se puede ver el error como una advertencia:
#include <QApplication>
#include <QtConcurrent/QtConcurrent>
#include <QChartView>
#include <QChart>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QtConcurrent::run([](){
QtCharts::QChartView Plot;
});
QWidget MyWindow;
MyWindow.show();
return app.exec();
}
#include "main.moc"
¿Se puede usar el QChart sin el QChartView?