Results 1 to 2 of 2

Thread: CSS Related Problem

  1. #1
    Join Date
    Jun 2010
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default CSS Related Problem

    So, i have a small login form, and i want to put it lying on a image made by me.
    In order to do this, i created a frame, and customized it with stylesheets so the background-image is my image and put the form inside in a layout.

    the problem is when i resize the window, the image stays always the same size, which blows up my layout.

    How do i make the image stretch and shrink so it always fits the form?

    Thanks

  2. #2
    Join Date
    Apr 2011
    Location
    San Francisco Bay area
    Posts
    5
    Qt products
    Platforms
    MacOS X Windows

    Default Re: CSS Related Problem

    I don't know if you can use CSS (presumably using setStyleSheet()) to cause a background image to resize as needed, but you can certainly do it by writing your own paintEvent() (and resizeEvent() too, for efficiency) in a subclass of QFrame:

    Keep your original pixmap and your scaled_pixmap as members of your new class!

    Qt Code:
    1. void resizeEvent (QResizeEvent *) {
    2. if (scaled_pixmap)
    3. delete scaled_pixmap;
    4. float width_f = float(width()) / float(pixmap->width());
    5. float height_f = float(height()) / float(pixmap->height());
    6. float zoom_f = (width_f < height_f ? width_f : height_f);
    7. scaled_pixmap = pixmap->scaled(zoom_f, zoom_f, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    8. }
    9.  
    10. void paintEvent (QPaintEvent *event) {
    11. if (!scaled_pixmap) {
    12. QResizeEvent event; // ignored anyway, so just fake it
    13. resizeEvent(&event);
    14. }
    15. painter = QPainter(this);
    16. painter.save();
    17. painter.fillRect(event->rect(), QColor("black"));
    18. // the zooming in resizeEvent() ensures that only one of these is non-zero
    19. h_offset = (width() - scaled_pixmap->width()) / 2; // if frame is wider than image
    20. v_offset = (height() - scaled_pixmap->height()) / 2; // if frame is taller
    21. painter.drawPixmap(h_offset, v_offset, scaled_pixmap);
    22. painter.restore();
    23. }
    To copy to clipboard, switch view to plain text mode 

    The shaped clock example is a great reference for other painter capabilities! http://doc.trolltech.com/widgets-sha...clock-cpp.html

    Good luck!

Similar Threads

  1. Coordinates Related
    By jsmith in forum Qt Programming
    Replies: 0
    Last Post: 18th August 2009, 15:37
  2. Multi-thread related problem,help me
    By mendynew in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2008, 03:02
  3. Problem related exiting the thread
    By raghvendramisra in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2008, 10:10
  4. Related To QGraphicsItemGroup
    By ashishsaryar in forum Qt Programming
    Replies: 0
    Last Post: 29th April 2008, 14:55
  5. Problem related to porting from QT3 to QT4 in list items
    By rohitjun in forum Qt Programming
    Replies: 5
    Last Post: 1st June 2007, 09:05

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.