Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use of undeclared identifier 'setCentralWidget'
Forum Updated to NodeBB v4.3 + New Features

Use of undeclared identifier 'setCentralWidget'

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Troyer
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • T Troyer

      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

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Troyer Please post text not screen-shots.

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      1
      • jsulmJ jsulm

        @Troyer Please post text not screen-shots.

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

        T Offline
        T Offline
        Troyer
        wrote on last edited by
        #3

        @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;
        }
        
        jsulmJ 1 Reply Last reply
        0
        • T Troyer

          @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;
          }
          
          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply
          1
          • jsulmJ jsulm

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

            T Offline
            T Offline
            Troyer
            wrote on last edited by
            #5

            @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
            
            
            jsulmJ 1 Reply Last reply
            0
            • T Troyer

              @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
              
              
              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              T 1 Reply Last reply
              1
              • jsulmJ jsulm

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

                T Offline
                T Offline
                Troyer
                wrote on last edited by
                #7

                @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 :)

                jsulmJ 1 Reply Last reply
                0
                • T Troyer

                  @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 :)

                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  T 1 Reply Last reply
                  5
                  • jsulmJ jsulm

                    @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);
                    }
                    
                    T Offline
                    T Offline
                    Troyer
                    wrote on last edited by
                    #9

                    @jsulm Thank you very much :)

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved