Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: pixmap or qimage second time usage in widgets ( platform S60 )

  1. #1
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Question pixmap or qimage second time usage in widgets ( platform S60 )

    I have come across a scenario where I have to load the same pixmap / same QImage onto two different widgets ( belonging to the same mainwindow ).

    First time I load a pixmap 1 on widget 1 , it works fine.
    Second time if I try to load the same pixmap ( or QImage ) on another widget ( say widget 2), the code just throws some error ( saying memory full ) or does not load the pixmap on widget 2.

    Ultimately I end up duplicating the png files i use to load the pixmaps, giving them distinct file names. Is there a solution to avoid this loading problem so that the I can use the same png file to be loaded any number of times ?

  2. #2
    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: pixmap or qimage second time usage in widgets ( platform S60 )

    Why not reuse the same image instead of loading it again?
    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.


  3. #3
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Question Re: pixmap or qimage second time usage in widgets ( platform S60 )

    I faced the same issue with the usage of the same pixmap more than once when adding it to a model class ( without loading ). Is there a solution to this ?

  4. #4
    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: pixmap or qimage second time usage in widgets ( platform S60 )

    Please provide a minimal compilable example reproducing the problem.
    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.


  5. #5
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Question Re: pixmap or qimage second time usage in widgets ( platform S60 )

    The code segment for this is as shown below -

    Qt Code:
    1. gridLayout = new QGridLayout(mainwindow);
    2. gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    3. gridLayout->setContentsMargins(0, 0, 0, 0);
    4. label = new QLabel(gridLayout);
    5. label->setObjectName(QString::fromUtf8("label"));
    6. label->setPixmap(QPixmap(QString::fromUtf8("../folder/user_group_icon.bmp")));
    7.  
    8. gridLayout->addWidget(label, 0, 0, 1, 1);
    9.  
    10. label_2 = new QLabel(gridLayout);
    11. label_2->setObjectName(QString::fromUtf8("label_2"));
    12. label_2->setPixmap(QPixmap(QString::fromUtf8("../folder/user_group_icon.bmp")));
    13.  
    14. gridLayout->addWidget(label_2, 0, 1, 1, 1);
    To copy to clipboard, switch view to plain text mode 

    Here the gridLayout is added to a mainwindow & contains 2 labels. Both set the same bmp file as pixmap. The first label displays it but the second label does not. This problem occurs ONLY ON S60 ( not on windows ).

    Has anyone faced this issue before?

  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: pixmap or qimage second time usage in widgets ( platform S60 )

    What if you do this:
    Qt Code:
    1. QPixmap pixmap(QString::fromUtf8("../folder/user_group_icon.bmp"));
    2. gridLayout = new QGridLayout(mainwindow);
    3. gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    4. gridLayout->setContentsMargins(0, 0, 0, 0);
    5. label = new QLabel(gridLayout);
    6. label->setObjectName(QString::fromUtf8("label"));
    7. label->setPixmap(pixmap);
    8.  
    9. gridLayout->addWidget(label, 0, 0, 1, 1);
    10.  
    11. label_2 = new QLabel(gridLayout);
    12. label_2->setObjectName(QString::fromUtf8("label_2"));
    13. label_2->setPixmap(pixmap);
    14.  
    15. gridLayout->addWidget(label_2, 0, 1, 1, 1);
    To copy to clipboard, switch view to plain text mode 
    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. #7
    Join Date
    Jan 2011
    Location
    Cairo, Egypt
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: pixmap or qimage second time usage in widgets ( platform S60 )

    Well, I have the same problem, and didn't find a solution till now. I move a lot between Widgets that need Pixmaps to paint in the background, view in labels, and others for some downloaded news. If I go back and forth between widgets, after about 10 times the applications freezes and sometimes the message of "Memory Full" appears. I traced this problem for days and notices the console output. It goes fine when going back and forth, until the time that crashes:

    Qt Code:
    1. [Qt Message] My Headlines Widget Message: Loading pixmaps in the constructor
    2. [Qt Message] QPainter::begin: Cannot paint on a null pixmap
    3. [Qt Message] QPainter::begin: Cannot paint on a null pixmap
    4. [Qt Message] QPainter::begin: Cannot paint on a null pixmap
    5. [Qt Message] My Headlines Widget Message: drawing bg in the paintEvent
    6. [Qt Message] My Headlines Widget Message: drawing bannar in the paintEvent
    7. [Qt Message] My Headlines Widget Message: drawing title in the paintEvent
    8. [Qt Message] QPainter::end: Painter not active, aborted
    9. [Qt Message] QPainter::begin: Cannot paint on a null pixmap
    10. [Qt Message] QPainter::begin: Cannot paint on a null pixmap
    11. [Qt Message] QPainter::pen: Painter not active
    12. [Qt Message] QPainter::setPen: Painter not active
    13. [Qt Message] QPainter::setPen: Painter not active
    14. [Qt Message] QPainter::setPen: Painter not active
    15. [Qt Message] QPainter::setPen: Painter not active
    16. [Qt Message] QPainter::setPen: Painter not active
    17. [Qt Message] QPainter::begin: Cannot paint on a null pixmap
    To copy to clipboard, switch view to plain text mode 

    And messages keep popping in the console until a 'data exception' occurs and application crashes.
    Qt Code:
    1. Process 1106, thread 1107 stopped at 0x79573c1e: A data abort exception has occurred.
    2. Finished.
    To copy to clipboard, switch view to plain text mode 

    I tried increasing the QPixmapCacheLimit from 1024 KB to 4096 KB: and it only helped to increase the number of trials before crashing.
    I tried clearing the QPixmapCache before closing any widget: did not help, I got worse.

    Any other suggestions?


    Added after 38 minutes:


    An update:
    I used this code instead

    Qt Code:
    1. if (!QPixmapCache::find("background", &m_bg)) {
    2. m_bg.load(":/bg.png");
    3. QPixmapCache::insert("background", m_bg);
    4. }
    5.  
    6. if (!QPixmapCache::find("bannar", &m_bannar)) {
    7. m_bannar.load(":/bannar.png");
    8. QPixmapCache::insert("bannar", m_bannar);
    9. }
    10.  
    11. if (!QPixmapCache::find(m_sectionId, &m_title)) {
    12. m_title.load(":/titles/"+m_sectionId+".png");
    13. QPixmapCache::insert(m_sectionId, m_title);
    14. }
    15. // m_bannar.load(":/bannar.png");
    16. // m_bg.load(":/bg.png");
    17. // m_title.load(":/titles/"+m_sectionId+".png");
    To copy to clipboard, switch view to plain text mode 

    but no effect at all
    Last edited by Mah.Adly; 13th January 2011 at 12:39.

  8. #8
    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: pixmap or qimage second time usage in widgets ( platform S60 )

    Are you using threads?
    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.


  9. #9
    Join Date
    Jan 2011
    Location
    Cairo, Egypt
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: pixmap or qimage second time usage in widgets ( platform S60 )

    No, only signals/slots

  10. #10
    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: pixmap or qimage second time usage in widgets ( platform S60 )

    Please post the backtrace after a crash.
    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.


  11. #11
    Join Date
    Jan 2011
    Location
    Cairo, Egypt
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: pixmap or qimage second time usage in widgets ( platform S60 )

    Quote Originally Posted by wysota View Post
    Please post the backtrace after a crash.
    Where can I find it?

  12. #12
    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: pixmap or qimage second time usage in widgets ( platform S60 )

    In your debugger. It's also sometimes called a "stack trace" and contains a list of function calls leading to the crash.
    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.


  13. #13
    Join Date
    Jan 2011
    Location
    Cairo, Egypt
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: pixmap or qimage second time usage in widgets ( platform S60 )

    I hope I got it right:

    the application has this sequence:
    NewsHeadlines ----(choose item)----> NewsDetails
    NewsDetails ----(go back)----> NewsHeadlines

    this is what happens at the constructor of NewsDetails when an item is selected in NewsHeadlines


    and when going back


    and this is when inside the paintEvent of NewsDetails (and similar one in NewsHeadlines), whether it works fine or crashes

  14. #14
    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: pixmap or qimage second time usage in widgets ( platform S60 )

    This is not during a crash. This is a trap on some breakpoint. Let the program crash and then dump the backtrace. In textual form, not graphical (open context menu and copy to clipboard).
    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.


  15. #15
    Join Date
    Jan 2011
    Location
    Cairo, Egypt
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: pixmap or qimage second time usage in widgets ( platform S60 )

    I tried but couldn't get more than this:
    Qt Code:
    1. Thread 1 (Thread 922):
    2. #0 0x795ac6e4 in ?? ()
    3. #1 0x79934fbe in ?? ()
    4. Backtrace stopped: previous frame identical to this frame (corrupt stack?)
    To copy to clipboard, switch view to plain text mode 
    only for the two question marks in the previous images. Once the application crashes, there are no more functions or variables to watch.

    So I tried a new way to save some time. I made a dummy project with two windows. Each window has only one button to close itself and open the other window. The code is not more than:
    Qt Code:
    1. void WindowA::on_pushButton_clicked()
    2. {
    3. WindowB *b = new WindowB();
    4. b->showMaximized();
    5. this->close();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void WindowB::on_pushButton_clicked()
    2. {
    3. WindowA *a = new WindowA();
    4. a->showMaximized();
    5. this->close();
    6. }
    To copy to clipboard, switch view to plain text mode 

    And tested this application for about 30 times then it crashed just the same as my previous example. And the same QPainter messages.

    I attached the example
    Attached Files Attached Files
    Last edited by Mah.Adly; 16th January 2011 at 16:53.

  16. #16
    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: pixmap or qimage second time usage in widgets ( platform S60 )

    Maybe you are running out of memory? You have a serious memory leak - you should be reusing old objects which you are not deleting instead of spawning new ones each time a button is clicked.

    An example implementation follows. It's a bit complicated as it uses a state machine but I wanted the code to be minimalisticly short.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv){
    4. QApplication app(argc, argv);
    5.  
    6. QWidget w1, w2;
    7. QPushButton *b1 = new QPushButton("Change 1");
    8. QPushButton *b2 = new QPushButton("Change 2");
    9. QVBoxLayout *l1 = new QVBoxLayout(&w1);
    10. QVBoxLayout *l2 = new QVBoxLayout(&w2);
    11. l1->addWidget(b1);
    12. l2->addWidget(b2);
    13.  
    14. QStateMachine machine;
    15. QState *s1 = new QState; // state: w1 visible
    16. QState *s2 = new QState; // state: w2 visible
    17.  
    18. s1->assignProperty(&w1, "visible", true);
    19. s1->assignProperty(&w2, "visible", false);
    20. s2->assignProperty(&w1, "visible", false);
    21. s2->assignProperty(&w2, "visible", true);
    22.  
    23. s1->addTransition(b1, SIGNAL(clicked()), s2); // [ s1, b1.clicked() ] -> {s2}
    24. s2->addTransition(b2, SIGNAL(clicked()), s1); // [ s2, b2.clicked() ] -> {s1}
    25.  
    26. machine.addState(s1);
    27. machine.addState(s2);
    28. machine.setInitialState(s1); // start with w1 visible
    29. machine.start();
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 
    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.


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

    Mah.Adly (26th January 2011)

  18. #17
    Join Date
    Jan 2011
    Location
    Cairo, Egypt
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: pixmap or qimage second time usage in widgets ( platform S60 )

    Thanks for this idea. I worked with something like this for while using SlidingStackedWidget. But of course I had to find out "Why?"

    A new way appeared. From Qt docs:
    "The widget is hidden if it accepts the close event."
    So that's why when I open and close many widgets/windows the application crashes and a message of "Memory Full" appears.

    Ar first, I had two identical solutions:
    1- After closing each widget/window:
    Qt Code:
    1. this->close();
    2. this->deleteLater();
    To copy to clipboard, switch view to plain text mode 

    2- Setting an attribute to the widget in the constructor:
    Qt Code:
    1. this->setAttribute(Qt::WA_DeleteOnClose);
    To copy to clipboard, switch view to plain text mode 

    This way the widget will be deleted not only hidden. These two ways have one flaw till now. When I exit the application, a message appears in the terminal (in case of S60) that "a data abort exception has occurred" or a message appears to user (Desktop) that "the application has stopped working".

    And after a couple of trials, I put the
    Qt Code:
    1. this->deleteLater();
    To copy to clipboard, switch view to plain text mode 
    in the destructor and is working fine till now

    Qt Code:
    1. WindowB::~WindowB()
    2. {
    3. delete ui;
    4. this->deleteLater();
    5. }
    To copy to clipboard, switch view to plain text mode 

  19. #18
    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: pixmap or qimage second time usage in widgets ( platform S60 )

    Putting this->deleteLater() in a destructor doesn't make any sense. It's practically a no-op. If you have identical windows you are swapping between then why do you create new ones each time instead of reusing the existing ones?
    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.


  20. #19
    Join Date
    Jan 2011
    Location
    Cairo, Egypt
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: pixmap or qimage second time usage in widgets ( platform S60 )

    Quote Originally Posted by wysota View Post
    Putting this->deleteLater() in a destructor doesn't make any sense. It's practically a no-op.
    You're totally right about this one. This was totally useless, my fault.

    I'm trying to find a solution because a user can navigate between *many* windows and open each possible one, so I'm trying to avoid this memory problem.. In this part of the appilcation I went into an example of swapping. But there are many more. So loading all windows and switching between them is my last choice.

  21. #20
    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: pixmap or qimage second time usage in widgets ( platform S60 )

    "Many" means how many? 1000? You can always settle for a solution where you cache the recently used windows, something like:
    Qt Code:
    1. QCache<int, QWidget> cache;
    2. cache.setMaxCost(10); // cache last 10 windows
    3. //...
    4. void X::openWindow(int id) {
    5. QWidget *w = cache.object(id);
    6. if(!w) {
    7. w = createWindow(id);
    8. cache.insert(id, w);
    9. }
    10. w->show();
    11. }
    12.  
    13. QWidget* X::createWindow(int id) {
    14. switch(id) {
    15. case StartWindow: return new StartWindow;
    16. case ConfigWindow:: return new ConfigWindow;
    17. // ...
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Then it's just a matter of calling openWindow() with a proper id based on the set of windows the application provides. The cache will take care of deleting least recently used windows. You can also instruct the cache that different windows may use a different amount of memory by providing the third argument to QCache::insert().
    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.


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

    Mah.Adly (26th January 2011)

Similar Threads

  1. QImage::scaled takes long time
    By nrabara in forum Qt Programming
    Replies: 0
    Last Post: 15th December 2009, 12:19
  2. need some clarification on QImage and QRgb usage
    By kona in forum Qt Programming
    Replies: 5
    Last Post: 6th September 2008, 14:20
  3. CPU Time & Memory Usage in QThread or QProcess
    By Davidaino in forum Qt Programming
    Replies: 0
    Last Post: 11th July 2008, 19:15
  4. about qt/embedded widgets showing in ARM platform
    By xianshuiren in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 3rd December 2007, 05:48

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.