Results 1 to 14 of 14

Thread: Custom widget with frame like QLineEdit

  1. #1
    Join Date
    May 2009
    Posts
    9
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Custom widget with frame like QLineEdit

    Hi All.

    I would like to build a custom widget that appears with the same frame and background of QLineEdit, whatever they are. The inner content of the widget will be drawn by my paintEvent method.

    Till now I was able to draw the same frame, inheriting my widget from QFrame (instead of QWidget) and setting the frame style to StyledPanel | Sunken. This isn't enough, because I have no background (QLineEdit has a background, usually white).

    There's a way to tell Qt "draw this widget like QLineEdit"?

    I'm doing this through PyQt4 (if matters...).

    Thank you.

  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: Custom widget with frame like QLineEdit

    If it is just about background then you need to tell the frame it should draw one (QWidget::autoFillBackground) and what colour it should use for it (QWidget::setBackgroundRole(QPalette::Base)).

  3. #3
    Join Date
    May 2009
    Posts
    9
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget with frame like QLineEdit

    I tried this (python code):
    Qt Code:
    1. palette = self.palette()
    2. palette.setColor(QPalette.Window, Qt.white)
    3. self.setPalette(palette)
    4. self.setAutoFillBackground(True)
    To copy to clipboard, switch view to plain text mode 
    and this, like you suggested:
    Qt Code:
    1. self.setBackgroundRole(QPalette.Base)
    2. self.setAutoFillBackground(True)
    To copy to clipboard, switch view to plain text mode 
    both draw a white filled rectangle under the widget's frame, but this rectangle is slightly bigger than the frame and this is awful (at least with my KDE4/Oxygen widget style, which has rounded corners for all widgets; the white background of QLineEdit is, obviously, well contained inside the frame).

    thank you for your help.

  4. #4
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom widget with frame like QLineEdit


  5. The following user says thank you to borisbn for this useful post:

    cil (15th April 2010)

  6. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget with frame like QLineEdit

    Quote Originally Posted by cil View Post
    There's a way to tell Qt "draw this widget like QLineEdit"?
    Yes, you can derive your class from QLineEdit.
    On the other hand you can draw whole controls with QStyle or QStylePainter. Read about those classes in Assistant.

  7. #6
    Join Date
    May 2009
    Posts
    9
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget with frame like QLineEdit

    Deriving from QLineEdit seems to me a little forced, because I don't want to modify/extend the QLineEdit behaviour but I want only its appearance. Anyway, If there are no technical reasons to don't derive from QLineEdit for a totally different scope, this could be the simpliest solution.

    QStyle seems promising! But not so easy to understand... I'll look for some examples. Thank you.

  8. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget with frame like QLineEdit

    What if you set the stylesheet with a thick border ?
    something like myLineEdit->setStyleSheet("border : 2px solid black;");

    Check the stylesheet example in Qt demos...

  9. #8
    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: Custom widget with frame like QLineEdit

    Go for QStyle as faldżip suggested, it's easy, really.

  10. #9
    Join Date
    May 2009
    Posts
    9
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget with frame like QLineEdit

    I think this is the right direction... and now I understand the borisbn suggestion.

    in my paintEvent method (python code):
    Qt Code:
    1. def paintEvent(self, event):
    2.  
    3. painter = QPainter(self)
    4.  
    5. option = QStyleOption()
    6. option.initFrom(self)
    7. self.style().drawPrimitive(QStyle.PE_PanelLineEdit, option, painter)
    8. ... other drawing operations...
    To copy to clipboard, switch view to plain text mode 

    but this has no effect. I get only my drawing operations.
    I have some troubles to understand what to do with the QStyleOption object. Maybe the problem is this.

  11. #10
    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: Custom widget with frame like QLineEdit

    I think you should be using QStyleOptionFrame(V3). It's best to look into QLineEdit source code and verify that instead of guessing.

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

    cil (15th April 2010)

  13. #11
    Join Date
    May 2009
    Posts
    9
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget with frame like QLineEdit

    I read qlineedit.cpp, as you wisely suggest. Doing this gave me the inspiration
    for this solution (python code):

    Qt Code:
    1. class MyWidget(QWidget):
    2.  
    3. def __init__(self, parent=None):
    4.  
    5. super(MyWidget, self).__init__(parent)
    6.  
    7. self.styleoption = QStyleOptionFrameV2()
    8. qle = QLineEdit()
    9. qle.initStyleOption(self.styleoption)
    10.  
    11.  
    12. def paintEvent(self, event):
    13.  
    14. painter = QPainter(self)
    15. self.styleoption.rect = self.contentsRect()
    16. self.style().drawPrimitive(QStyle.PE_PanelLineEdit, self.styleoption, painter)
    To copy to clipboard, switch view to plain text mode 


    In other words, I use a QLineEdit to fill in the QStyleOption object for me.
    Then I can use drawPrimitive (borisbn's hint) to have my widget drawn as
    I like.

    Thank you for you patience, I learned some useful things.

  14. #12
    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: Custom widget with frame like QLineEdit

    Hmm... Not very efficient to create a line edit just to fill in a few properties.

  15. #13
    Join Date
    May 2009
    Posts
    9
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget with frame like QLineEdit

    It's true, but there aren't any visibile performance problems and my code is really simple and compact. I can always explode initStyleOption() whenever it becomes necessary.

  16. #14
    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: Custom widget with frame like QLineEdit

    Yeah, but it's still ugly

Similar Threads

  1. Resizing a maximized, custom-frame window
    By Claymore in forum Qt Programming
    Replies: 9
    Last Post: 24th November 2010, 18:17
  2. How to draw a widget directly on the frame buffer
    By kapoorsudhish in forum Qt Programming
    Replies: 8
    Last Post: 12th November 2009, 06:59
  3. Replies: 2
    Last Post: 21st June 2009, 06:04
  4. Show dock widget frame/border
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 21st May 2009, 10:04
  5. QLineEdit - red frame
    By slava in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2008, 12:44

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.