PDA

View Full Version : Qt in other class



Morea
8th April 2006, 22:29
I wish to write a totaly simple C++ class. It should contain a pointer to a QMainWidget (or another suitable Qt object) and if a certain bool variable is set to false the pointer should be 0 or else it should point to a qtobject that contains three buttons and a big area where I can draw stuff.

Like this

class Box{
private:
SomeQtclass* theQtObject;
bool use_gui;
void drawstuff(){if (use_gui) /*do stuff*/}
void do_random_thing_not_directly_affecting_the_gui();
};

In the main application I would then have


int main()
{
Box mybox;
cin>>mybox.use_gui;
while (true){
mybox.do_random_thing_not_directly_affecting_the_g ui();
mybox.drawstuff();
}
}

The Box class should be a kind of class that could be included in many other programs, like std::string.

How do I write this kind of code? I need some kind of QApplication, right? Where do I put it?
Anyone with idas, please speak up!

wysota
8th April 2006, 22:41
But what is exactly your problem? Based on the number of your posts and the forum you post in I take it that you should know where to create a QApplication object...

Morea
9th April 2006, 08:09
But the thing is, I don't want to give up the controll to QApplication::exec(). I can't run it and get back to the other code I was running before that? That is the problem.

Don't let the number of questions fool you. It might be a sign of a REAL newbee.

fullmetalcoder
9th April 2006, 10:04
But the thing is, I don't want to give up the controll to QApplication::exec(). I can't run it and get back to the other code I was running before that? That is the problem.

Don't let the number of questions fool you. It might be a sign of a REAL newbee.

Just do it! :p
Basically, the whole QtCore module is accessible (containers and so on) without any need of QApplication::exec() but if you want piece of GUI you'll need to give up the control and rely on the event loop...

wysota
9th April 2006, 17:08
If you want to run some code in parallel to operating on a GUI, you might consider running it in a separate thread (http://doc.trolltech.com/4.1/qthread.html) or as a timeout slot for a QTimer object.