Results 1 to 7 of 7

Thread: Problems by inserting Images in a QLabel

  1. #1
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Exclamation Problems by inserting Images in a QLabel

    in Levels.h

    Qt Code:
    1. #ifndef MYWINDOW_H
    2. #define MYWINDOW_H
    3.  
    4. #include <QPushButton>
    5. #include <QMessageBox>
    6. #include <QMainWindow>
    7. #include <QHBoxLayout>
    8. #include "QScrollArea"
    9. #include "QImage"
    10. #include "QLabel"
    11. #include "QWidget"
    12. #include "QVBoxLayout"
    13. #include "QGroupBox"
    14. #include "QSpacerItem"
    15. #include "QList"
    16.  
    17. class Levels: public QMainWindow
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. Levels(){};
    23. ~ Levels(){};
    24. void erzeugen(Levels *level);
    25. void setzeBilder();
    26. private:
    27. QVBoxLayout *ramen;
    28. QVBoxLayout *layout;
    29. QImage *image;
    30. QList<QLabel *> labels;
    31. QPushButton *back;
    32. QWidget *central;
    33. QGroupBox *box;
    34. QScrollArea *area;
    35. QSpacerItem *space;
    36. QList<QString *> bilder;
    37.  
    38.  
    39. //public slots:
    40. };
    41. #endif
    To copy to clipboard, switch view to plain text mode 

    in levels.cpp
    Qt Code:
    1. #include "levels.h"
    2.  
    3. void Levels::erzeugen(Levels *level)
    4. {
    5. setzeBilder();
    6. central=new QWidget(this);
    7. ramen= new QVBoxLayout(central);
    8. setCentralWidget(central);
    9. box = new QGroupBox;
    10. back = new QPushButton(" ? Back");
    11. for(int i=0;i<12;i++)
    12. {
    13. labels << new QLabel();
    14. }
    15. for(int i=0;i<12;i++)
    16. {
    17. labels.at(i)->setBackgroundRole(QPalette::Dark);
    18. labels.at(i)->setAutoFillBackground(true);
    19. image = new QImage(bilder.at(i));//Muss im debug-Ordner sein!!!
    20. labels.at(i)->setPixmap(QPixmap::fromImage(*image));
    21. labels.at(i)->setMaximumHeight(image->height());
    22. labels.at(i)->setMaximumWidth(image->width());
    23. }
    24.  
    25. space = new QSpacerItem(1,10);
    26. for(int i=0;i<12;i++)
    27. {
    28. layout->addWidget(labels.at(i));
    29. }
    30.  
    31.  
    32. box->setLayout(layout);
    33. area = new QScrollArea;
    34. area->setWidgetResizable(false);
    35. area->setWidget(box);
    36.  
    37. }
    38. void Levels::setzeBilder()
    39. {
    40. for(int i=0;i<12;i++)
    41. {
    42. bilder << new QString;
    43. }
    44. bilder.at(0)=QString::fromStdString("l01.png");
    45. bilder.at(1)=QString::fromStdString("l02.png");
    46. bilder.at(2)=QString::fromStdString("l03.png");
    47. bilder.at(3)=QString::fromStdString("l04.png");
    48. bilder.at(4)=QString::fromStdString("l05.png");
    49. bilder.at(5)=QString::fromStdString("l06.png");
    50. bilder.at(6)=QString::fromStdString("l07.png");
    51. bilder.at(7)=QString::fromStdString("l08.png");
    52. bilder.at(8)=QString::fromStdString("l09.png");
    53. bilder.at(9)=QString::fromStdString("l10.png");
    54. bilder.at(10)=QString::fromStdString("l11.png");
    55. bilder.at(11)=QString::fromStdString("l12.png");
    56.  
    57. }
    To copy to clipboard, switch view to plain text mode 

    And i Get lots of Error-Messages ...(25) which are probably allways the same...
    Is there a way to set the list at the class where this class is called?

  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: Problems by inserting Images in a QLabel

    The errors (I guess) are basic C++:
    • bilder is a list of QString pointers. You cannot assign a QString, which is what QString::fromStdString() returns, to a pointer.
    • QList<T>::at() returns a const reference to an item in the list: you cannot assign a value to it. You would need to use operator[]


    You probably want a QList<QString> or QStringList. You do not need to use QString::fromStdString(); you can construct a QString directly from a const char*

    Is there a way to set the list at the class where this class is called?
    I do not know what this means.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems by inserting Images in a QLabel

    Since you don't bother to tell us what the error messages are, how are we supposed to give you any help?

    I will point out at least one other serious error in your code: The constructor for your "Levels" calss is wrong and should be:

    Qt Code:
    1. Levels::Levels( QWidget * parent )
    2. : QMainWindow( parent )
    3. {
    4. }
    To copy to clipboard, switch view to plain text mode 

    In addition, the "bilder" variable is defined incorrectly. It should be QList<QString>, not QList<QString *>. This will cause compiler errors in the "setzeBilder" function because QString::fromStdString() returns a QString, not a QString *.

    And why does your "erzeugen()" method have a Levels * argument that is never used? Looks to me like you don't understand how to create and initialize a QWidget-based class. Everything that you are doing in these two extra methods should be called from your class constructor, not externally, like this:

    Qt Code:
    1. Levels::Levels( QWidget * parent )
    2. : QMainWindow( parent )
    3. {
    4. setzeBilder();
    5. erzeugen(); // Note, "Levels *" argument is unnecessary and is removed.
    6. }
    To copy to clipboard, switch view to plain text mode 

    And there are probably 6 or 7 more serious errors and memory leaks in your code as well which I will let you discover on your own.

    Is there a way to set the list at the class where this class is called?
    I have no idea what this question means.

  4. #4
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Problems by inserting Images in a QLabel

    Quote Originally Posted by joshy198 View Post
    Is there a way to set the list at the class where this class is called?
    If you mean to create an object and initialize a list when an object of your class that contains that list is being created then you can probably use a constructor of your class with one more parameter.

  5. #5
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: Problems by inserting Images in a QLabel

    levels.h
    Qt Code:
    1. #ifndef MYWINDOW_H
    2. #define MYWINDOW_H
    3.  
    4. #include <QPushButton>
    5. #include <QMessageBox>
    6. #include <QMainWindow>
    7. #include <QHBoxLayout>
    8. #include "QScrollArea"
    9. #include "QImage"
    10. #include "QLabel"
    11. #include "QWidget"
    12. #include "QVBoxLayout"
    13. #include "QGroupBox"
    14. #include "QSpacerItem"
    15. #include "QList"
    16.  
    17. class Levels: public QMainWindow
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. Levels(){};
    23. ~ Levels(){};
    24. void erzeugen();
    25. void setzeBilder();
    26. private:
    27. QVBoxLayout *ramen;
    28. QVBoxLayout *layout;
    29. QImage *image;
    30. QList<QLabel *> labels;
    31. QPushButton *back;
    32. QWidget *central;
    33. QGroupBox *box;
    34. QScrollArea *area;
    35. QSpacerItem *space;
    36. QList<QString> bilder;
    37.  
    38.  
    39. //public slots:
    40. };
    41. #endif
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "levels.h"
    2.  
    3. void Levels::erzeugen()
    4. {
    5. setzeBilder();
    6. central=new QWidget(this);
    7. ramen= new QVBoxLayout(central);
    8. setCentralWidget(central);
    9. box = new QGroupBox;
    10. back = new QPushButton(" ? Back");
    11. for(int i=0;i<12;i++)
    12. {
    13. labels << new QLabel();
    14. }
    15. for(int i=0;i<12;i++)
    16. {
    17. labels.at(i)->setBackgroundRole(QPalette::Dark);
    18. labels.at(i)->setAutoFillBackground(true);
    19. image = new QImage(bilder.at(i));//Muss im debug-Ordner sein!!!
    20. labels.at(i)->setPixmap(QPixmap::fromImage(*image));
    21. labels.at(i)->setMaximumHeight(image->height());
    22. labels.at(i)->setMaximumWidth(image->width());
    23. }
    24.  
    25. space = new QSpacerItem(1,10);
    26. for(int i=0;i<12;i++)
    27. {
    28. layout->addWidget(labels.at(i));
    29. }
    30.  
    31.  
    32. box->setLayout(layout);
    33. area = new QScrollArea;
    34. area->setWidgetResizable(false);
    35. area->setWidget(box);
    36.  
    37. }
    38. void Levels::setzeBilder()
    39. {
    40. bilder.at(0)=QString::fromStdString("l01.png");
    41. bilder.at(1)=QString::fromStdString("l02.png");
    42. bilder.at(2)=QString::fromStdString("l03.png");
    43. bilder.at(3)=QString::fromStdString("l04.png");
    44. bilder.at(4)=QString::fromStdString("l05.png");
    45. bilder.at(5)=QString::fromStdString("l06.png");
    46. bilder.at(6)=QString::fromStdString("l07.png");
    47. bilder.at(7)=QString::fromStdString("l08.png");
    48. bilder.at(8)=QString::fromStdString("l09.png");
    49. bilder.at(9)=QString::fromStdString("l10.png");
    50. bilder.at(10)=QString::fromStdString("l11.png");
    51. bilder.at(11)=QString::fromStdString("l12.png");
    52.  
    53. }
    To copy to clipboard, switch view to plain text mode 

    Errors

    problem.jpg

    @ZikO
    If i make the QList public and include the levels.h in another class (the class from where levels is called), can i add elements to the QList directly in the class where i initialize levels?
    @d_stranz
    I don't really know how to initialize it your way. I know I'm quite bad at c++
    Normaly I'm develloping in java or c# (much easyer, but my nokia device does't support java oder c#)

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

    Default Re: Problems by inserting Images in a QLabel

    Please read again what Chris posted regarding the use of QList::at()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    joshy198 (22nd September 2012)

  8. #7
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Problems by inserting Images in a QLabel

    Quote Originally Posted by joshy198 View Post
    Is there a way to set the list at the class where this class is called?
    Quote Originally Posted by joshy198 View Post
    If i make the QList public and include the levels.h in another class (the class from where levels is called), can i add elements to the QList directly in the class where i initialize levels?
    I think I know what you meant by writing the first sentence. You probably mean to initialize a list in run-time. I don't think it can help if you do what you say in the second sentence. To be honest, Chris already explained this and another problem and wrote the complete answer.


    I read your first post along with the code and the errors that you have provided and I can only add that in C++ const is used in order to prevent from changing or assigning anything to an object returned from any function as a reference; it prevents from using such objects as so-called L-value. QList::at() returns a const reference:
    Qt Code:
    1. const T & QList::at ( int i ) const
    To copy to clipboard, switch view to plain text mode 
    This is why the compiler complains about discarding qualifier here:
    Qt Code:
    1. bilder.at(0)=QString::fromStdString("l01.png");
    To copy to clipboard, switch view to plain text mode 
    Bilder.at(0) etc. is at the left of "=". But bear in mind you have also the other problem that Chris also refers to.

  9. The following user says thank you to ZikO for this useful post:

    joshy198 (24th September 2012)

Similar Threads

  1. Qlabel and images
    By eltecprogetti in forum Qt Programming
    Replies: 3
    Last Post: 5th March 2012, 08:39
  2. To draw images in QLabel
    By augusbas in forum Qt Programming
    Replies: 5
    Last Post: 11th October 2010, 06:27
  3. Inserting Images in the form
    By joseph_mathew in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2009, 12:16
  4. Images in QLabel
    By qball2k5 in forum Qt Programming
    Replies: 5
    Last Post: 6th March 2006, 21:28

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.