Results 1 to 6 of 6

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Symbian S60

    Post 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
    Last edited by harish; 13th December 2011 at 07:30. Reason: Edit

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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:
    Qt Code:
    1. void Homescreen::replyFinished(QNetworkReply *reply)
    2. {
    3. QImageReader imageReader(reply);
    4. myImage= imageReader.read();
    5. *pix = QPixmap::fromImage(myImage); // What type and where is pix declared? Is the pointer initialised? Why use a pointer at all?
    6. QLabel *label; // An uninitialised pointer to a QLabel. OK, but surely you wanted a label in the UI?
    7. label->setPixmap(*pix); // BOOM!! You cannot use an uninitialised pointer and expect anything but grief.
    8. }
    To copy to clipboard, switch view to plain text mode 
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    Nov 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Symbian S60

    Post 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?
    Last edited by harish; 13th December 2011 at 07:30. Reason: Modify

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to get the value from the pointer using QPixmap and use it in QListwidget?

    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:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtNetwork>
    3. #include <QDebug>
    4.  
    5. class MainWindow: public QMainWindow {
    6. Q_OBJECT
    7. public:
    8. MainWindow(QWidget *p = 0): QMainWindow(p) {
    9. m_label = new QLabel(this);
    10. setCentralWidget(m_label);
    11. resize(320, 240);
    12.  
    13. connect(&m_nam, SIGNAL(finished(QNetworkReply*)), SLOT(finished(QNetworkReply*)));
    14. QNetworkRequest request(QUrl("http://www.qtcentre.org/images/qtcentre/QtCentre.png"));
    15. (void) m_nam.get(request); // Deliberately ignoring return value
    16. }
    17. public slots:
    18. void finished(QNetworkReply *reply) {
    19. if (reply->error() == QNetworkReply::NoError) {
    20. QByteArray data( reply->readAll() );
    21. QPixmap pixmap;
    22. bool ok = pixmap.loadFromData( data );
    23. m_label->setPixmap( pixmap );
    24. qDebug() << "Data size ==" << data.size();
    25. qDebug() << "Conversion OK ==" << ok;
    26. qDebug() << "Pixmap is null ==" << pixmap.isNull();
    27. }
    28. else
    29. qDebug() << "Network error" << reply->errorString();
    30. }
    31.  
    32. private:
    33. QLabel *m_label;
    34. QNetworkAccessManager m_nam;
    35. };
    36.  
    37. int main(int argc, char *argv[])
    38. {
    39. QApplication app(argc, argv);
    40.  
    41. MainWindow m;
    42. m.show();
    43. return app.exec();
    44. }
    45. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  5. #5
    Join Date
    Nov 2011
    Posts
    45
    Qt products
    Qt4
    Platforms
    Symbian S60

    Post 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.

  6. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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.

Similar Threads

  1. Replies: 2
    Last Post: 1st April 2011, 09:32
  2. Replies: 1
    Last Post: 4th December 2010, 17:20
  3. Replies: 4
    Last Post: 28th August 2008, 13:13
  4. Replies: 1
    Last Post: 21st August 2008, 07:44
  5. Replies: 5
    Last Post: 9th April 2007, 14:26

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.