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

Why numerator function doesn't work?



  • This program should add enumerated points on screen on every left button press. Points are ellipses painted on the scene, numbering should be done by new labels creation.

    mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QLabel>
    #include <QTimer>
    #include <QResizeEvent>
    
    #include <paintscene.h>
    
    
    //QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    //QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
        void numerator(int num, qreal x, qreal y);
    
    private:
        Ui::MainWindow *ui;
        QTimer *timer;
        paintScene *scene;
    
        void resizeEvent(QResizeEvent * event);
    
    
        int j = 1;
        bool ind = true;
    
    
    private slots:
        void slotTimer();
    
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        scene = new paintScene();
        ui->graphicsView->setScene(scene);
    
        timer = new QTimer();
        connect(timer, &QTimer::timeout, this, &MainWindow::slotTimer);
        timer->start(100);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::numerator(int num, qreal x, qreal y)
    {
        QLabel *label = new QLabel(this);
        label->setText(QString("%1").arg(num));
        label->setGeometry(x+10,y-20,20,20);
        label->show();
    }
    
    void MainWindow::slotTimer()
    {
        timer->stop();
        scene->setSceneRect(0,0, ui->graphicsView->width() - 20, ui->graphicsView->height() - 20);
    }
    
    
    void MainWindow::resizeEvent(QResizeEvent *event)
    {
        timer->start(100);
        QWidget::resizeEvent(event);
    }
    

    paintscene.h

    #ifndef PAINTSCENE_H
    #define PAINTSCENE_H
    
    #include <QGraphicsScene>
    #include <QGraphicsSceneMouseEvent>
    #include <QTimer>
    
    #include <QLabel>
    
    class paintScene : public QGraphicsScene
    {
    
        Q_OBJECT
    
    public:
        paintScene(QObject *parent = 0);
        ~paintScene();
    
        void addPoint(qreal x, qreal y);
    
    private:
        void mousePressEvent(QGraphicsSceneMouseEvent * event);
    
    
        QVector<qreal> coord_x, coord_y, distances;
        bool ind = true;
        const qreal rad = 5.0;
        qreal last_pt_x, last_pt_y;
        int i = 1;
    
    
    };
    
    #endif // PAINTSCENE_H
    
    

    paintscene.cpp

    #include "paintscene.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    //#include <QLabel>
    
    paintScene::paintScene(QObject *parent) : QGraphicsScene(parent)
    {
        
    }
    
    paintScene::~paintScene()
    {
    
    }
    
    void paintScene::addPoint(qreal x, qreal y)
    {
        coord_x.push_back(x);
        coord_y.push_back(y);
    }
    
    void paintScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        MainWindow mw;
        if (event->button() == Qt::LeftButton && ind == true)
            {
            addEllipse(event->scenePos().x() - rad,
                       event->scenePos().y() - rad,
                       2*rad,
                       2*rad,
                       QPen(Qt::NoPen),
                       QBrush(Qt::red));
            mw.numerator(i,event->scenePos().x(), event->scenePos().y());
            ++i;
            addPoint(event->scenePos().x(), event->scenePos().y());
    
            }
        else if (event->button() == Qt::RightButton && ind == true)
            {
            addEllipse(event->scenePos().x() - rad,
                       event->scenePos().y() - rad,
                       2*rad,
                       2*rad,
                       QPen(Qt::NoPen),
                       QBrush(Qt::black));
            ind = false;
            last_pt_x = event->scenePos().x();
            last_pt_y = event->scenePos().y();
        }
    }
    
    
    
    

  • Moderators

    @ddryomyss said in Why numerator function doesn't work?:

    please read up on the c++ basics, about object instances, scopes, heap and stack

    because this

    void paintScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    MainWindow mw;

    is != to

    to the MainWindow that you have inside main.cpp and that you actually show.


Log in to reply