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
{
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
thanx in advance
Bookmarks