Results 1 to 3 of 3

Thread: stylesheet to QDialog Title Bar

  1. #1
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Post stylesheet to QDialog Title Bar

    Hi
    How to apply stylesheet to QDialog Title Bar?
    Thanks & Regards
    Srinivasa Rao Chigurupati

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: stylesheet to QDialog Title Bar

    Quote Originally Posted by srinirc View Post
    Hi
    How to apply stylesheet to QDialog Title Bar?
    You cant do it, using style sheet. Use QStyle instead.

  3. #3
    Join Date
    Mar 2009
    Posts
    72
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: stylesheet to QDialog Title Bar

    You must create a custom class which inherits by QDialog and then re-implement the "paintEvent" method with something similar to this:

    Qt Code:
    1. MyDialog::MyDialog(QWidget* parent) : QDialog(parent, Qt::FramelessWindowHint) {}
    2.  
    3. // ...
    4.  
    5. void MyDialog::paintEvent(QPaintEvent* event)
    6. {
    7. Q_UNUSED(event)
    8.  
    9. QPainter p(this);
    10. QStyle* style = this->style();
    11. QRect active_area = this->rect();
    12. int titlebar_height = 0;
    13.  
    14. // Titlebar.
    15. t_opt.initFrom(this);
    16.  
    17. titlebar_height = style->pixelMetric(QStyle::PM_TitleBarHeight, &t_opt, this);
    18.  
    19. t_opt.rect = QRect(0, 0, this->width(), titlebar_height);
    20. t_opt.titleBarState = this->windowState();
    21. t_opt.text = t_opt.fontMetrics.elidedText(this->windowTitle(), Qt::ElideRight, t_opt.rect.width());
    22. style->drawComplexControl(QStyle::CC_TitleBar, &t_opt, &p, this);
    23. style->drawItemText(&p, t_opt.rect, Qt::AlignCenter, t_opt.palette, true, t_opt.text, QPalette::ToolTipText);
    24.  
    25. // Background widget.
    26. active_area.setTopLeft(QPoint(0, titlebar_height));
    27. this->setContentsMargins(0, titlebar_height, 0, 0);
    28.  
    29. QStyleOption w_opt;
    30. w_opt.initFrom(this);
    31. w_opt.rect = active_area;
    32. style->drawPrimitive(QStyle::PE_Widget, &w_opt, &p, this);
    33. }
    To copy to clipboard, switch view to plain text mode 

    Now you can style it with a stylesheet:

    Qt Code:
    1. MyDialog::title {
    2. height: 24px;
    3. font-weight: bold;
    4. color: #000000;
    5. background: #ffffff;
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by zuck; 24th November 2009 at 16:37.

Similar Threads

  1. Replies: 7
    Last Post: 12th January 2011, 23:01
  2. Replies: 5
    Last Post: 1st March 2010, 16:55
  3. Replies: 5
    Last Post: 21st April 2008, 08:54
  4. Resizing a QDialog to the content size
    By Nyphel in forum Qt Programming
    Replies: 8
    Last Post: 15th March 2007, 09:16

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.