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 ...
Code:
#include <QApplication>
#include <QtGui>
#ifndef LINEEDIT_H
#define LINEEDIT_H
{
Q_OBJECT
public:
private:
private slots:
void updateClearWidget(const QString& text);
protected:
};
#endif
#ifndef CLEARWIDGET_H
#define CLEARWIDGET_H
{
Q_OBJECT
public:
};
#endif
/* Implementation of LineEdit */
LineEdit
::LineEdit(QWidget *parent
){
//clearWidget = new ClearWidget(this); // comment out this line and uncomment then next line and things won't work anymore
clearWidget->setStyleSheet("QWidget { background-color:gray; }");
clearWidget->hide();
connect(this, SIGNAL(textChanged(const QString&)), SLOT(updateClearWidget(const QString&)));
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());
}
{
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)
{
LineEdit * le = new LineEdit;
le->show();
return app.exec();
}
thanx in advance
Re: LineEdit with clearbutton ...
Probably because its contents causes the stylesheet to be ignored (if that is what you mean).
Re: LineEdit with clearbutton ...
Quote:
Originally Posted by
wysota
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 :(.
Re: LineEdit with clearbutton ...
http://trolltech.com/developer/task-...ntry&id=166742
Quote:
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:
Code:
{
opt.init(this);
style
()->drawPrimitive
(QStyle::PE_Widget,
&opt,
&p,
this);
}