Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Use of undeclared identifier 'setCentralWidget'



  • Hello!
    How do I solve this problem related to setCentralWidget? What should I do? I need to display the graph on the tab. I can get it to output to mainwindow.cpp, but not in the other cpp file

    1.JPG
    2.JPG


  • Lifetime Qt Champion

    @Troyer Please post text not screen-shots.

    setCentralWidget is a method in QMainWindow. What is the base class of your fundction_QPainter class?



  • @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;
    }
    

  • Lifetime Qt Champion

    @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
    
    

  • Lifetime Qt Champion

    @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?



  • @jsulm All I want to do is display on the tab (which I'm already very tired of) a graph of the function built in the code. And for some reason, without this 'setCentralWidget' command, it doesn't want to display it on the tab :)


  • Lifetime Qt Champion

    @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);
    }
    


  • @jsulm Thank you very much :)


Log in to reply