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. Issues with getter and setter methods
QtWS25 Last Chance

Issues with getter and setter methods

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 6 Posters 704 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.
  • ddryomyssD Offline
    ddryomyssD Offline
    ddryomyss
    wrote on last edited by
    #1

    Hello y'all!
    Yep, I'm kind of new to qt (and c++ also, basically), so I have issues with basic stuff.

    This code is supposed to create enumerated points on the screen. I described numeration (label creation) in MainWindow class and points creation in paintScene class. Maybe it wasn't the best solution, but it's the only one I came up with.

    I need to start this app work on KeyPressEvent (I chose Key "C"). So I need to pass info about the key press from one class to another. But it doesn't work and I have no idea why...

    Code:

    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();
    
    private:
        Ui::MainWindow *ui;
        QTimer *timer;
        paintScene *scene;
    
        void resizeEvent(QResizeEvent * event);
        void mousePressEvent(QMouseEvent *mpe);
    
    
        int j = 1;
        bool ind = true;
        bool start_num = false;
    
    
    private slots:
        void slotTimer();
    
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include "paintscene.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::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);
    }
    
    void MainWindow::mousePressEvent(QMouseEvent *mpe)
    {
        paintScene ps;
      //  start_num = ps.get();
        if (mpe->button() == Qt::LeftButton && ind == true && ps.get() == true)
            {
                QLabel *label = new QLabel(this);
                label->setText(QString("%1").arg(j));
                label->setGeometry(mpe->x()+10,mpe->y()-20,20,20);
                label->show();
                ++j;
                ui->statusbar->showMessage(QString("Coordinates: %1, %2").arg(mpe->x()).arg(mpe->y()));
            }
        else if (mpe->button() == Qt::RightButton && ind == true && ps.get() == true)
            {
                ui->statusbar->showMessage(QString("Coordinates: %1, %2").arg(mpe->x()).arg(mpe->y()));
                ind = false;
        }
    }
    

    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);
    
        bool get();
    
    private:
        void mousePressEvent(QGraphicsSceneMouseEvent * event);
    
    
        QVector<qreal> coord_x, coord_y, distances;
        bool ind = true;
        bool start_pt = false;
    
        const qreal rad = 5.0;
        qreal last_pt_x, last_pt_y;
        int i = 1;
    
    protected:
        void keyPressEvent(QKeyEvent *e);
    
    
    };
    
    #endif // PAINTSCENE_H
    
    

    paintscene.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    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);
    }
    
    bool paintScene::get()
    {
        return start_pt;
    }
    
    void paintScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        if (event->button() == Qt::LeftButton && ind == true && start_pt == true)
            {
            addEllipse(event->scenePos().x() - rad,
                       event->scenePos().y() - rad,
                       2*rad,
                       2*rad,
                       QPen(Qt::NoPen),
                       QBrush(Qt::red));
            ++i;
            addPoint(event->scenePos().x(), event->scenePos().y());
    
            }
        else if (event->button() == Qt::RightButton && ind == true && start_pt == 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();
        }
    }
    void paintScene::keyPressEvent(QKeyEvent *e)
    {
        if (e->key() == Qt::Key_S)
        {
            start_pt = true;
        }
    }
    
    

    Will be grateful for your feedback!

    1 Reply Last reply
    0
    • ddryomyssD ddryomyss

      @SGaist and so? I don't really get, why ps.get() do not return to the value of start_pt.

      If I will describe get() function like that:

      bool paintScene::get()
      {
          return true;
      }
      

      numeration will work. Where is the problem?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #5

      @ddryomyss you didn't read up on your basics, like I suggested in your other thread!

      What you did, is:

      You're in your house, and you create a box there. The box was empty, so you placed a banana in it.

      Then you went to your garden shed and leave your box behind. But now you're missing your box! So you create an other a box there, the same way you did in your house. Its also empty, but you're now wondering why there's no banana in!


      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.

      JonBJ ddryomyssD 3 Replies Last reply
      5
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @ddryomyss said in Issues with getter and setter methods:

        void MainWindow::mousePressEvent(QMouseEvent *mpe)
        {
        paintScene ps;

        ps is local to that function. Hence it's going to be empty and also destroyed a the end of the function.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        ddryomyssD 1 Reply Last reply
        1
        • SGaistS SGaist

          @ddryomyss said in Issues with getter and setter methods:

          void MainWindow::mousePressEvent(QMouseEvent *mpe)
          {
          paintScene ps;

          ps is local to that function. Hence it's going to be empty and also destroyed a the end of the function.

          ddryomyssD Offline
          ddryomyssD Offline
          ddryomyss
          wrote on last edited by
          #3

          @SGaist and so? I don't really get, why ps.get() do not return to the value of start_pt.

          If I will describe get() function like that:

          bool paintScene::get()
          {
              return true;
          }
          

          numeration will work. Where is the problem?

          J.HilkJ 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            The issue is your understanding of the variable scope. As I already said, you create a function local object of the type paintScene. You are getting the correct value since it's initialised to false.

            You should be using your class instance of paintScene.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3
            • ddryomyssD ddryomyss

              @SGaist and so? I don't really get, why ps.get() do not return to the value of start_pt.

              If I will describe get() function like that:

              bool paintScene::get()
              {
                  return true;
              }
              

              numeration will work. Where is the problem?

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #5

              @ddryomyss you didn't read up on your basics, like I suggested in your other thread!

              What you did, is:

              You're in your house, and you create a box there. The box was empty, so you placed a banana in it.

              Then you went to your garden shed and leave your box behind. But now you're missing your box! So you create an other a box there, the same way you did in your house. Its also empty, but you're now wondering why there's no banana in!


              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.

              JonBJ ddryomyssD 3 Replies Last reply
              5
              • J.HilkJ J.Hilk

                @ddryomyss you didn't read up on your basics, like I suggested in your other thread!

                What you did, is:

                You're in your house, and you create a box there. The box was empty, so you placed a banana in it.

                Then you went to your garden shed and leave your box behind. But now you're missing your box! So you create an other a box there, the same way you did in your house. Its also empty, but you're now wondering why there's no banana in!

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @J-Hilk
                Very good analogy! But would this apply to apples too, or is it banana-specific?

                J.HilkJ 1 Reply Last reply
                1
                • JonBJ JonB

                  @J-Hilk
                  Very good analogy! But would this apply to apples too, or is it banana-specific?

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #7

                  @JonB no, Bananas and Oranges only!

                  The solution btw, is to go the the window, rise your hand and point at your house and order your first box to come the f over :D


                  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
                  • J.HilkJ J.Hilk

                    @ddryomyss you didn't read up on your basics, like I suggested in your other thread!

                    What you did, is:

                    You're in your house, and you create a box there. The box was empty, so you placed a banana in it.

                    Then you went to your garden shed and leave your box behind. But now you're missing your box! So you create an other a box there, the same way you did in your house. Its also empty, but you're now wondering why there's no banana in!

                    ddryomyssD Offline
                    ddryomyssD Offline
                    ddryomyss
                    wrote on last edited by
                    #8

                    @J-Hilk It finally clicked in my head, thank you so much, it was such a nice analogy!

                    1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @ddryomyss you didn't read up on your basics, like I suggested in your other thread!

                      What you did, is:

                      You're in your house, and you create a box there. The box was empty, so you placed a banana in it.

                      Then you went to your garden shed and leave your box behind. But now you're missing your box! So you create an other a box there, the same way you did in your house. Its also empty, but you're now wondering why there's no banana in!

                      ddryomyssD Offline
                      ddryomyssD Offline
                      ddryomyss
                      wrote on last edited by
                      #9

                      @J-Hilk I know that I am weak with basics. I was kind of forced to jump right into practice, so now it hurts badly a lot of times (lack of basic knowledge, I mean). Maybe you have any recommendations on sources of info? Something like a clear study plan (maybe a book, or course of lectures). Or maybe there was a post here on this topic? I'll be grateful to hear any recommendations from you (:

                      J.HilkJ 1 Reply Last reply
                      2
                      • KH-219DesignK Offline
                        KH-219DesignK Offline
                        KH-219Design
                        wrote on last edited by
                        #10

                        If I may be so bold as to suddenly insert myself...

                        @ddryomyss It is refreshing and heartening to see you acknowledge your weaknesses. That takes a certain courage, and I commend you!

                        My workplace study-group (all full-time professional software and firmware engineers) recently brainstormed a book list. We found there was great overlap when we each listed the books that had had the biggest influence on each of us.

                        I'll paste a relevant excerpt here.

                        I'd like to explicitly note: some of the books from 10+ years ago are still extremely relevant! Don't automatically discount any of these due to publication date alone.

                        For the C++ language specifically, please choose from this other list: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

                        For widely-applicable software craft advice:

                        (2004) Head First Design Patterns

                        (2004) An Introduction to GCC <--- excellent for understanding how code turns into libraries and executables (whether or not you use GCC. the principles hold even on Windows in Visual Studio)

                        (2005) Effective C++ (definitely get the 3rd edition)

                        (2008) Clean Code

                        (2009) The Passionate Programmer

                        (2014) Effective Modern C++

                        www.219design.com
                        Software | Electrical | Mechanical | Product Design

                        ddryomyssD 1 Reply Last reply
                        1
                        • W Offline
                          W Offline
                          wrosecrans
                          wrote on last edited by
                          #11

                          The simplest explanation of scope I've seen for students is that a set of { ... } curly braces in C++ are like Las Vegas. What happens in curly braces stays in curly braces!

                          1 Reply Last reply
                          0
                          • ddryomyssD ddryomyss

                            @J-Hilk I know that I am weak with basics. I was kind of forced to jump right into practice, so now it hurts badly a lot of times (lack of basic knowledge, I mean). Maybe you have any recommendations on sources of info? Something like a clear study plan (maybe a book, or course of lectures). Or maybe there was a post here on this topic? I'll be grateful to hear any recommendations from you (:

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #12

                            @ddryomyss said in Issues with getter and setter methods:

                            @J-Hilk I know that I am weak with basics. I was kind of forced to jump right into practice, so now it hurts badly a lot of times (lack of basic knowledge, I mean).

                            Don't worry, I started in a very similar fashion myself, I can relate.
                            As long as you're hopeful, polite and most importantly willing to improve yourself, I'm sure no-one here will refuse to aide you!

                            Maybe you have any recommendations on sources of info? Something like a clear study plan (maybe a book, or course of lectures). Or maybe there was a post here on this topic? I'll be grateful to hear any recommendations from you (:

                            Well, I can only tell you from my personal experience.
                            I learn the more from videos than from books. So I've a couple of YT channels, that I follow that over the years published a hand full of tutorials:

                            I can recommend the following:
                            The Cherno:
                            https://www.youtube.com/c/TheChernoProject

                            he's a previous (c++)Game Developer and has a playlist about the basics (still ongoing) an OpenGL tutorial playlist and a GameEngine developer playlist.
                            So it varies between beginner level entry and advanced, pick your poison carefully

                            VoidRealms:
                            https://www.youtube.com/user/VoidRealms

                            He's also a (somewhat) active forum member, he was one of my first Qt related sources for me.
                            Also has introductions to C++ and other Languages, and last I heard he has udemy courses to offer (never checked them out myself!)

                            Cᐩᐩ Weekly:
                            https://www.youtube.com/user/lefticus1

                            For more advanced stuff and oddities in c++, :D


                            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
                            • KH-219DesignK KH-219Design

                              If I may be so bold as to suddenly insert myself...

                              @ddryomyss It is refreshing and heartening to see you acknowledge your weaknesses. That takes a certain courage, and I commend you!

                              My workplace study-group (all full-time professional software and firmware engineers) recently brainstormed a book list. We found there was great overlap when we each listed the books that had had the biggest influence on each of us.

                              I'll paste a relevant excerpt here.

                              I'd like to explicitly note: some of the books from 10+ years ago are still extremely relevant! Don't automatically discount any of these due to publication date alone.

                              For the C++ language specifically, please choose from this other list: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

                              For widely-applicable software craft advice:

                              (2004) Head First Design Patterns

                              (2004) An Introduction to GCC <--- excellent for understanding how code turns into libraries and executables (whether or not you use GCC. the principles hold even on Windows in Visual Studio)

                              (2005) Effective C++ (definitely get the 3rd edition)

                              (2008) Clean Code

                              (2009) The Passionate Programmer

                              (2014) Effective Modern C++

                              ddryomyssD Offline
                              ddryomyssD Offline
                              ddryomyss
                              wrote on last edited by
                              #13

                              @KH-219Design @J-Hilk I'm truly amazed by your willingness to share your experience with me! I'll try to make maximum use of your recommendations as soon as I can, huuuge thank you (:

                              1 Reply Last reply
                              2

                              • Login

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