Results 1 to 15 of 15

Thread: Function not receiving QPixmap

  1. #1
    Join Date
    May 2008
    Location
    USA
    Posts
    22
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Function not receiving QPixmap

    Hi,

    I subclassed QLabel so that it supports the clicked() event and I'm trying to create a small image viewer.
    The problem is that my setImage function is not receiving the image properly from the connect statement. Instead of actually setting the image, the QLabel sets itself blank (ie it doesnt show anything). I would really appreciate any help on what am I doing wrong.


    Qt Code:
    1. // connecting signal
    2. connect(labels[imagenum], SIGNAL(clicked(QPixmap&)), this, SLOT(setImage(QPixmap&)));
    3.  
    4. void MainWindow::setImage(QPixmap &pic)
    5. {
    6. // mainimage is a QLabel
    7. mainimage->setPixmap(pic);
    8. mainimage->repaint();
    9. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // imagelabel.cpp
    2.  
    3. #include <QtGui>
    4. #include "imagelabel.h"
    5.  
    6. ImageLabel::ImageLabel(QWidget *parent) : QLabel(parent)
    7. {
    8. connect(this, SIGNAL(clicked(QPixmap&)), this, SLOT(slotClicked()));
    9. }
    10.  
    11. void ImageLabel::slotClicked()
    12. {
    13. qDebug() << "clicked";
    14. }
    15.  
    16. void ImageLabel::mousePressEvent(QMouseEvent *event)
    17. {
    18. emit clicked(local_pixmap);
    19. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // imagelabel.h
    2.  
    3.  
    4. #ifndef IMAGELABEL_H
    5. #define IMAGELABEL_H
    6.  
    7. #include <QLabel>
    8.  
    9. class ImageLabel : public QLabel
    10. {
    11. Q_OBJECT
    12. public:
    13. ImageLabel (QWidget *parent = 0);
    14. ~ImageLabel() {}
    15.  
    16. signals:
    17. void clicked(QPixmap&);
    18.  
    19. public slots:
    20. void slotClicked();
    21.  
    22. protected:
    23. void mousePressEvent(QMouseEvent *event);
    24.  
    25. private:
    26. QPixmap local_pixmap;
    27. };
    28.  
    29. #endif // IMAGELABEL_H
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot in advance for your help!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Function not receiving QPixmap

    "QPixmap &" and "const QPixmap &" are two very different things. First of all I'd transmit the latter instead of the former. Second of all I would emit clicked() on mouseRelease and not mousePress. Third of all I would enable console support (CONFIG+=console) and see if Qt spits out any warnings. Fourth of all I would use QListView and QStandardItemModel instead of a bunch of labels.

  3. #3
    Join Date
    May 2008
    Location
    USA
    Posts
    22
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Function not receiving QPixmap

    Hi Wysota,

    Thank you for your answer.
    I made the corrections that you suggested but I still have no luck

    Regarding your suggestion to use a QListView, I am actually using a QScrollArea and a QHBoxLayout to contain the labels.

    I enabled CONFIG+=console and at the beggining it was spitting out some errors but I think I corrected all of them since now it is not spitting any however i still have the same problem.

    Did I do right the corrections that you suggested?

    Qt Code:
    1. connect(labels[imagenum], SIGNAL(clicked(const QPixmap&)), this, SLOT(setImage(const QPixmap &)));
    2.  
    3. void MainWindow::setImage(const QPixmap &pic)
    4. {
    5. mainimage->setPixmap(pic);
    6. mainimage->repaint();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // imagelabel.cpp
    2. #include <QtGui>
    3. #include "imagelabel.h"
    4.  
    5. ImageLabel::ImageLabel(QWidget *parent) : QLabel(parent)
    6. {
    7. connect(this, SIGNAL(clicked(const QPixmap&)), this, SLOT(slotClicked()));
    8. }
    9.  
    10. void ImageLabel::slotClicked()
    11. {
    12. qDebug() << "clicked";
    13. }
    14.  
    15. void ImageLabel::mouseReleaseEvent(QMouseEvent *event)
    16. {
    17. emit clicked(local_pixmap);
    18. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // imagelabel.h
    2. #ifndef IMAGELABEL_H
    3. #define IMAGELABEL_H
    4.  
    5. #include <QLabel>
    6.  
    7. class ImageLabel : public QLabel
    8. {
    9. Q_OBJECT
    10. public:
    11. ImageLabel (QWidget *parent = 0);
    12. ~ImageLabel() {}
    13.  
    14. signals:
    15. void clicked(const QPixmap&);
    16.  
    17. public slots:
    18. void slotClicked();
    19.  
    20. protected:
    21. void mouseReleaseEvent(QMouseEvent *event);
    22.  
    23. private:
    24. QPixmap local_pixmap;
    25. };
    26.  
    27. #endif // IMAGELABEL_H
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance.

  4. #4
    Join Date
    May 2008
    Location
    USA
    Posts
    22
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Function not receiving QPixmap

    I think my error is somehow in my connect statement, because I just added:

    Qt Code:
    1. if (pic.isNull())
    2. qDebug() << "it's null";
    To copy to clipboard, switch view to plain text mode 
    to my setImage function and its returning null...
    so i guess i'm doing something wrong in my connect statement.

    is that the right way of passing the QPixmap attribute of a QLabel?
    connect(label, SIGNAL(clicked(const QPixmap&)), this, SLOT(setImage(const QPixmap &)))

    Thanks in advance.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Function not receiving QPixmap

    I don't think you assign "local_pixmap" anywhere. Anyway I would really suggest to use QListView. You may visit http://doc.trolltech.com/qq/ and grab example sources from my article from issue #27 regarding responsive GUIs. The article is about something completely different but there is a good piece of code you can see. Run the watcher example and see what you can obtain easily using a listview. Reacting to clicks is just a matter of connecting to the clicked() or activated() signal of the view.

    Here is a small teaser:
    watcher.jpg

  6. #6
    Join Date
    May 2008
    Location
    USA
    Posts
    22
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Function not receiving QPixmap

    What do you mean by not assigning local_pixmap anywhere? Isn't the emit emit clicked(local_pixmap) enough?

    And I printed out the Qt Quarterly article that you wrote, its a very interesting read, my app is actually suffering a little bit from that so I'll definitely implement one of your solutions !!! The watcher example is really nice and I will reconsider recoding that part using a ListView like you say.

    I'm not sure what else to do with this. I have tried everything with no luck.
    Also, a friend of mine suggested using QPixmap * instead of &. I tried it but never got it to work, the compiler was complaining and never liked the type QPixmap *.


    Thanks a lot in advance for your help.

  7. #7
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Function not receiving QPixmap

    What do you mean by not assigning local_pixmap anywhere? Isn't the emit emit clicked(local_pixmap) enough?
    As you have only:
    Qt Code:
    1. QPixmap local_pixmap;
    To copy to clipboard, switch view to plain text mode 
    it means your pixmap is blank. Are you setting any content to it?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  8. #8
    Join Date
    May 2008
    Location
    USA
    Posts
    22
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Function not receiving QPixmap

    Quote Originally Posted by faldżip View Post
    As you have only:
    Qt Code:
    1. QPixmap local_pixmap;
    To copy to clipboard, switch view to plain text mode 
    it means your pixmap is blank. Are you setting any content to it?
    The content is being passed by my connect statement:
    Qt Code:
    1. connect(labels[imagenum], SIGNAL(clicked(QPixmap&)), this, SLOT(setImage(QPixmap&)));
    To copy to clipboard, switch view to plain text mode 

    Which is supposed to copy the Pixmap from one QLabel into another.

  9. #9
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Function not receiving QPixmap

    Quote Originally Posted by nmuntz View Post
    The content is being passed by my connect statement:
    Qt Code:
    1. connect(labels[imagenum], SIGNAL(clicked(QPixmap&)), this, SLOT(setImage(QPixmap&)));
    To copy to clipboard, switch view to plain text mode 

    Which is supposed to copy the Pixmap from one QLabel into another.
    Yes, but you are sending local_pixmap which is blank. Maybe more clear.

    You have your ImageLabel. In there you have local_pixmap. Your local_pixmap isn't set anywhere, but constructed with the default QPixmap constructor. Then you are sending that blank pixmap with your signal clicked(QPixmap&) to the slot setImage(QPixmap&) which is setting received (blank) pixmap. So your code is working. It is doing exactly what you wrote in your source files.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  10. #10
    Join Date
    May 2008
    Location
    USA
    Posts
    22
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Function not receiving QPixmap

    Quote Originally Posted by faldżip View Post
    Yes, but you are sending local_pixmap which is blank. Maybe more clear.

    You have your ImageLabel. In there you have local_pixmap. Your local_pixmap isn't set anywhere, but constructed with the default QPixmap constructor. Then you are sending that blank pixmap with your signal clicked(QPixmap&) to the slot setImage(QPixmap&) which is setting received (blank) pixmap. So your code is working. It is doing exactly what you wrote in your source files.
    Ahhh! That makes sense. You are right !
    Now I'm not really sure how to set local_pixmap to be the pixmap sent by the QLabel.
    Should that be part of my ImageLabel::slotClicked() function ? And would I pass that from my MainWindow into ImageLabel ?
    Would something like this work?
    Qt Code:
    1. ImageLabel *im = new ImageLabel;
    2. im.localpixmap = labels[imagenum]->pixmap();
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Function not receiving QPixmap

    hmm I can't really understand clearly your code. What's the labels array? As I understand from previous code fragments its something like this:
    Qt Code:
    1. ImageLabel * labels[NUMBER];
    To copy to clipboard, switch view to plain text mode 

    Now, tell me, what pixmap you want to set where? If you want to have a bunch of ImageLabels with pixmap set on them you can do it like this:
    Qt Code:
    1. for (int i = 0; i < NUMBER; ++i)
    2. {
    3. labels[i]->setPixmap(some_nice_pixmap);
    4. }
    To copy to clipboard, switch view to plain text mode 
    And that labels will be shown in your widget with that pixmap.
    If it's want you want to, you can send it like here:
    Qt Code:
    1. emit clicked(*pixmap());
    To copy to clipboard, switch view to plain text mode 
    And you don't need any additional local_pixmap;
    Last edited by faldzip; 3rd February 2009 at 00:57. Reason: Added * in emit clicked(*pixmap()); as it should be there
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  12. The following user says thank you to faldzip for this useful post:

    nmuntz (2nd February 2009)

  13. #12
    Join Date
    May 2008
    Location
    USA
    Posts
    22
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Function not receiving QPixmap

    Quote Originally Posted by faldżip View Post
    hmm I can't really understand clearly your code. What's the labels array? As I understand from previous code fragments its something like this:
    Qt Code:
    1. ImageLabel * labels[NUMBER];
    To copy to clipboard, switch view to plain text mode 

    Now, tell me, what pixmap you want to set where? If you want to have a bunch of ImageLabels with pixmap set on them you can do it like this:
    Qt Code:
    1. for (int i = 0; i < NUMBER; ++i)
    2. {
    3. labels[i]->setPixmap(some_nice_pixmap);
    4. }
    To copy to clipboard, switch view to plain text mode 
    And that labels will be shown in your widget with that pixmap.
    If it's want you want to, you can send it like here:
    Qt Code:
    1. emit clicked(pixmap());
    To copy to clipboard, switch view to plain text mode 
    And you don't need any additional local_pixmap;
    Qt Code:
    1. QList<ImageLabel *> labels;
    2. int imagenum = 0;
    3. foreach(const QString &path, files) {
    4. QPixmap px(path);
    5. labels.append(new ImageLabel);
    6. labels[imagenum]->setPixmap(px.scaledToHeight(90));
    7. qhbox->addWidget(labels[imagenum]);
    8. connect(labels[imagenum], SIGNAL(clicked(const QPixmap&)), this, SLOT(setImage(const QPixmap &)));
    9. imagenum++;
    10. }
    To copy to clipboard, switch view to plain text mode 

    thats the relevant code. what it does is load all the images in a dir as thumnails and put them in a qhbox. and what I'm actually trying to do is that when I click on any of these ImageLabels it will load up the image on q mainimage label which is the full size image.

    Thats why I subclassed QLabel so that it supports the clicked() event.

    Thanks a lot for all your help!

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Function not receiving QPixmap

    You are overcomplicating things. If you want a clickable label, all you need to do is this:

    Qt Code:
    1. class ClickableLabel : public QLabel {
    2. Q_OBJECT
    3. public:
    4. ClickableLabel(QWidget *parent=0) : QLabel(parent){}
    5. signals:
    6. void clicked(const QPixmap &);
    7. void clicked(const QString &);
    8. protected:
    9. void mouseReleaseEvent(QMouseEvent *e){
    10. if(e->button()==Qt::LeftButton){
    11. if(pixmap()) emit clicked(*pixmap()); // has a pixmap set
    12. else if(!text.isEmpty()) emit clicked(text()); // has text set
    13. else e->ignore(); // is empty, let's ignore the event, maybe the parent will handle it
    14. } else e->ignore(); // wrong button released - ignore
    15. }
    16. };
    To copy to clipboard, switch view to plain text mode 

    You don't need to store the pixmap anywhere as QLabel already does that.

  15. The following user says thank you to wysota for this useful post:

    nmuntz (3rd February 2009)

  16. #14
    Join Date
    May 2008
    Location
    USA
    Posts
    22
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Function not receiving QPixmap

    Wysota:

    Thank worked perfectly !! Thank you very much!!!
    Good call before... I was obviously doing it wrong and I overcomplicated things.

    Thank you very much to both of you for all your help!

  17. #15
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Function not receiving QPixmap

    Ahh! I forgot "*" ...
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.