Issues with getter and setter methods
-
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!
-
@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!
-
@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.
-
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.
-
@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!
-
@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 (:
-
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++
-
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!
-
@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/TheChernoProjecthe'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 carefullyVoidRealms:
https://www.youtube.com/user/VoidRealmsHe'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/lefticus1For more advanced stuff and oddities in c++, :D
-
@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 (: