PDA

View Full Version : LineEdit with clearbutton ...



momesana
13th September 2007, 18:44
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 ...


#include <QApplication>
#include <QtGui>

#ifndef LINEEDIT_H
#define LINEEDIT_H

class LineEdit : public QLineEdit
{
Q_OBJECT
public:
LineEdit(QWidget *parent=0);
private:
QWidget *clearWidget;
private slots:
void updateClearWidget(const QString& text);
protected:
void resizeEvent(QResizeEvent *);
};
#endif

#ifndef CLEARWIDGET_H
#define CLEARWIDGET_H

class ClearWidget : public QWidget
{
Q_OBJECT
public:
ClearWidget(QWidget *parent=0) : QWidget(parent) {}
};
#endif

/* Implementation of LineEdit */

LineEdit::LineEdit(QWidget *parent)
: QLineEdit(parent)
{
//clearWidget = new ClearWidget(this); // comment out this line and uncomment then next line and things won't work anymore
clearWidget = new QWidget(this);
clearWidget->setStyleSheet("QWidget { background-color:gray; }");

clearWidget->hide();
connect(this, SIGNAL(textChanged(const QString&)), SLOT(updateClearWidget(const QString&)));

QSize sz = sizeHint();
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
this->setStyleSheet(QString("QLineEdit { padding-right:%1px; }").arg(sz.height() + frameWidth));
}

void LineEdit::updateClearWidget(const QString& text)
{
clearWidget->setVisible(!text.isEmpty());
}

void LineEdit::resizeEvent(QResizeEvent *)
{
QSize sz = sizeHint();
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
clearWidget->resize(sz.height(), sz.height());
clearWidget->move(rect().right() - clearWidget->width() - frameWidth,
(rect().bottom() + 1 - clearWidget->height())/2);
}

/* Main */

#include "main.moc"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
LineEdit * le = new LineEdit;
le->show();
return app.exec();
}
thanx in advance

wysota
13th September 2007, 18:48
Probably because its contents causes the stylesheet to be ignored (if that is what you mean).

momesana
13th September 2007, 18:55
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 :(.

jpn
13th September 2007, 18:58
http://trolltech.com/developer/task-tracker/index_html?method=entry&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:


void paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}