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