qint64 get_size()
{
QString link = url->text();
std::cout<<qPrintable(link)<<std::endl;
QNetworkAccessManager manager;
QEventLoop loop;
QNetworkReply *reply = manager.head(QNetworkRequest(link));
QObject::connect(reply,SIGNAL(finished()), &loop, SLOT(quit()), Qt::DirectConnection);
loop.exec();
QVariant var = reply->header(QNetworkRequest::ContentLengthHeader);
delete reply;
qint64 size = var.toLongLong();
std::cout<<size<<std::endl;
return size;
}
class uWidget : public QWidget
{
public:
uWidget()
{
QCompleter * completer = new QCompleter(this);
QFileSystemModel * model = new QFileSystemModel(completer);
model->setFilter(QDir::Dirs | QDir::Drives | QDir::AllDirs);// | QDir::NoDotAndDotDot);
model->setRootPath(tr("D:"));
completer->setModel(model);
QHBoxLayout* layout = new QHBoxLayout(this);
QPushButton* button = new QPushButton("Click");
QLineEdit* edit = new QLineEdit();
layout->addWidget(button);
layout->addWidget(edit);
edit->setCompleter(completer);
}
};
具体可参见qt下的例子-webkit\previewer
通过webview->setHtml(string);即可加载显示网页信息
另外通过QWebFrame* frame = webview->page()->mainFrame();即获取当前网页源码
感觉QWebView使用很方便的,只不过QWebkit.dll大小为11.5m太夸张了.
public static String load(String name)
{
File file = new File(name);
String buffer = new String();
if(!file.exists())
{
System.out.println("can't find " + name);
}
try
{
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while((line = reader.readLine()) != null)
{
buffer += line;
}
reader.close();
}
catch (IOException e)
{
e.getStackTrace();
}
return buffer;
}
#include <QApplication>
#include <QMessageBox>
#include <QtUiTools/QtUiTools>
#include <iostream>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QUiLoader loader;
QFile file("ui.ui");
QWidget* ui = loader.load(&file);
if(ui)
{
foreach(QString name,loader.availableWidgets())
std::cout<<qPrintable(name)<<std::endl;
ui->show();
}
else
{
QMessageBox::information(NULL,"Error","Load ui script failed");
}
return app.exec();
}
有时候动态载入控件还是很有必要的
python代码二段:
call.py
def test():
print 'hello world'
def add(a,b):
return a + b
api.py
import io
def load_test():
fp = open('call.py','r')
buffer = ''
if fp:
buffer = fp.read()
fp.close()
return buffer
cpp代码:
#include <stdio.h>
#include <stdlib.h>
#include <Python.h>
int main(int argc, char *argv[])
{
Py_Initialize();
if(!Py_IsInitialized())
{
return -1;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyObject* pName;
PyObject* pModule;
PyObject* pDict;
PyObject* pFunc;
pName = PyString_FromString("api");
pModule = PyImport_Import(pName);
if(!pModule)
{
printf("can't find call.py");
getchar();
return -1;
}
pDict = PyModule_GetDict(pModule);
if(!pDict)
{
return -1;
}
{
pFunc = PyDict_GetItemString(pDict,"load_test");
if(!pFunc || !PyCallable_Check(pFunc))
{
printf("can't find function [test]");
getchar();
return -1;
}
PyObject *pFn = PyObject_CallObject(pFunc,0);
char* buffer = PyString_AsString(pFn);
printf("%s\n",buffer);
PyObject* o = Py_CompileString(buffer,"none",Py_file_input);
PyObject* m = PyImport_ExecCodeModule("a.a",o);
PyObject* d = PyModule_GetDict(m);
pFunc = PyDict_GetItemString(d,"add");
if(!pFunc || !PyCallable_Check(pFunc))
{
printf("can't find function [add]");
getchar();
return -1;
}
PyObject* args = PyTuple_New(2);
PyTuple_SetItem(args,0,Py_BuildValue("l",3));
PyTuple_SetItem(args,1,Py_BuildValue("l",4));
PyObject *pAdded = PyObject_CallObject(pFunc,args);
int ret = PyInt_AsLong(pAdded);
printf("add value:%d\n",ret);
}
Py_Finalize();
system("PAUSE");
return 0;
}
这段代码和上一篇有点区别
主要区别是从从内存载入python模块然后调用函数
主要部分是这块:
PyObject* o = Py_CompileString(buffer,"none",Py_file_input);
PyObject* m = PyImport_ExecCodeModule("a.a",o);
PyObject* d = PyModule_GetDict(m);
buffer是python源码字符串
在python2.7中执行正常
先上python代码:
# call.py
def test():
print 'hello world'
def add(a,b):
return a + b
再上c代码
#include <stdio.h>
#include <stdlib.h>
#include <Python.h>
int main(int argc, char *argv[])
{
Py_Initialize();
if(!Py_IsInitialized())
{
return -1;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyObject* pName;
PyObject* pModule;
PyObject* pDict;
PyObject* pFunc;
pName = PyString_FromString("call");
pModule = PyImport_Import(pName);
if(!pModule)
{
printf("can't find call.py");
getchar();
return -1;
}
pDict = PyModule_GetDict(pModule);
if(!pDict)
{
return -1;
}
{
pFunc = PyDict_GetItemString(pDict,"test");
if(!pFunc || !PyCallable_Check(pFunc))
{
printf("can't find function [test]");
getchar();
return -1;
}
PyObject_CallObject(pFunc,0);
}
{
pFunc = PyDict_GetItemString(pDict,"add");
if(!pFunc || !PyCallable_Check(pFunc))
{
printf("can't find function [test]");
getchar();
return -1;
}
PyObject* args = PyTuple_New(2);
PyTuple_SetItem(args,0,Py_BuildValue("l",3));
PyTuple_SetItem(args,1,Py_BuildValue("l",4));
PyObject *pAdded = PyObject_CallObject(pFunc,args);
int ret = PyInt_AsLong(pAdded);
printf("add value:%d\n",ret);
Py_DECREF(args);
}
Py_DECREF(pName);
Py_DECREF(pDict);
Py_DECREF(pModule);
Py_Finalize();
system("PAUSE");
return 0;
}
就不做解释了
不过如何从字符串中载入模块?
#include <QApplication>
#include <QStateMachine>
#include <QPushButton>
#include <QTextEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSpacerItem>
#include <QSignalTransition>
#include <QPropertyAnimation>
int main(int argc,char **argv)
{
QApplication app(argc,argv);
QWidget* panel = new QWidget;
panel->resize(320,360);
QVBoxLayout* layout = new QVBoxLayout();
panel->setLayout(layout);
QPushButton* button = new QPushButton("Click");
QSpacerItem* spacer = new QSpacerItem(40,20,QSizePolicy::Expanding,QSizePolicy::Minimum);
QHBoxLayout* hlayout = new QHBoxLayout();
layout->addLayout(hlayout);
hlayout->addItem(spacer);
hlayout->addWidget(button);
QTextEdit* edit1 = new QTextEdit();
edit1->setGeometry(QRect(10,50,300,300));
QTextEdit* edit2 = new QTextEdit();
edit2->setGeometry(QRect(10,300,300,0));
layout->addWidget(edit1);
layout->addWidget(edit2);
QStateMachine* machine = new QStateMachine;
QState* state1 = new QState(machine);
state1->assignProperty(edit1,"geometry",QRect(10,50,300,300));
state1->assignProperty(edit2,"geometry",QRect(10,300,300,0));
QState* state2 = new QState(machine);
state2->assignProperty(edit1,"geometry",QRect(10,50,300,0));
state2->assignProperty(edit2,"geometry",QRect(10,50,300,300));
machine->setInitialState(state1);
QPropertyAnimation* ani1 = new QPropertyAnimation(edit1,"geometry");
ani1->setDuration(2000);
ani1->setEasingCurve(QEasingCurve::OutBounce);
QPropertyAnimation* ani2 = new QPropertyAnimation(edit2,"geometry");
ani2->setDuration(2000);
ani2->setEasingCurve(QEasingCurve::InOutExpo);
QSignalTransition* transition1 = state1->addTransition(button,SIGNAL(clicked()),state2);
QSignalTransition* transition2 = state2->addTransition(button,SIGNAL(clicked()),state1);
transition1->addAnimation(ani1);
transition1->addAnimation(ani2);
transition2->addAnimation(ani1);
transition2->addAnimation(ani2);
machine->start();
panel->show();
return app.exec();
}
有时候需要从QUrl中取出本地文件名
代码如下:
QFileInfo info(url.toLocalFile());
QString filename = info.absoluteFilePath();
libavformat
libavcodec
libavutil