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