Results 1 to 10 of 10

Thread: Background image of widget in QWidgetStack

  1. #1
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Background image of widget in QWidgetStack

    Hi,

    I have a QStackedWidget with two widgets in it. Now, I want to apply a background image to only the first widget without changing anything in the second widget. How do I do it?

    Anyways here's the code:

    Qt Code:
    1. qs = new QStackedWidget(this);
    2. qs->resize(880,558);
    3. qs->addWidget(men);
    4. qs->addWidget(qur);
    To copy to clipboard, switch view to plain text mode 

    *I tried the following but didn't work/got unexpected results.*

    1) men->setStyleSheet("QWidget {background-image: url(:/file/back_menu.png)}");

    In this the main background of 'men' did not change. Only the background of men's children widget like QPushButton changed.

    2) qs->setStyleSheet("QStackWidget {background-image: url(:/file/back_menu.png)}");

    This changed the background of both the first and second widget

    3) qs->setStyleSheet("QWidget#men {background-image: url(:/file/back_menu.png)}");

    Nothing changes.
    Last edited by el33t; 14th March 2011 at 09:49.

  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: Background image of widget in QWidgetStack

    How is "men" implemented?
    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
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Background image of widget in QWidgetStack

    Ok, edited the question. Bolded the edited part.

    @wysota : Mmmm, I'm not sure I understand what you are asking, but here is the complete code. I hope this helps.

    mainwindow.cpp
    Qt Code:
    1. mainwindow::mainwindow()
    2. {
    3.  
    4. #include <QtGui>
    5. #include <iostream>
    6. #include "mainwindow.h"
    7. #include "game.h"
    8. #include "menu1.h"
    9.  
    10. men = new menu1(this);
    11. qur = new game(this);
    12.  
    13. qs = new QStackedWidget(this);
    14. qs->resize(880,558);
    15. qs->addWidget(men);
    16. qs->addWidget(qur);
    17.  
    18. qs->setCurrentIndex(0);
    19.  
    20. show();
    21. }
    To copy to clipboard, switch view to plain text mode 


    menu1.cpp

    Qt Code:
    1. #include "menu1.h"
    2. #include <QtGui>
    3.  
    4. menu1::menu1(QWidget *parent) :
    5. QWidget(parent)
    6. {
    7.  
    8. resize(880,558);
    9.  
    10. }
    11.  
    12. void menu1::paintEvent(QPaintEvent *)
    13. {
    14. QPainter painter(this);
    15. painter.setRenderHint(QPainter::Antialiasing, true);
    16. path.moveTo(qrand() % 80, qrand() % 320);
    17. path.cubicTo(200, 200, 320, 80, 480, 320);
    18. painter.setPen(QPen(Qt::green, 8));
    19. painter.drawPath(path);
    20. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: Background image of widget in QWidgetStack

    You're doing painting yourself so stylesheets won't work. You either have to reenable stylesheets by adding some code to the paint event or set the image as the background role in the palette and enable autoFillBackground for your widget.
    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. The following user says thank you to wysota for this useful post:

    el33t (14th March 2011)

  6. #5
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Background image of widget in QWidgetStack

    Ok, thanks. I'll try out your solutions and report back as soon as possible.


    Added after 41 minutes:


    Quote Originally Posted by wysota View Post
    You're doing painting yourself so stylesheets won't work.
    Hey, I removed all the paintevent code from menu1.cpp and menu1.h . Still the stylesheet doesn't work. I mean, only the child widgets of 'men' like pushbuttons have their backgrounds changed and not men itself.

    Quote Originally Posted by wysota View Post
    You either have to reenable stylesheets by adding some code to the paint event or set the image as the background role in the palette and enable autoFillBackground for your widget.
    Can you please show me some code for the above two solutions? I'm very new to QT, so please pardon my lack of knowledge.


    By the way, can you suggest some solutions through whcih I can change the background through stylesheet and not through QPalette.

    Regards.
    Last edited by el33t; 14th March 2011 at 11:05.

  7. #6
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Background image of widget in QWidgetStack

    Bump... Please help me guys...

  8. #7
    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: Background image of widget in QWidgetStack

    Quote Originally Posted by el33t View Post
    Hey, I removed all the paintevent code from menu1.cpp and menu1.h .
    Did you comment out the paintEvent() method?

    Can you please show me some code for the above two solutions? I'm very new to QT, so please pardon my lack of knowledge.
    This is no excuse. Search the forum, the answers are there.
    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. #8
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Background image of widget in QWidgetStack

    Quote Originally Posted by wysota View Post
    1) Did you comment out the paintEvent() method?


    2) This is no excuse. Search the forum, the answers are there.
    1) Yes. Why? that won't work?

    2) Sorry. I solved the problem using the QPalllete method but I wanted to use StyleSheet rather than QPallette.

  10. #9
    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: Background image of widget in QWidgetStack

    Quote Originally Posted by el33t View Post
    1) Yes. Why? that won't work?
    If your widget class has the Q_OBJECT macro then it won't work. I have recently "solved" this "problem" twice on this forum so the answer is there, just look for it.
    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. The following user says thank you to wysota for this useful post:

    el33t (16th March 2011)

  12. #10
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Background image of widget in QWidgetStack

    Quote Originally Posted by wysota View Post
    If your widget class has the Q_OBJECT macro then it won't work. I have recently "solved" this "problem" twice on this forum so the answer is there, just look for it.
    Ok, thanks for the assistance.

Similar Threads

  1. Background image on a combined widget list
    By buffer in forum Qt Programming
    Replies: 2
    Last Post: 18th November 2010, 00:04
  2. Setting background image for Central Widget
    By xfurrier in forum Qt Programming
    Replies: 8
    Last Post: 25th February 2010, 10:14
  3. how to change BackGround image on a stacked widget
    By mcrahr in forum Qt Programming
    Replies: 3
    Last Post: 10th February 2010, 08:38
  4. Background image for main window widget using css.
    By Enygma in forum Qt Programming
    Replies: 8
    Last Post: 23rd August 2007, 15:40
  5. Background image and semitransparent widget
    By asieriko in forum Qt Programming
    Replies: 3
    Last Post: 20th August 2007, 16:43

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.