Results 1 to 4 of 4

Thread: LineEdit with clearbutton ...

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default LineEdit with clearbutton ...

    can anybody tell me why the example below stops working when I use a QWidget derived object instead of a QWidget object in line 37/38? Using an objekt derived from QToolButton works fine though ...
    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. #ifndef LINEEDIT_H
    5. #define LINEEDIT_H
    6.  
    7. class LineEdit : public QLineEdit
    8. {
    9. Q_OBJECT
    10. public:
    11. LineEdit(QWidget *parent=0);
    12. private:
    13. QWidget *clearWidget;
    14. private slots:
    15. void updateClearWidget(const QString& text);
    16. protected:
    17. void resizeEvent(QResizeEvent *);
    18. };
    19. #endif
    20.  
    21. #ifndef CLEARWIDGET_H
    22. #define CLEARWIDGET_H
    23.  
    24. class ClearWidget : public QWidget
    25. {
    26. Q_OBJECT
    27. public:
    28. ClearWidget(QWidget *parent=0) : QWidget(parent) {}
    29. };
    30. #endif
    31.  
    32. /* Implementation of LineEdit */
    33.  
    34. LineEdit::LineEdit(QWidget *parent)
    35. : QLineEdit(parent)
    36. {
    37. //clearWidget = new ClearWidget(this); // comment out this line and uncomment then next line and things won't work anymore
    38. clearWidget = new QWidget(this);
    39. clearWidget->setStyleSheet("QWidget { background-color:gray; }");
    40.  
    41. clearWidget->hide();
    42. connect(this, SIGNAL(textChanged(const QString&)), SLOT(updateClearWidget(const QString&)));
    43.  
    44. QSize sz = sizeHint();
    45. int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
    46. this->setStyleSheet(QString("QLineEdit { padding-right:%1px; }").arg(sz.height() + frameWidth));
    47. }
    48.  
    49. void LineEdit::updateClearWidget(const QString& text)
    50. {
    51. clearWidget->setVisible(!text.isEmpty());
    52. }
    53.  
    54. void LineEdit::resizeEvent(QResizeEvent *)
    55. {
    56. QSize sz = sizeHint();
    57. int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
    58. clearWidget->resize(sz.height(), sz.height());
    59. clearWidget->move(rect().right() - clearWidget->width() - frameWidth,
    60. (rect().bottom() + 1 - clearWidget->height())/2);
    61. }
    62.  
    63. /* Main */
    64.  
    65. #include "main.moc"
    66. int main(int argc, char **argv)
    67. {
    68. QApplication app(argc, argv);
    69. LineEdit * le = new LineEdit;
    70. le->show();
    71. return app.exec();
    72. }
    To copy to clipboard, switch view to plain text mode 
    thanx in advance

  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: LineEdit with clearbutton ...

    Probably because its contents causes the stylesheet to be ignored (if that is what you mean).

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: LineEdit with clearbutton ...

    Quote Originally Posted by wysota View Post
    Probably because its contents causes the stylesheet to be ignored (if that is what you mean).
    Well, it has no content and worse than that, the widget itself is not shown at all. Actually the ClearWidget does not differ from an ordinary QWidget so why isn't it displayed? I even tried to fill it with some content but even then you see a pixel or two but not a proper widget. Trying to resize the widget or to set a fixed size didn't help either .

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: LineEdit with clearbutton ...

    http://trolltech.com/developer/task-...ntry&id=166742
    Resolution: QWidget, QDialog are special widgets in the sense that they do not have a paintEvent. In Qt, we paint through the backing store. For custom widgets that directly inherit from QWidget and QDialog, one needs to provide a paintEvent as below:

    Qt Code:
    1. void paintEvent(QPaintEvent *)
    2. {
    3. opt.init(this);
    4. QPainter p(this);
    5. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 13th September 2007 at 18:59. Reason: reformatted to look better
    J-P Nurmi

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

    momesana (13th September 2007)

Similar Threads

  1. Strings from LineEdit to Integers
    By Misko in forum Newbie
    Replies: 3
    Last Post: 12th August 2007, 11:11
  2. Slots in LineEdit
    By Misko in forum Newbie
    Replies: 3
    Last Post: 28th July 2007, 13:36
  3. Get Color from LineEdit
    By raphaelf in forum Newbie
    Replies: 4
    Last Post: 15th May 2007, 15:30
  4. cursor in LineEdit
    By Jerry in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2006, 21:56
  5. subclassing or redrawing a lineEdit
    By jayw710 in forum Qt Programming
    Replies: 2
    Last Post: 7th February 2006, 18:26

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.