Results 1 to 4 of 4

Thread: QT Class GUI Interactions??

  1. #1
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy QT Class GUI Interactions??

    This is a question that I can't seem to figure out for some reason I don't know why but...

    I have a main class which includes the header files of all the other classes and in the cpp of the main class it goes something like this...

    Qt Code:
    1. MainClass::MainClass(QWidget *parent) : QWidget(parent), mainWindow(new Ui::MainClass)
    2. {
    3. mainWindow->setupUi(this);
    4.  
    5. classTwo = new SecondClass(this);
    6. classThree = new ThirdClass(this);
    7. }
    To copy to clipboard, switch view to plain text mode 

    but in classTwo for instance I want to disable a button in classThree. Now I can do this through the mainClass say like this...

    Qt Code:
    1. connect(classTwo->classTwoGui->disableClassThree_Button, SIGNAL(clicked()), this, SLOT(disableButtonInClassThree()));
    To copy to clipboard, switch view to plain text mode 

    where in the above example disableButtonInClassThree() would be a member function of the main class and the function defined in the MainClass cpp would probably be...

    Qt Code:
    1. void MainClass::disableButtonInClassThree()
    2. {
    3. classThree->classThreeGui->buttonOne->setEnabled(false);
    4. }
    To copy to clipboard, switch view to plain text mode 

    the problem is that I don't want to go through MainClass as I'd imagine that is very inefficient and creates a cluttered clumsy MainClass when using way more than 2 other classes in interaction so I talked to a couple friends and they told me to try this...

    In the SecondClass header I have...

    Qt Code:
    1. #include "thirdclass.h"
    2.  
    3. class SecondClass : public QMainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. SecondClass (ThirdClass *test, QWidget *parent = 0);
    9. Ui::SecondClass *classTwoGui;
    10.  
    11. public slots:
    12. void on_disableClassThree_Button_clicked();
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    In the SecondClass cpp I have...

    Qt Code:
    1. SecondClass::SecondClass (ThirdClass *test, QWidget *parent) : QMainWindow(parent), classTwoGui(new Ui::SecondClass )
    2. {
    3. classTwoGui->setupUi(this);
    4.  
    5. test->classThreeGui->buttonOne->setEnabled(false);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Now the above code works BUT it only works in the constructor of SecondClass so essentially meaning I can't really control it through a button clicked in SecondClass because doing any code outside of the constructor is not recognizing test.

    I have thus researched all over and found nothing to help, I have tried forward class declarations but that's not my problem I believe and that didn't work either, I have also tried taking it out of the constructor and initializing a pointer of the ThirdClass below the constructor such as...

    Qt Code:
    1. public:
    2. SecondClass (QWidget *parent = 0);
    3. ThirdClass *test,
    4. Ui::SecondClass *classTwoGui;
    To copy to clipboard, switch view to plain text mode 

    which could then be accessed in any function inside of the SecondClass but then it caused an instant crash upon launching I believe due to the fact that in the MainClass it is also being initialized too??

    I almost forgot to mention that in order for me to get the constructor working working I had to change...classTwo = new SecondClass(this); in the MainClass constructor to...

    Qt Code:
    1. classTwo = new SecondClass(classThree, this);
    To copy to clipboard, switch view to plain text mode 

    I hope someone can help me as I'm really struggling with this part and I have had no success for awhile although I can still get it to work the inefficient way by going through the MainClass but I'd rather not due to the reasons above so simply put...

    I need to access GUI objects, such as a button, from a class using another class without going through the MainClass.

    Final Example to hopefully clear things up is...

    Clicking a button in a second class GUI will disable a button in a third class GUI without going through the main class, essentially have the second class somehow recognize GUI objects of the third class in any section of the second class so any member function of the second class can interact with objects of the third class.

    I hope this all makes sense I tried my best to explain it, sorry if it's to long

    Thanks again for trying to help!!!

  2. #2
    Join Date
    Nov 2007
    Posts
    17
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Class GUI Interactions??

    IMHO it seems to me your problem comes because you're unsure about the dependencies between your classes. Therefore, my answer depends on what you want to achieve in particular. For example

    ...if the classes are independent of eachother then you need a class like MainClass to control things like you've explained. Signals/Slots are a very powerful feature of Qt. Perfect for designing loose coupled dependencies. For example use MainClass as a mediator between the others and establish connects like...
    Qt Code:
    1. connect(classTwo, SIGNAL(needPushButtonDeactivate()), classThree, SLOT(deactivatePushButton()));
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to montuno for this useful post:

    mikey33 (19th November 2009)

  4. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Class GUI Interactions??

    It seems like you have a group of classes controlling ui elements and you need a controller, kind of like the Model View Controller (MVC), as described here: http://en.wikipedia.org/wiki/Model%E...0%93controller

    You have the classes controlling the view, so you need a controller. So instead of main constructing the classes and controlling them, you put that code into the controller, and let main just initialise the controller class which will look after SecondClass/ThirdClass and so on.

    Or, if you prefer SecondClass to have access to the gui of ThirdClass, then pass in a class pointer of ThirdClass in the constructor of SecondClass, and then copy that pointer to a member variable also of ThirdClass *, then any member function can access the ui of thirdclass.

    To me though, I'd say SecondClass talking to ThirdClass would be messy code, and that you should go down the MVC route.

  5. The following user says thank you to squidge for this useful post:

    mikey33 (19th November 2009)

  6. #4
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Class GUI Interactions??

    Quote Originally Posted by fatjuicymole View Post
    Or, if you prefer SecondClass to have access to the gui of ThirdClass, then pass in a class pointer of ThirdClass in the constructor of SecondClass, and then copy that pointer to a member variable also of ThirdClass *, then any member function can access the ui of thirdclass.
    You both are correct and I believe I forgot to mention that MainClass is acting like the controller in this situation but I just figured since the SecondClass was only interacting with the ThirdClass then it should have a direct link between the two instead of using the MainClass as a middleman aka controller so thanks a ton for both of your replys...

    Although I have thus solved the problem just like what fatjuicymole said above but in a slightly different way which works below...

    MainClass Constructor...

    Qt Code:
    1. classTwo = new SecondClass(this);
    2. classThree = new ThirdClass(this);
    3.  
    4. classTwo->setThirdClassPointer(classThree);
    To copy to clipboard, switch view to plain text mode 

    SecondClass header...

    Qt Code:
    1. public:
    2. void setThirdClassPointer(ThirdClass *classThree);
    3.  
    4. private:
    5. ThirdClass *test;
    To copy to clipboard, switch view to plain text mode 

    SecondClass cpp...

    Qt Code:
    1. void SecondClass::setThirdClassPointer(ThirdClass *classThree)
    2. {
    3. test = classThree;
    4. }
    To copy to clipboard, switch view to plain text mode 

    and then I was able to use the ThirdClass objects essentially anywhere in the SecondClass.

    My issue before was one of the simplest mistakes anyone can probably make. I'm using QSettings in the SecondClass and my read/load function for QSettings is placed in the SecondClass constructor and the function defined is right below the constructor so in testing to ensure the code worked I was lazy and put the test code in the nearest available function below the constructor, I used the test code below...

    Qt Code:
    1. test->classThreeGui->buttonOne->setEnabled(false);
    To copy to clipboard, switch view to plain text mode 

    in the read/load function so I'm gonna guess the initial problem revolved around trying to access objects that weren't created yet since the read/load function was placed in the constructor and at that time the instance of the ThirdClass wasn't created yet if that makes sense or is right so basically it was pointing to an invalid unknown location causing the program to crash. If only I knew that the first time I could've saved a lot of time hehe! Thanks again everyone! Hope this helps someone else!!!

Similar Threads

  1. Joomla 1.5 password hash Class 1.0 for QT using C++
    By RajabNatshah in forum Qt Programming
    Replies: 0
    Last Post: 6th October 2009, 15:49
  2. Extending two class "the same way"
    By caduel in forum General Programming
    Replies: 3
    Last Post: 22nd July 2009, 22:55
  3. class in the class
    By baray98 in forum General Programming
    Replies: 2
    Last Post: 23rd July 2008, 07:01
  4. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

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.