Question

I've been trying to start with a simple app that retrieves data from a simple HTML page upon clicking a button and stumbled upon a rather helpful tutorial on QT-Project and have been trying to implement it for my own project.

Everything manages to compile until I try to actually try to implement the loadImage function (as found in the tutorial). (I actually had to initialize m_pImgCtrl as Filedownloader * m_pImgCtrl = new FileDownloader(imageUrl, this); and I'm not exactly sure how it's suppose to work without prior object declaration?)

From what I get, m_pImgCtrl isn't actually defined in the loadImage() function since it is initialized outside of the function? Or does the connect() function do something that I'm not too aware of?

Thanks for the help!

Was it helpful?

Solution

The tutorial doesn't tell you the whole story.

The code in section Usage is supposed to be part of a class MainWindow – the controller of your main window (see line 1 of tutorial's last snippet). This class contains a slot loadImaged() called when the NetworkReply has finished. It also has a member FileDownloader * m_pImgCtrl.

For instance, the second Usage snippet could be part of a slot MainWindow::buttonClicked() like

void MainWindow::buttonClicked()
{
    QUrl imageUrl("http://qt.digia.com/Documents/1/QtLogo.png");
    m_pImgCtrl = new FileDownloader(imageUrl, this);
    connect(m_pImgCtrl, SIGNAL(downloaded()), SLOT(loadImage()));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top