Results 1 to 7 of 7

Thread: MVC problem parameter handover

  1. #1
    Join Date
    Jan 2016
    Location
    Oberhausen, Germany
    Posts
    5
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default MVC problem parameter handover

    Hello forum

    I tries for the first time at the Model View Controller programming.

    I create three classes one for the logic, one for the Gui
    and one for the controlling.

    But i have a problem by the parameter handover in the logic class.

    The application crashed after click button.

    My test programm.

    Header:

    Qt Code:
    1. #ifndef LOGIC_H
    2. #define LOGIC_H
    3.  
    4. #include <QDebug>
    5. #include "gui.h"
    6.  
    7. class Gui;
    8.  
    9. class logic
    10. {
    11.  
    12. Gui *testgui;
    13.  
    14. public:
    15. logic(Gui *myGui);
    16.  
    17. void writeTEST();
    18. };
    19.  
    20. #endif // LOGIC_H
    To copy to clipboard, switch view to plain text mode 

    CPP:

    Qt Code:
    1. #include "logic.h"
    2.  
    3.  
    4. logic::logic(Gui *myGui)
    5. {
    6. testgui = myGui;
    7. }
    8.  
    9. void logic::writeTEST()
    10. {
    11. testgui->testTE->setText("test"); //application crashed
    12. //testgui->testTE->setText("test"); //application run if it comment out
    13. }
    To copy to clipboard, switch view to plain text mode 


    Since the proplem is unmistakable by the function call exist.
    I don't to see the wood for the trees.


    I hope anybody can help me.

    I do apologize for my bad english

    best regards

    Basti1990
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: MVC problem parameter handover

    Where do you create the logic object?
    Do you pass a valid Gui object to its constructor?

    Cheers,
    _

  3. #3
    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: MVC problem parameter handover

    In your code, you do not create an instance of a "logic" object anywhere. You declare a pointer to a logic instance in a variable named "l", but it is never initialized. Likewise, since you never create a logic class instance, the "testgui" member variable is never assigned to anything. So you make a call through an uninitialized pointer to a logic class member function, which then tries to make another call through an uninitialized pointer to a GUI class instance.

    When you comment out the line you indicated, it is only an accident that your program doesn't crash there, because the "l" pointer doesn't point to anything real (it is uninitialized), but then it isn't executing any code either. When you uncomment that line, your program crashes because now you are trying to use another bad pointer to do something and that's more than the runtime can deal with.
    <=== 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.

  4. The following user says thank you to d_stranz for this useful post:

    Basti1990 (24th September 2016)

  5. #4
    Join Date
    Jan 2016
    Location
    Oberhausen, Germany
    Posts
    5
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: MVC problem parameter handover

    hello,

    thanks for the fast replies.

    Now i know what i must to do on weekend.

    I'll report if i have solved my problem.

    Best regards

    Basti1990

  6. #5
    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: MVC problem parameter handover

    While you are learning this weekend, you should also read about "encapsulation". You have made your QPushButton and other GUI elements "public" members of your GUI class, and you then access them in another class. It is a bad design to expose member variables as public. They should be private, and if you need access to their settings, you provide member functions (or Qt slots and signals) so you can set their value or be notified when the value changes.

    It is hard to implement pure MVC using Qt, because the View and Controller parts are usually combined into a class combined from QWidget. For example, QPushButton has a visual appearance on screen (the View part) but also has a Controller part because it handles mouse and keyboard events internally.

    However, Qt's slots and signals -do- give you a way to implement MVC at a higher level. Your QWidget classes can provide you with abstractions - for example, QFileDialog has a signal that tells you when a file has been selected. You don't really care how QFileDialog does that on the inside (for example, by selecting a file from a list and then clicking an OK button), what your app cares about is that the user chose a file to open. By implementing a slot that responds to QFileDialog's signal, you can get the name of the file and open it. You could also completely replace QFileDialog with something else, as long as the new widget emits a signal that you can connect to the same slot.

    So in your case, you should not create a "logic" object that requires a GUI pointer in order to work. Instead, "logic" should have signals and slots that can be connected (in main() or QMainWindow, or somewhere else) to slots and signals of some other classes. "logic" does not care what those classes are, just that when it receives a signal to do something from a class it is connected to, it does the right thing. Likewise, when "logic" changes its state, it should emit a signal that says so. It doesn't care if anyone is listening, it just does what it is supposed to do.
    <=== 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.

  7. The following user says thank you to d_stranz for this useful post:

    Basti1990 (24th September 2016)

  8. #6
    Join Date
    Jan 2016
    Location
    Oberhausen, Germany
    Posts
    5
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: MVC problem parameter handover

    hello d_stranz,

    thanks for your reply.
    The Information are very interessting.

    You mean i should remove the MVC class
    and integrate it on Gui and Logic class.

    I'll splite the MVC class
    and integrated it on the both another classes.

    Like the picture:

    SpliteMVC.jpg

    Or mean you another thing.

    best regards

    Basti1990


    Ps. The membervariable is only public for testing.
    Last edited by Basti1990; 24th September 2016 at 14:54.

  9. #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: MVC problem parameter handover

    Like the picture
    Yes, I think that in general this is the way many Qt programs are implemented. The logic rules are separate from the GUI, and each side "talks" to the other side using signals and slots.

    It helps to make names for signals and slots that have a meaning for the way they are used. For example, you might write a signal for the GUI side called "buttonXClicked()". On the logic side, you write a slot called "performActionX()". (And not called "onButtonXClicked()"). The logic side does not care what happened on the GUI side, it just knows that something wants it to execute "actionX". Likewise, on the GUI side, it does not know what will happen when the user clicks "buttonX", it just knows that when it happens, it need to emit the signal.

    This is the way many parts of Qt work; in the Item / Views architecture, the "logic" part is a QAbstractItemModel. It stores data and emits certain signals when the data changes. On the GUI side is the QAbstractItemView. It displays the contents of the model, and will change the display in response to the model's signals.
    <=== 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. The following user says thank you to d_stranz for this useful post:

    Basti1990 (24th September 2016)

Similar Threads

  1. QAxObject, problem with VARIANT* parameter
    By jack_0x666 in forum Qt Programming
    Replies: 0
    Last Post: 10th June 2015, 13:41
  2. Replies: 3
    Last Post: 12th March 2012, 01:45
  3. Sql problem - parameter mismatch count
    By Marina K. in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2011, 18:27
  4. application parameter problem..
    By triperzonak in forum Newbie
    Replies: 7
    Last Post: 9th February 2009, 01:56
  5. Problem with unused parameter
    By devilj in forum Qt Programming
    Replies: 5
    Last Post: 4th July 2007, 18:48

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.