MCP
MCP

Reputation: 4536

Using Qt GUI with objects external to application

I'm creating a Qt GUI that will generate some forms for the user, let them fill them in, and then save the forms in a binary search tree that will then be serialized. This is done using Qt (obviously), c++, and boost. The GUI is the only interface with the application at this point. I would like to instantiate my binary search tree object outside of the GUI as creating it inside the main GUI class seems to be a bad design pattern. Is this thought correct?

Here's my main() right now:

int main (int argc, char *argv[])
{
    QApplication app(argc, argv);
    ChocAnGui *gui = new ChocAnGui;
    gui->show();

    return app.exec();
}

Is there a way to create a BST object that the ChocAnGui class can use, but that lives outside of the actual GUI? I'd like to do something like:

int main (int argc, char *argv[])
{
    MyBST bst = new MyBST;
    MyRecord record = new MyRecord;

    QApplication app(argc, argv);
    ChocAnGui *gui = new ChocAnGui;
    gui->show();

    return app.exec();
}

I would then like the GUI to be able to call methods on the MyBST and MyRecord classes from within. Because at the start and finish of the application I am serializing and de-serializing data, this seems to be the most abstract approach (vs. serializing and de-serializing from within the GUI code itself). I hope this question is clear... I'm learning all this stuff as we speak. Is there a way to pass those vars through QApplication(argc, argv)? Eventually the record and BST classes will be built into a database and, again, creating the instances outside of the GUI seems most straightforward. Thanks for any input you can provide.

Upvotes: 2

Views: 779

Answers (2)

Min Lin
Min Lin

Reputation: 3197

I think it is necessary for you to put your binary search tree outside the GUI thread only when your Binary search takes a long time and is possible to block the GUI's thread. If this is the case, the what you wanna do is to put the MyBST object under another thread,

In your main application, create a new thread that will handle all BST operations:

QThread * BSTThread = new QThread();
BSTThread->start();

BSTThread is a new thread that has its own event queue. Then you create you MyBST Object, Make sure MyBST inherits QObject, and you can call the moveToThread method on the object.

MyBST * bst = new MyBST();
bst->moveToThread(BSTThread);

When the main application wants to communicate with the MyBST, you emit a signal from you main application. Connect the signal to a slot in the MyBST class using Qt::QueuedConnection, or just use Qt::AutoConnection, it will automatically be queuedconnection because your bst object lives in a different thread as the main app, and the function in the slot will be executed by the BSTThread.

And when the bst object wants to communicate back to the main app, also emit a signal, this signal is connected to the slot in the main app, which will handle this properly in your mainThread. In this way, your communication is asynchronous, you can not get the result immediately when you send request to the bst object, because you send it as a signal and it is posted in the event queue of the BSTThread waiting to be processed. After the BSTThread processed the request from the main app, it posts back the result to the main thread's event queue, the main thread handles the results and updates the GUI.

I think you need to read this http://doc.qt.digia.com/qt/thread-basics.html on QT if you want to know more details.

Upvotes: 1

jdi
jdi

Reputation: 92569

Just create your window to declare a MyBST member:

class MyBST;

class ChocAnGui
    : public QMainWindow
{
    Q_OBJECT

 public:
   ChocAnGui(MyBST* aBst = 0, QWidget *parent = 0);
   MyBST *bst

And then initialize it with your object:

ChocAnGui *gui = new ChocAnGui(bst);

Or you can not make it a member, and make your gui just always take a MyBST pointer:

class MyBST;

class ChocAnGui
    : public QMainWindow
{
    Q_OBJECT

 public:
   ChocAnGui(QWidget *parent = 0);
   void doSomething(MyBST*);

Upvotes: 0

Related Questions