Use of undeclared identifier 'setCentralWidget'
-
@jsulm I created these classes by simply right-clicking on file - > add new -> and selecting the widget class. So the base class turns out to be QMainWindow
function_qpainter.cpp
#include "function_qpainter.h" #include "ui_function_qpainter.h" #include <QtCharts/QChartView> function_QPainter::function_QPainter(QWidget *parent) : QWidget(parent), ui(new Ui::function_QPainter) { ui->setupUi(this); QScatterSeries *series0 = new QScatterSeries(); series0->setName("Синие точки"); series0->setMarkerShape(QScatterSeries::MarkerShapeCircle); series0->setMarkerSize(15.0); QScatterSeries *series1 = new QScatterSeries(); series1->setName("Зеленые квадраты"); series1->setMarkerShape(QScatterSeries::MarkerShapeRectangle); series1->setMarkerSize(20.0); QScatterSeries *series2 = new QScatterSeries(); series2->setName("Оранжевые звезды"); series2->setMarkerShape(QScatterSeries::MarkerShapeRectangle); series2->setMarkerSize(30.0); series0->append(0, 6); series0->append(2, 4); series0->append(3, 8); series0->append(7, 4); series0->append(10, 5); *series1 << QPointF(1, 1) << QPointF(3, 3) << QPointF(7, 6) << QPointF(8, 3) << QPointF(10, 2); *series2 << QPointF(1, 5) << QPointF(4, 6) << QPointF(6, 3) << QPointF(9, 5); QPainterPath starPath; starPath.moveTo(28, 15); for (int i = 1; i < 5; ++i) { starPath.lineTo(14 + 14 * qCos(0.8 * i * M_PI), 15 + 14 * qSin(0.8 * i * M_PI)); } starPath.closeSubpath(); QImage star(30, 30, QImage::Format_ARGB32); star.fill(Qt::transparent); QPainter painter(&star); painter.setRenderHint(QPainter::Antialiasing); painter.setPen(QRgb(0xf6a625)); painter.setBrush(painter.pen().color()); painter.drawPath(starPath); series2->setBrush(star); series2->setPen(QColor(Qt::transparent)); QChart *chart = new QChart(); chart->addSeries(series0); chart->addSeries(series1); chart->addSeries(series2); chart->setTitle("Пример точечной диаграммы"); chart->createDefaultAxes(); chart->setDropShadowEnabled(false); chart->legend()->setMarkerShape(QLegend::MarkerShapeFromSeries); QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); setCentralWidget(chartView); } function_QPainter::~function_QPainter() { delete ui; }
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include "function_qpainter.h" #include "function_qchart.h" #include "function_qchart_view.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QTabWidget *ptrTabWidget = new QTabWidget(this); ptrTabWidget->addTab(new function_QPainter(), "QPainter"); ptrTabWidget->addTab(new function_qchart(), "QChart"); ptrTabWidget->addTab(new function_qchart_view(), "QChartView"); ptrTabWidget->resize(400,600); } MainWindow::~MainWindow() { delete ui; }
-
@Troyer said in Use of undeclared identifier 'setCentralWidget':
So the base class turns out to be QMainWindow
This is wrong.
You call setCentralWidget in function_QPainter not in MainWindow. MainWindow is a QMainWindow.
So, again: what is the base class of function_QPainter? Can you show its declaration? -
@jsulm Sorry, i'm new to this app
it is my base class
function_qpainter.h
#ifndef FUNCTION_QPAINTER_H #define FUNCTION_QPAINTER_H #include <QWidget> #include <QtCharts/QtCharts> #include <QPainterPath> #include <QtCharts/QLineSeries> #include <QtCharts/QSplineSeries> namespace Ui { class function_QPainter; } class function_QPainter : public QWidget { Q_OBJECT public: explicit function_QPainter(QWidget *parent = nullptr); ~function_QPainter(); private: Ui::function_QPainter *ui; }; #endif // FUNCTION_QPAINTER_H
-
@Troyer said in Use of undeclared identifier 'setCentralWidget':
class function_QPainter : public QWidget
As you can see function_QPainter is a QWidget and QWidget does not have setCentralWidget. Why do you try to call setCentralWidget there?
-
@Troyer So you want to show your function_QPainter in a tab in ptrTabWidget in MainWindow, right?
You need to call setCentralWidget in MainWindow:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QTabWidget *ptrTabWidget = new QTabWidget(this); ptrTabWidget->addTab(new function_QPainter(), "QPainter"); ptrTabWidget->addTab(new function_qchart(), "QChart"); ptrTabWidget->addTab(new function_qchart_view(), "QChartView"); ptrTabWidget->resize(400,600); setCentralWidget(ptrTabWidget); }