##// END OF EJS Templates
fixed example for 2.0 API...
florianlink -
r154:e871b6908a03
parent child
Show More
@@ -1,101 +1,92
1 /*
1 /*
2 *
2 *
3 * Copyright (C) 2010 MeVis Medical Solutions AG All Rights Reserved.
3 * Copyright (C) 2010 MeVis Medical Solutions AG All Rights Reserved.
4 *
4 *
5 * This library is free software; you can redistribute it and/or
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
8 * version 2.1 of the License, or (at your option) any later version.
9 *
9 *
10 * This library is distributed in the hope that it will be useful,
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
13 * Lesser General Public License for more details.
14 *
14 *
15 * Further, this software is distributed without any warranty that it is
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
20 * other software, or any other product whatsoever.
21 *
21 *
22 * You should have received a copy of the GNU Lesser General Public
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
25 *
26 * Contact information: MeVis Medical Solutions AG, Universitaetsallee 29,
26 * Contact information: MeVis Medical Solutions AG, Universitaetsallee 29,
27 * 28359 Bremen, Germany or:
27 * 28359 Bremen, Germany or:
28 *
28 *
29 * http://www.mevis.de
29 * http://www.mevis.de
30 *
30 *
31 */
31 */
32
32
33 //----------------------------------------------------------------------------------
33 //----------------------------------------------------------------------------------
34 /*!
34 /*!
35 // \file PythonQtTests.cpp
35 // \file PythonQtTests.cpp
36 // \author Florian Link
36 // \author Florian Link
37 // \author Last changed by $Author: florian $
37 // \author Last changed by $Author: florian $
38 // \date 2006-05
38 // \date 2006-05
39 */
39 */
40 //----------------------------------------------------------------------------------
40 //----------------------------------------------------------------------------------
41
41
42 #include "PythonQt.h"
42 #include "PythonQt.h"
43 #include <QApplication>
43 #include <QApplication>
44 #include <QTextBrowser>
44 #include <QTextBrowser>
45 #include <QLayout>
45 #include <QLayout>
46 #include <QGroupBox>
46 #include <QGroupBox>
47 #include <QPushButton>
47 #include <QPushButton>
48 #include <QLineEdit>
48 #include <QLineEdit>
49
49
50 int main( int argc, char **argv )
50 int main( int argc, char **argv )
51 {
51 {
52 QApplication qapp(argc, argv);
52 QApplication qapp(argc, argv);
53
53
54 // init PythonQt and Python
54 // init PythonQt and Python
55 PythonQt::init();
55 PythonQt::init();
56
56
57 // get the __main__ python module
57 // get the __main__ python module
58 PythonQtObjectPtr mainModule = PythonQt::self()->getMainModule();
58 PythonQtObjectPtr mainModule = PythonQt::self()->getMainModule();
59
59
60 // evaluate a simple python script and receive the result a qvariant:
60 // evaluate a simple python script and receive the result a qvariant:
61 QVariant result = mainModule.evalScript("19*2+4", Py_eval_input);
61 QVariant result = mainModule.evalScript("19*2+4", Py_eval_input);
62
62
63 // Create object from python, hold onto reference in C++:
64 PythonQtObjectPtr tag = mainModule.evalScript("EyeD3Tagger()\n", Py_eval_input);
65 Q_ASSERT(!tag.isNull());
66
67 // call python methods from C++
68 tag.call("setFileName", QVariantList() << "t.mp3");
69 QVariant fn = tag.call("fileName", QVariantList());
70 Q_ASSERT(fn.toString() == QString("t.mp3"));
71
72 // create a small Qt GUI
63 // create a small Qt GUI
73 QVBoxLayout* vbox = new QVBoxLayout;
64 QVBoxLayout* vbox = new QVBoxLayout;
74 QGroupBox* box = new QGroupBox;
65 QGroupBox* box = new QGroupBox;
75 QTextBrowser* browser = new QTextBrowser(box);
66 QTextBrowser* browser = new QTextBrowser(box);
76 QLineEdit* edit = new QLineEdit(box);
67 QLineEdit* edit = new QLineEdit(box);
77 QPushButton* button = new QPushButton(box);
68 QPushButton* button = new QPushButton(box);
78 button->setObjectName("button1");
69 button->setObjectName("button1");
79 edit->setObjectName("edit");
70 edit->setObjectName("edit");
80 browser->setObjectName("browser");
71 browser->setObjectName("browser");
81 vbox->addWidget(browser);
72 vbox->addWidget(browser);
82 vbox->addWidget(edit);
73 vbox->addWidget(edit);
83 vbox->addWidget(button);
74 vbox->addWidget(button);
84 box->setLayout(vbox);
75 box->setLayout(vbox);
85
76
86 // make the groupbox to the python under the name "box"
77 // make the groupbox to the python under the name "box"
87 mainModule.addObject("box", box);
78 mainModule.addObject("box", box);
88
79
89 // evaluate the python script which is defined in the resources
80 // evaluate the python script which is defined in the resources
90 mainModule.evalFile(":GettingStarted.py");
81 mainModule.evalFile(":GettingStarted.py");
91
82
92 // define a python method that appends the passed text to the browser
83 // define a python method that appends the passed text to the browser
93 mainModule.evalScript("def appendText(text):\n box.browser.append(text)");
84 mainModule.evalScript("def appendText(text):\n box.browser.append(text)");
94 // shows how to call the method with a text that will be append to the browser
85 // shows how to call the method with a text that will be append to the browser
95 mainModule.call("appendText", QVariantList() << "The ultimate answer is ");
86 mainModule.call("appendText", QVariantList() << "The ultimate answer is ");
96
87
97
88
98
89
99 return qapp.exec();
90 return qapp.exec();
100 }
91 }
101
92
General Comments 0
You need to be logged in to leave comments. Login now