Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Pointer to another window.

  1. #1

    Default Pointer to another window.

    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Pointer to another window.

    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".
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3

    Default Re: Pointer to another window.

    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.

    }

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Pointer to another window.

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5

    Default Re: Pointer to another window.

    [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/9jaaai...ow_Pointer.rar
    Last edited by high_flyer; 12th March 2018 at 18:21.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Pointer to another window.

    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??
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Pointer to another window.

    Qt Code:
    1. Window_A* Window_A::Return_Pointer()
    2. {
    3.  
    4. return Window_A_PTR;
    5.  
    6. }
    7.  
    8. Now in window B:
    9.  
    10. //Wher i wanna use it
    11. {
    12.  
    13. Window_A* Temp = new Window_A;
    14. Window_A Get_The_Pointer;
    15.  
    16. Get_The_Pointer = Temp->Return_Pointer();
    17.  
    18. //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.
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #8

    Default Re: Pointer to another window.

    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.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Pointer to another window.

    Window_A_PTR is stored in MainWindow.h
    OK, then why did you post this code:

    Qt Code:
    1. Window_A* Window_A::Return_Pointer()
    2. {
    3. return Window_A_PTR;
    4. }
    To copy to clipboard, switch view to plain text mode 

    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".
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. #10

    Default Re: Pointer to another window.

    Like i said, here is everything you need:

    http://www.mediafire.com/file/9jaaai...ow_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
    Qt Code:
    1. #ifndef WINDOW_002_H
    2. #define WINDOW_002_H
    3.  
    4. #include <QWidget>
    5.  
    6. namespace Ui {
    7. class Window_002;
    8. }
    9.  
    10. class Window_002 : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit Window_002(QWidget *parent = 0);
    16. ~Window_002();
    17. friend class MainWindow;
    18. friend class window_003;
    19.  
    20. private slots:
    21. void on_Window_002_Set_Text_Button_clicked();
    22.  
    23. private:
    24. Ui::Window_002 *ui;
    25. };
    26.  
    27. #endif // WINDOW_002_H
    To copy to clipboard, switch view to plain text mode 


    MAIN_WINDOW.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "window_002.h"
    6. #include "window_003.h"
    7.  
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20. Window_002 window_002;
    21. Window_002* window_002_PTR = &window_002;
    22. Window_002* Return_Pointer();
    23. window_003 window_003__;
    24. window_003* window_003_PTR = &window_003__;
    25.  
    26.  
    27. private slots:
    28. void on_Open_Window_Button_clicked();
    29.  
    30. void on_Set_Text_Button_clicked();
    31.  
    32. void on_Open_Window_Three_Button_clicked();
    33.  
    34. private:
    35. Ui::MainWindow *ui;
    36. };
    37.  
    38. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 




    MAIN_WINDOW
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "ui_window_002.h"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. Window_002 *MainWindow::Return_Pointer()
    18. {
    19. return window_002_PTR;
    20. }
    21.  
    22. void MainWindow::on_Open_Window_Button_clicked()
    23. {
    24. window_002_PTR->activateWindow();
    25. window_002_PTR->show();
    26. }
    27.  
    28. void MainWindow::on_Set_Text_Button_clicked()
    29. {
    30. window_002_PTR->ui->Test_LineEdit->setText("This is from MainWIndow");
    31. }
    32.  
    33. void MainWindow::on_Open_Window_Three_Button_clicked()
    34. {
    35. window_003_PTR->activateWindow();
    36. window_003_PTR->show();
    37. }
    To copy to clipboard, switch view to plain text mode 


    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
    Qt Code:
    1. #include "window_003.h"
    2. #include "ui_window_003.h"
    3. #include "mainwindow.h"
    4. #include "window_002.h"
    5. #include "ui_window_002.h"
    6.  
    7. window_003::window_003(QWidget *parent) :
    8. QWidget(parent),
    9. ui(new Ui::window_003)
    10. {
    11. ui->setupUi(this);
    12. }
    13.  
    14. window_003::~window_003()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void window_003::on_Window_003_Set_Text_Button_clicked()
    20. {
    21. MainWindow MW;
    22. MainWindow* MW_PTR = &MW;
    23. Window_002* Window_Pointer;
    24. Window_Pointer = MW_PTR->Return_Pointer();
    25. Window_Pointer->ui->Test_LineEdit->setText("This is from window 003");
    26. }
    To copy to clipboard, switch view to plain text mode 



    That old example, should looks like this:
    It's created in MainWindow, i make a mistake ther.
    Qt Code:
    1. Window_A* MainWindow::Return_Pointer()
    2. {
    3. return Window_A_PTR;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by NoOne123; 14th March 2018 at 06:20.

  11. #11
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pointer to another window.

    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

  12. #12

    Default Re: Pointer to another window.

    "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.
    Last edited by NoOne123; 14th March 2018 at 10:42.

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Pointer to another window.

    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:

    Qt Code:
    1. Window_002 *MainWindow::Return_Pointer()
    2. {
    3. return &window_002;
    4. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. void window_003::on_Window_003_Set_Text_Button_clicked()
    2. {
    3. MainWindow MW;
    4. MainWindow* MW_PTR = &MW;
    5. Window_002* Window_Pointer;
    6. Window_Pointer = MW_PTR->Return_Pointer();
    7. Window_Pointer->ui->Test_LineEdit->setText("This is from window 003");
    8. }
    To copy to clipboard, switch view to plain text mode 

    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 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.
    Last edited by d_stranz; 14th March 2018 at 17:10.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  14. #14

    Default Re: Pointer to another window.

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

  15. #15
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Pointer to another window.

    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.
    Last edited by high_flyer; 15th March 2018 at 13:51.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  16. #16

    Default Re: Pointer to another window.

    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.

  17. #17
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Pointer to another window.

    d_stranz already answered you about that.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  18. #18

    Default Re: Pointer to another window.

    Sorry, but i dont see the solution.

  19. #19
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pointer to another window.

    Check d_stranz's second code block in comment #13 and the text below it. It explains your problem.

    Ginsengelf

  20. #20

    Default Re: Pointer to another window.

    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:

    Qt Code:
    1. nMainWindow* WN;
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. QApplication::setStyle("fusion");
    7.  
    8. WN = new nMainWindow;
    9. WN->Return()->activateWindow();
    10. WN->Return()->execOnStart();
    11. WN->Return()->show();
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. Replies: 2
    Last Post: 14th October 2016, 11:55
  2. Replies: 3
    Last Post: 23rd May 2015, 14:57
  3. Replies: 6
    Last Post: 9th November 2011, 05:31
  4. Replies: 2
    Last Post: 17th February 2011, 13:30
  5. Replies: 1
    Last Post: 4th December 2010, 18:20

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.