2

I am customizing vlc source code and needed to use QNetworkAccessManager from Qt OpenDialog (part of QT UI dialogs for vlc).

I am trying to use following code sample

void MainWindow::requestShowPage(){
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(requestReceived(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://google.com")));
}

void MainWindow::requestReceived(QNetworkReply* reply){
    QString replyText;
    replyText.fromAscii(reply->readAll());
    ui->txt_debug->appendPlainText(replyText);
}

My primary problem is that vlc fails to load UI even if there is a single call as follows:

QNetworkAccessManager *manager = new QNetworkAccessManager(this);

Following is the output produced on vlc console

./vlc
[0x19c9388] main libvlc: Running app with the default interface. 
[0x1f82988] main interface error: corrupt module: /VLC/vlc-2.0.4/modules/gui/qt4/.libs/libqt4_plugin.so
[0x2586748] main generic error: corrupt module: /VLC/vlc-2.0.4/modules/gui/qt4/.libs/libqt4_plugin.so
[0x1f82988] skins2 interface error: no suitable dialogs provider found (hint: compile the qt4 plugin, and make sure it is loaded properly)
[0x1f82988] skins2 interface error: cannot instantiate qt4 dialogs provider
[0x1f82988] [cli] lua interface: Listening on host "*console".

Simply omitting the QNetworkAccessManager brings the UI back again.

a. Is there any thing special regarding QNetworkAccessManager usage scenarios i.e. should it be created globally or something? I went through its documentation, but didn't find any thing.

b. Are there any special conventions with respect to Qt or it's use with VLC that i am missing? I am significantly experienced in c/c++ and Linux but new to QT.

UPDATE1: I saw this SO question too which basically is trying to do the same httpget using QNetworkAccessManager. However, i believe calling this one simple api is not required to be done as a separate module (the question attempts writing a new module)? Or is there any such restriction in qt / vlc

UPDATE2: What i suspect so far is that it has something to do with adding a new class to vlc qt ui section. I tried including http example that comes with qt installation with vlc, but see the same behavior. Any guidelines on including a .cpp and .h in vlc ui components would be helpful.

UPDATE 3: I followed as suggested in the answer below and can't seem to make sense out of the following compilation errors. Can any one help?

/usr/include/qt4/QtCore/qobject.h: In copy constructor ‘QNetworkAccessManager::QNetworkAccessManager(const QNetworkAccessManager&)’:
/usr/include/qt4/QtCore/qobject.h:333:5: error: ‘QObject::QObject(const QObject&)’ is private
In file included from /usr/include/qt4/QtNetwork/QNetworkAccessManager:1:0,
                 from qt4.hpp:39,
                 from qt4.cpp:37:
/usr/include/qt4/QtNetwork/qnetworkaccessmanager.h:72:24: error: within this context
qt4.cpp: At global scope:
qt4.cpp:192:63: note: synthesized method ‘QNetworkAccessManager::QNetworkAccessManager(const QNetworkAccessManager&)’ first required here 
In file included from qt4.cpp:54:0:

============

qt4.hpp contains following added at global scope

#include <QNetworkAccessManager>
extern QNetworkAccessManager NETWORK_MANAGER;

qt4.cpp contains this at global scope

QNetworkAccessManager NETWORK_MANAGER = QNetworkAccessManager();

and then i am accessing in one of the sub classes of qt4

QNetworkAccessManager * qnam = &NETWORK_MANAGER;

UPDATE 4 I also discovered that manually including QtNetwork to the make file was also problematic (although it it didn't complain in compilation for the headers) and vlc ui failed to load. However, when i added it to main vlc configuration file, even the basic local creation of QNetworkAccessManager worked. See this vlc mailing list thread for details

Community
  • 1
  • 1
fkl
  • 5,412
  • 4
  • 28
  • 68
  • VLC may not use the Qt mainloop correctly - although I can not assert this with certainty. If synchronous is okay, you could try to use a QMainLoop and hook `finished` up to the mainloop's `quit`. – John Chadwick Dec 10 '12 at 11:44
  • Thanks, you mean VLC code might not be using it correctly otherwise? But there are tons of other stably working dialogs. Even if i ignore finished event and only include a single line new QNetworkAccessManager in some method of open dialog, it fails. – fkl Dec 10 '12 at 11:48
  • That's interesting... Does VLC already create a QNetworkAccessManager anywhere else? I'm going to check the Qt sources in a second. – John Chadwick Dec 10 '12 at 11:48
  • I don't think it does. Didn't find any occurrences via grep / search and also had to modify the make files i.e. add QtNetwork module. The others included by default were QtGui, core etc. – fkl Dec 10 '12 at 11:51

1 Answers1

2

The QNetworkAccessManager should be created globally. If you create it in a class or in a method, it will be destroyed with the object (or the method) and the requests that you sent will be lost. Moreover, I have already encounter problems with several QNetworkAccessManagers in a program So I recommend you to do somethng like this :

a_module.hpp :

// ...

#ifndef THE_MODULE
#define THE_MODULE

// ...

#include <QNetworkAccessManager>

// ...

extern QNetworkAccessManager NETWORK_MANAGER;

// ...

#endif    // THE_MODULE

a_module.cpp :

// ...

#include "a_module.hpp"

// ...

QNetworkAccessManager NETWORK_MANAGER = QNetworkAccessManager();

// ...

In your code :

// ...

#include "a_module.hpp"

// ...

QNetworkAccessManager * qnam = &NETWORK_MANAGER;

// ...

EDIT : if it does not work (cf. update 3 in the question), try QNetworkAccessManager NETWORK_MANAGER; instead of QNetworkAccessManager NETWORK_MANAGER = QNetworkAccessManager(); in a_module.cpp. It is due to the compiler. Unlike MSVC, g++ compiles with QNetworkAccessManager NETWORK_MANAGER; but not QNetworkAccessManager NETWORK_MANAGER = QNetworkAccessManager();.

air-dex
  • 4,130
  • 2
  • 25
  • 25
  • I used it my code and am getting compilation errors that so far i don't exactly understand. I have added details in question's update. Would appreciate if you could help. Thanks – fkl Dec 24 '12 at 07:26
  • @fayyazkl: which compiler do you use ? g++ ? MSVC ? Another one ? Look at the EDIT I have made for this. – air-dex Dec 24 '12 at 22:47
  • using g++. I will try that out and let you know Thank you – fkl Dec 25 '12 at 05:04
  • I also found the proper way to include QNewtorkAccessManager into vlc source code. Updated into question – fkl Jan 05 '13 at 12:04