PDA

View Full Version : How to get the value from the pointer using QPixmap and use it in QListwidget?



harish
13th December 2011, 06:27
Hi,
I had tried to parse the image from the server.This is my coding:


QPixmap *pix;

void Homescreen::content(QList<QString> name,QList<QString> category,QList<QString> image)
{
QListWidget *listWidget = new QListWidget(0);
QStringList strList;

for(int i=0 ; i<name.count(); i++)
{
QString Name = name[i];
QString Image = image[i];

strList << Name ;



QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager,SIGNAL(finished(QNetworkReply*)),t his,SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(Image));



QPixmap pi = new QPixmap(*pix);
ui->listWidget->addItem(new QListWidgetItem(QIcon(pi),Name,ui->listWidget));


void Homescreen::replyFinished(QNetworkReply *reply)
{

QImageReader imageReader(reply);
myImage= imageReader.read();
*pix = QPixmap::fromImage(myImage);

}

I had written the code to get the image from the server.Then i had converted the image using QPixmap to show it in the listwidget.


Now the problem is,the image is in a pointer and i need to convert the pointer value using QPixmap so that i can add it to the QListwidget.

Can anyone tell me what i need to do so that my pointer value gets converted into QPixmap.

From my code,it is showing that the painter is not active.



Thanks in advance,
Harish.M

ChrisW67
13th December 2011, 07:04
Repeat after me: I will use
tags around my code. Seriously! It would also help if you posted enough of your actual code that we could see where some of those lines come from. Edit your post and fix it.

Before you are going to get anywhere you need to learn some basic C++. You will not succeed with blind cut and paste. For example, your replyFinished() slot:


void Homescreen::replyFinished(QNetworkReply *reply)
{
QImageReader imageReader(reply);
myImage= imageReader.read();
*pix = QPixmap::fromImage(myImage); // What type and where is pix declared? Is the pointer initialised? Why use a pointer at all?
QLabel *label; // An uninitialised pointer to a QLabel. OK, but surely you wanted a label in the UI?
label->setPixmap(*pix); // BOOM!! You cannot use an uninitialised pointer and expect anything but grief.
}

harish
13th December 2011, 07:21
How to get the value from the pointer using QPixmap and use it in QListwidget?

ChrisW67
13th December 2011, 09:38
Can you point out me where i had done the mistake?
Using an uninitialised pointer... see my last post.
Using a global where a member variable or local variable would do.
Using a pointer where none is needed (pix) is inelegant and potentially fatal if you never init the pointer.


Here is a complete example:

#include <QtGui>
#include <QtNetwork>
#include <QDebug>

class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p) {
m_label = new QLabel(this);
setCentralWidget(m_label);
resize(320, 240);

connect(&m_nam, SIGNAL(finished(QNetworkReply*)), SLOT(finished(QNetworkReply*)));
QNetworkRequest request(QUrl("http://www.qtcentre.org/images/qtcentre/QtCentre.png"));
(void) m_nam.get(request); // Deliberately ignoring return value
}
public slots:
void finished(QNetworkReply *reply) {
if (reply->error() == QNetworkReply::NoError) {
QByteArray data( reply->readAll() );
QPixmap pixmap;
bool ok = pixmap.loadFromData( data );
m_label->setPixmap( pixmap );
qDebug() << "Data size ==" << data.size();
qDebug() << "Conversion OK ==" << ok;
qDebug() << "Pixmap is null ==" << pixmap.isNull();
}
else
qDebug() << "Network error" << reply->errorString();
}

private:
QLabel *m_label;
QNetworkAccessManager m_nam;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"

Spitfire
13th December 2011, 09:46
"Dereference (http://www.cplusplus.com/doc/tutorial/pointers/)the pointer and insert a list item containing your icon (http://www.developer.nokia.com/Community/Wiki/How_to_use_QListWidget_and_QListWidgetItem)into the widget."

My answer is as vague as your question, but that's all you get without being more specific.

harish
13th December 2011, 10:08
Thank you Chris for your help it helped me a lot.