How to get the value from the pointer using QPixmap and use it in QListwidget?
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
Re: How to get the value from the pointer using QPixmap and use it in QListwidget?
Repeat after me: I will use [code][/code] 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:
Code:
void Homescreen::replyFinished(QNetworkReply *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.
}
Re: How to get the value from the pointer using QPixmap and use it in QListwidget?
How to get the value from the pointer using QPixmap and use it in QListwidget?
Re: How to get the value from the pointer using QPixmap and use it in QListwidget?
Quote:
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:
Code:
#include <QtGui>
#include <QtNetwork>
#include <QDebug>
Q_OBJECT
public:
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) {
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:
QNetworkAccessManager m_nam;
};
int main(int argc, char *argv[])
{
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"
Re: How to get the value from the pointer using QPixmap and use it in QListwidget?
"Dereference the pointer and insert a list item containing your icon into the widget."
My answer is as vague as your question, but that's all you get without being more specific.
Re: How to get the value from the pointer using QPixmap and use it in QListwidget?
Thank you Chris for your help it helped me a lot.