PDA

View Full Version : Pointer to another window.



NoOne123
11th March 2018, 01:08
How to return a pointer, to another window?

If i just return, for example


return Pointer_name;

Then change something in another window,it doesn't affect the orginal one, when i change anything, it seams like another copy.

d_stranz
11th March 2018, 16:26
You need to provide some more information. A single line of code with no context at all doesn't give us anything to go on.

Please post some code that shows what you are trying to do, especially the code that returns the pointer, and where you are trying to use the pointer and "it doesn't affect the original one".

NoOne123
12th March 2018, 07:25
Ther is nothing to show. I wanna return one pointer, to another window thats it.

So for example Window_A_PTR in a part of MainWindow.
I wanna return and use that pointer to Window_B.
So:

Window_A* Window_A::Return_Pointer()
{

return Window_A_PTR;

}

Now in window B:

//Wher i wanna use it
{

Window_A* Temp = new Window_A;
Window_A Get_The_Pointer;

Get_The_Pointer = Temp->Return_Pointer();

//Then use that "Get_The_Pointer" and do some changes in Window_B, but it should affect Window_A_PTR, cuz it's the orginal one.

}

high_flyer
12th March 2018, 09:52
Why are you breaking your English?
If you want to be taken seriously use proper language.

Your pseudo code does what you want in terms of returning the pointer.
The problem you have is therefore not clear.
Its seems to me your last comment may be pointing to the problem which may be the scope of usage.
To be sure however the real code where you use the pointer getter method is needed.

Show the place in code that doe NOT behave like you intend it to maybe this will brings us to the right path.
And please use code tags when posting code.

NoOne123
12th March 2018, 13:33
[text removed due to breach of forum rules]

Added after 7 minutes:

Ther is an example, if something is "not clear" ask.

http://www.mediafire.com/file/9jaaaifl3ga6yg6/Sending_Window_Pointer.rar

high_flyer
12th March 2018, 17:28
The language you used in your last post is not tolerated here.
This time you are only warned, and I have removed your offending text.
Next time you will be blocked.

I was not referring to your English mistakes, but to you breaking it on purpose.
Wririting 'cus' instead of 'because' (and other such examples) is not a mistake, its on purpose.
It has nothing to do with how good your actual English is.
Like many here, I am also not a native English speaker, and I probably do make mistakes.
Mistakes are ok, but try to understand that it makes it hard to read your post when you mutilate the language on purpose.
There is no limit on the amount of characters you can use in a post, so no need to use sms short hand.


Ther is an example, if something is "not clear" ask.
Do you really expect help with that attitude??

d_stranz
13th March 2018, 03:11
Window_A* Window_A::Return_Pointer()
{

return Window_A_PTR;

}

Now in window B:

//Wher i wanna use it
{

Window_A* Temp = new Window_A;
Window_A Get_The_Pointer;

Get_The_Pointer = Temp->Return_Pointer();

//Then use that "Get_The_Pointer" and do some changes in Window_B, but it should affect Window_A_PTR, cuz it's the orginal one.

}

So what exactly do you expect this code to do? If as you say, there is a pointer to a Window_A instance in your MainWindow class, then why does your code show the Return_Pointer() method as a member of the Window_A class? Where is this "Window_A_PTR" stored?

In your use of the method in Window_B, you are creating a new instance of Window_A. This is not the same instance as the one in MainWindow, so the pointer it returns and assigns to "Get_The_Pointer" will not be the one for the MainWindow instance, and any changes you make using that pointer will not affect the instance in MainWindow.



To back up what high_flyer said: We are all volunteering our time on this forum to try to help people who are trying to learn Qt and are having trouble with it. We do it because we want to help spread Qt and get more people comfortable using it.

If you ask for help here, we expect you to give us more than a few lines of crap pseudocode that may or may not look anything like your actual code. Based on what you have posted, I am only guessing at what you might be doing wrong, but what you have posted really is just a few lines that mean almost nothing in terms of being able to give you any useful help.

Help which, despite your apparent rudeness which I didn't see before it was deleted, I am still willing to give you.

NoOne123
13th March 2018, 07:17
Listen, i am a little bit mad, cuz it's long time when i am trying to do that.

Window_A_PTR is stored in MainWindow.h, in the public section. A Get_The_Pointer, is returning that "Window_A_PTR" to Window_B.

Then i wanna create new member, and use ther the returned pointer.

So it should be something like:


Window_A* Temporary_Object->Get_The_Pointer.

Something like that, i wanna use it in Window_B, but it's not working with QT's window's.
If i do the same thing with my own class, it's working.

d_stranz
13th March 2018, 15:57
Window_A_PTR is stored in MainWindow.h

OK, then why did you post this code:



Window_A* Window_A::Return_Pointer()
{
return Window_A_PTR;
}


This clearly shows "Window_A_PTR" being returned from a method in the "Window_A" class, not "MainWindow". Even if this pointer was a public member variable of MainWindow, this code is not referring to that variable.

Why are you so resistant to posting code that actually helps us to solve your problem?

You asked for help here, we asked you for more information, yet all you do is repeat the same sentences without showing any real code. Obviously, your code is not doing what you describe, but if you don't post any real code where we can point to a line and say "here is the mistake", how are we going to give you any advice?

What you are doing is about the same as writing, "My car won't start" and posting a photo of your car. Or your friend's car. If you opened the hood and showed us that the battery was missing, we could point to that and say "Here's the problem".

NoOne123
14th March 2018, 05:00
Like i said, here is everything you need:

http://www.mediafire.com/file/9jaaaifl3ga6yg6/Sending_Window_Pointer.rar

I don't think, if i just place here that 4-8 files, it's gonna be better, than just running QT, downloading the files, and open the project file.

WINDOW_002.h


#ifndef WINDOW_002_H
#define WINDOW_002_H

#include <QWidget>

namespace Ui {
class Window_002;
}

class Window_002 : public QWidget
{
Q_OBJECT

public:
explicit Window_002(QWidget *parent = 0);
~Window_002();
friend class MainWindow;
friend class window_003;

private slots:
void on_Window_002_Set_Text_Button_clicked();

private:
Ui::Window_002 *ui;
};

#endif // WINDOW_002_H




MAIN_WINDOW.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "window_002.h"
#include "window_003.h"


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Window_002 window_002;
Window_002* window_002_PTR = &window_002;
Window_002* Return_Pointer();
window_003 window_003__;
window_003* window_003_PTR = &window_003__;


private slots:
void on_Open_Window_Button_clicked();

void on_Set_Text_Button_clicked();

void on_Open_Window_Three_Button_clicked();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H






MAIN_WINDOW



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_window_002.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

Window_002 *MainWindow::Return_Pointer()
{
return window_002_PTR;
}

void MainWindow::on_Open_Window_Button_clicked()
{
window_002_PTR->activateWindow();
window_002_PTR->show();
}

void MainWindow::on_Set_Text_Button_clicked()
{
window_002_PTR->ui->Test_LineEdit->setText("This is from MainWIndow");
}

void MainWindow::on_Open_Window_Three_Button_clicked()
{
window_003_PTR->activateWindow();
window_003_PTR->show();
}




Here i want to use that pointer, and affecr the orginal window, so in this example i wanna set the text from window 3, to window 2.
This is not working... If i press the button, nothing gonna happend, setText from MainWIndow to Window_2 is working, cuz ther is created the Window_2 object, but i need to do that from Window_3.

I know how to use signals and slots, but i prefer to do it just by pointer. (If ther is any way.)

WINDOW 3



#include "window_003.h"
#include "ui_window_003.h"
#include "mainwindow.h"
#include "window_002.h"
#include "ui_window_002.h"

window_003::window_003(QWidget *parent) :
QWidget(parent),
ui(new Ui::window_003)
{
ui->setupUi(this);
}

window_003::~window_003()
{
delete ui;
}

void window_003::on_Window_003_Set_Text_Button_clicked( )
{
MainWindow MW;
MainWindow* MW_PTR = &MW;
Window_002* Window_Pointer;
Window_Pointer = MW_PTR->Return_Pointer();
Window_Pointer->ui->Test_LineEdit->setText("This is from window 003");
}





That old example, should looks like this:
It's created in MainWindow, i make a mistake ther.


Window_A* MainWindow::Return_Pointer()
{
return Window_A_PTR;
}

Ginsengelf
14th March 2018, 07:58
I know how to use signals and slots, but i prefer to do it just by pointer. (If ther is any way.)
But why? Your way needs friend declarations and the different widgets need knowledge about the internals of other widgets. You could remove both by using signals and slots.

Ginsengelf

NoOne123
14th March 2018, 09:32
"But why? Your way needs friend declarations and the different widgets need knowledge about the internals of other widgets. You could remove both by using signals and slots. Ginsengelf "


Ye, and add 1000000000000000000000000000000000000000000000000 milions of milions functions to read one value, no, thanks.

Any arguments why i shouldn't let the others, to know that (When it's needed) ? Or it's just a "Professional programmers" tradition?

Ps. I wanna learn the other way / way's, and then judge what is better, for me, not for you.

d_stranz
14th March 2018, 16:03
Ps. I wanna learn the other way / way's, and then judge what is better, for me, not for you.

Wow, you really have a problem with accepting advice, don't you? I looked at your code and I have the same reaction as Ginsengelf: I would never write such code, and since Qt gives you the very powerful model of signals and slots to handle exactly the kind of thing you are trying to do with friends and pointers and references, it would help you to learn about how to use them so you can write better code.

It is also pretty clear that you don't have a lot of understanding of C++, scope, and pointers. Instead of having pointers and references everywhere, you can replace all of that with one line of code:



Window_002 *MainWindow::Return_Pointer()
{
return &window_002;
}


Not that this is a good idea. It's actually a bad idea to ever expose member variables of one class to the outside world. It's even a worse idea to have chains of exposed member variables like this: window_002_PTR->ui->Test_LineEdit->setText("This is from MainWIndow");

And yes, it absolutely is a "professional programmer" thing. It is something you learn as you teach yourself to write better code and understand why the code you wrote when you were first learning wasn't as good as it could be. When you show your code to a professional programmer (or when you want to get a job as a professional programmer), what would you prefer, a reaction like Gingsengelf and I have, or a comment like, "Yeah, you really know your stuff, you're hired"?

Anyway, now that you have opened the hood of the car, I can tell you that the problem is here:



void window_003::on_Window_003_Set_Text_Button_clicked( )
{
MainWindow MW;
MainWindow* MW_PTR = &MW;
Window_002* Window_Pointer;
Window_Pointer = MW_PTR->Return_Pointer();
Window_Pointer->ui->Test_LineEdit->setText("This is from window 003");
}


The MainWindow instance you create on the stack in the first line of this method is different and not the same instance as you create in main(). And because you declare an instance of Window_002 as a member variable of MainWindow, that Window_002 instance is not the same as the instance in the MainWindow you create in main(). So when you retrieve the Window_002 pointer from the new instance of MainWindow and then modify the text, you are modifying the temporary instance of Window_002 you retrieve from the temporary instance of MainWindow.

None of these temporary instances is related in any way to the instances you created through main(). Once this method exits, all those temporary variables go away.

I think it would help you a lot if you studied some of the many Qt examples that came with your Qt distribution. The Qt Tutorial for Beginners (https://wiki.qt.io/Qt_for_Beginners) covers most of the important things you will need to be able to write good C++ programs using Qt.

It will be worth you while to work your way through that, because even though you probably won't admit it, your code shows that you are a beginner. We all were at some point.

NoOne123
15th March 2018, 03:50
Ok, any exact solution, how to get that pointer?

Ps. "Not that this is a good idea." Argument's, in this case, i am not intrested in your opinion. Wher are the argument's? Otherwise it's just a tradition, if you dont do that you are wrong (No argument's why). Stupid tradition at that point, and i rly hate thinking like that "You have to do that, because you have to do that".

I create this topic, to know how to get the pointer,and work on orginal object, but as a secondary thing, you can show your agrument's, why exposing variables when it's needed, is a bad idea, cuz at this point, i read ther something like " It's like that, cuz it's like that" << And this is the way, how you sholud work.
What that mean?? I am sure we can talk about that, but first let me know finnaly HOW to get that pointer, i am waiting a lot of time, i tried to find any tutorials, but nothing helped.


If you are that professional, for you it's 2 minutes, and it's all done, so any help??
Just a simple example. Btw, if someone sak you, for a glass of coca-cola, dont give to him a pice of ham ,because you think it's better for him.
I repeat, i wanna get that pointer, not learn "professional programming".

high_flyer
15th March 2018, 12:38
Just a simple example. Btw, if someone sak you, for a glass of coca-cola, dont give to him a pice of ham ,because you think it's better for him.
That is wrong analogy.
The correct one is:
You are hungry - and  d_stranz can give you a fish, or, teach you how to fish so you wont go hungry again.
Looking at his lengthy answers to you, it clear he invested time and thought about them.
You can at least be respectful with your tone to him.


but as a secondary thing, you can show your agrument's, why exposing variables when it's needed, is a bad idea, cuz at this point, i read ther something like " It's like that, cuz it's like that" << And this is the way, how you sholud work.
The scope of a forum is not meant for that.
You need to learn OOP and C++ and software design to fully understand the reasoning behind it.
In a nutshell, it about dependency of code.
When you expose a pointer the way you are doing it, you are hard binding the class exposing it with the calling code.
In real world proffetional code, which can get very big and complicatred, this will cause problems when introducing changes as it will force a changes not only in the place you want to bring them in, but force all the calling code to be adjusted as well.
That's is one of many other problems with your design.
Read and learn about good software design and you will understand why what d_stranz said about not exposing your pointer is correct.

You wanted arguments here is one:
https://www.codesdope.com/cpp-encapsulation/

google for many more.

NoOne123
15th March 2018, 12:53
I am not intresetd in signals and slots, i wanna know how to get the pointer to work, and the analogy is 100% accurate, i don't want them, and you are trying to give it to me, signals and slot's are not the subject of this topic.

If you wanna speak about that, message me in private, but like i said, i am not interested in them, i wanna know how to get that pointer.

high_flyer
15th March 2018, 12:55
d_stranz already answered you about that.

NoOne123
15th March 2018, 13:01
Sorry, but i dont see the solution.

Ginsengelf
15th March 2018, 13:11
Check d_stranz's second code block in comment #13 and the text below it. It explains your problem.

Ginsengelf

NoOne123
15th March 2018, 15:04
I dont know how i do that, but i fix my problem.... FINALLY!! I was waiting too much, damn it, highly too much.

This solve my big problem:




nMainWindow* WN;

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QApplication::setStyle("fusion");

WN = new nMainWindow;
WN->Return()->activateWindow();
WN->Return()->execOnStart();
WN->Return()->show();

return a.exec();
}



I create one "Main class", inherit it from QWidget i think, add as a member MainWindow, and return it.
Yes, WN is a global pointer object, i don't like it too,but i fix two problems in one time, now i can (hopefull) use signals and slot's with that MainWindow member.
In the old situation, i like to use signals and slots with MainWindow object, but i couldn't find any way to get the pointer from main.

Everyone gonna say, it is bad what i am doing, but i am really happy, and i don't care, finally i fix it.

d_stranz
15th March 2018, 16:07
Everyone gonna say, it is bad what i am doing, but i am really happy, and i don't care, finally i fix it.

Great. I wish you success in your coding career, but I would suggest you don't make that statement when you are interviewing for a job.

As high_flyer said, trying to offer you anything more than a plate full of fish already cleaned and cooked exactly the way you like it seems to be a waste of everyone's time. We aren't here to catch and cook fish for you, we're here to teach people to fish by helping them become better Qt software developers. Maybe you will find another forum where people will help you write bad code, but I don't think you will find those people here.

NoOne123
15th March 2018, 17:05
If you first show me, what i want, and then you will speak about the right way, that's good, but otherwise that's horrific.
Don't do it, because someone can think "you are just stupid, you dont know something, that's why you are "forcing" someone to do it in another way".

The topic was clear, how to do it by pointer, not by "right way" or "professional way".
If someone ask you for something, first you respond to the topic to show what he wants, then when he knows it already, you can show the other way, otherwise it's just stupid, it's like buying two cars, when you can't afford to buy any of them.
First you buy first car, and then the second when you can afford it, something with programming.


PS. I am not gonna work for anybody in my life, that's why i rly don't care much.

high_flyer
16th March 2018, 10:27
because someone can think "you are just stupid, you dont know something, that's why you are "forcing" someone to do it in another way".
Just because someone may think that, it is not a reason not to do the right thing - which is to point you to the actual problem you have - not the one you THINK you have.

A junkie thinks his problem is getting drugs, and he will be mad at you if you tell him the next ration of drugs is not what he needs but that he needs to go to rehab, even though he thinks differently it is obvious the rehab suggestion is the right one, and not to bring him his next drug ration.

NoOne123
17th March 2018, 09:30
I know how much time i waste, to find the way of doing that, i am sure ther is no need to keep trying to help, in your way, because you think it's good and if someone don't want it.
Anyway i fix it finnaly, and we can close this topic at this point.

d_stranz
17th March 2018, 16:00
Yes, we can close the topic. I think the reason it has gone on as long as it has is that everyone who has responded has thought, "Wow, this guy is heading for a train wreck. We have to help him out". Yes, you may have been clear in stating your topic, but to give you that answer meant we would simply be pushing harder on the pedal to speed up the train.

I think it makes all of us sad to read your replies, because what we see is a stubborn person who would rather use a hammer to pound screws into wood, and who insults and argues with people who tell him maybe a screwdriver would be a better tool. You have the hammer in your hand already, and you aren't going to put it down.

If you don't take any pride in writing good code, are willing to spend weeks trying to understand why your programs won't work or why they crash, and insist that you don't care about alternatives because you've already decided what you want, well, good for you. Eventually you will get to the limit of your ability and you will be stuck in the bad habits you are developing now because you've refused to learn anything new. Good luck with it.