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. Why numerator function doesn't work?

Why numerator function doesn't work?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 142 Views 1 Watching
  • 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.
  • ddryomyssD Offline
    ddryomyssD Offline
    ddryomyss
    wrote on last edited by ddryomyss
    #1

    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();
        }
    }
    
    
    
    
    1 Reply Last reply
    0
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

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


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3

      • Login

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