Results 1 to 8 of 8

Thread: separate class using an interface

  1. #1
    Join Date
    Jul 2009
    Posts
    89
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default separate class using an interface

    A very basic question! I am porting a program from Borland builder. This is the setup: a ui interface with a button and a label. A separate clas that is a mathematical model called TWorld, doing a number of calculations. The ui is a class generated in the Eclipse IDE, the main function looks like:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. mod3 w;
    5. w.show();
    6. return a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    where mod3 is a QWidget. Eclipse generates the necessary ui related files. The class TWorld is declared in the class mod3 as:
    Qt Code:
    1. ...
    2. public:
    3. TWorld *W;
    4. ...
    To copy to clipboard, switch view to plain text mode 

    If the button is pushed the mathmodel class is created and calculations are executed:
    Qt Code:
    1. void mod3:: on_pushButton_clicked()
    2. {
    3. W = new TWorld();
    4. W->docalc();
    5. }
    To copy to clipboard, switch view to plain text mode 

    at the end of the calculations the result should be displayed as the label text on the interface:
    Qt Code:
    1. void TWorld::docalc()
    2. {
    3. ... lots of calculations ...
    4. w.label->setNum(result); //doesn't work
    5. }
    To copy to clipboard, switch view to plain text mode 

    this doesn't work of course because label is not part of W (TWorld) but of the interface mod3. mod3 is defined and created in the main function as "w" and only exists there it seems to me. If I make w a global variable, defined as extern in a header file with the definition in main.cpp, I get the error "QWidget: Must construct a QApplication before a QPaintDevice"
    I don't understand how I can make the class TWorld "see" the interface. Probably something very simple I am overloocking!
    thanks!
    Last edited by qt_gotcha; 18th February 2010 at 23:13.

  2. #2
    Join Date
    Jul 2009
    Posts
    89
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: separate class using an interface

    hmm, apparently it works when w is defined as a pointer in main.cpp:

    mod3 *w;

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    w = new mod3();
    w->show();
    return a.exec();
    }

    and extern mod3 *w in a header file ?

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: separate class using an interface

    You might be looking at this the wrong way around.

    If you give TWorld a public getter function to retrieve the result value then the
    Qt Code:
    1. void mod3:: on_pushButton_clicked()
    2. {
    3. W = new TWorld();
    4. W->docalc();
    5. }
    To copy to clipboard, switch view to plain text mode 
    becomes
    Qt Code:
    1. void mod3:: on_pushButton_clicked()
    2. {
    3. W = new TWorld();
    4. W->docalc();
    5. label.setNum(W->getResultValue());
    6. }
    To copy to clipboard, switch view to plain text mode 
    You could also achieve this by having the result value returned by
    Qt Code:
    1. int TWorld::docalc() {
    2. ... do lots of funky stuff
    3. return result;
    4. }
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. void mod3:: on_pushButton_clicked()
    2. {
    3. W = new TWorld();
    4. label.setNum( W->docalc() );
    5. }
    To copy to clipboard, switch view to plain text mode 
    You might also want to think about where W is created: Every time this button is pressed a new TWorld will be created... where is the old one deleted?

  4. #4
    Join Date
    Jul 2009
    Posts
    89
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: separate class using an interface

    thanks for the reply. World is deleted at the end of the run, I didn't show. In docalc many loops are performed and the result of each loop should be shown on the interface, not just at the end. Sory for not being clear. Following your example I have to program the calculations (the content of the socalc function) not in a separate class World but as part of the interface class mod3. Is that the only way?

    The loops can be long and in the mean time I don't want it to block the interface, so I can also make it a thread.

  5. #5
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: separate class using an interface

    My concern is that it sounds like the calculation may take a lot of time. If this is so, perhaps you should perform the calculation in another thread. If you do this, then you can add a signal to report the result of the calculation, and a slot to receive the result of the calculation. Just a thought....

  6. #6
    Join Date
    Jul 2009
    Posts
    89
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: separate class using an interface

    yuo're absolutely right. I did that and it works fine now
    thanks
    I followed this
    http://thesmithfam.org/blog/2009/09/...reading-in-qt/

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

    pitonyak (3rd March 2010)

  8. #7
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: separate class using an interface

    Quote Originally Posted by qt_gotcha View Post
    yuo're absolutely right. I did that and it works fine now
    thanks
    I followed this
    http://thesmithfam.org/blog/2009/09/...reading-in-qt/
    you could have used QProgressDialog, less hassle than multithreading

  9. #8
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: separate class using an interface

    Thanks for the link, very informative...

Similar Threads

  1. separate a QString
    By Zergi in forum Newbie
    Replies: 1
    Last Post: 18th July 2008, 21:50
  2. separate Qt class diagram
    By juanrb in forum Qt Programming
    Replies: 2
    Last Post: 14th April 2008, 19:55
  3. QFtp in a separate instances
    By baray98 in forum Qt Programming
    Replies: 9
    Last Post: 11th April 2008, 05:54
  4. new QWidget in separate thread
    By magland in forum Qt Programming
    Replies: 15
    Last Post: 7th February 2008, 12:32

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.