Вопрос по lineEdit: как узнать когда он в фокусе а когда нет? Пытался написать свое класс на основе QLineEdit и из focusEventов емитеть сигнал, нечего не получилось!:( Может есть более корректное решение? В итоге мне надо сделать следующее: если LineEdit пуст там появляется текст, при клике(табуляции) туда текст исчезает ))
#include <QLineEdit>
#include <QFocusEvent>
class authEdit : public QLineEdit
{
Q_OBJECT
public:
authEdit(QWidget *parent = 0);
signals:
void onEnterEvent();
void onLeaveEvent();
protected:
bool enterFlag;
void focusInEvent (QFocusEvent *event );
void focusOutEvent ( QFocusEvent *event);
};
#include "authedit.h"
authEdit::authEdit(QWidget *parent)
: QLineEdit (parent)
{
enterFlag=false;
}
void authEdit::focusInEvent (QFocusEvent *event )
{
if (!enterFlag) {
emit onEnterEvent();
enterFlag=true;
}
}
void authEdit::focusOutEvent ( QFocusEvent *event)
{
if (enterFlag) {
emit onLeaveEvent();
enterFlag=false;
}
}
Я думаю лучше это делать в paintEvent, примерно так:
void CMyClass::paintEvent(QPaintEvent *)
{
if( !pEdit->hasFocus() && pEdit->text().isEmpty())
{
QPainter painter( pEdit);
painter.setPen(Qt::gray);
painter.setFont(QFont("Arial", 12));
painter.drawText(pEdit->rect(), Qt::AlignCenter, "Введите текст");
}
}
focusInEvent, focusOutEvent. Оно?
entmobile, в обработчиках событий можно просто устанавливать/удалять текст:
void authEdit::focusInEvent (QFocusEvent *event )
{
setText("");
}
void authEdit::focusOutEvent ( QFocusEvent *event)
{
setText("Type here");
}
hasFocus не пробовал?
а почему focusInEvent, focusOutEvent не устроил?
//painter.setFont(QFont("Arial", 12));
painter.setFont(this->font());
дабы не было жестко зашитого в код размера шрифта
kuler, он так и сделал
//painter.setFont(QFont("Arial", 12));
painter.setFont(this->font());
дабы не было жестко зашитого в код размера шрифтаможет быть, я с рисованием еще не работал. Так на удачу воткнул
всё это уже есть в http://www.prog.org.ru/topic_7795_0.html - в доп.компонентах на prog.org.ru
скоро будет свежая версия...
#ifndef QWEBSTYLEEDIT_H
#define QWEBSTYLEEDIT_H
#include <QLineEdit>
#include <QColor>
class QWebStyleEdit : public QLineEdit
{
Q_OBJECT
Q_PROPERTY(QString grayedText READ grayedText WRITE setGrayedText)
QString m_grText;
QColor base,alternate;
QColor text,brtext;
bool modified;
void setBackgroundColor(QColor);
void setForegroundColor(QColor);
void setItalic(bool);
public:
QWebStyleEdit(QWidget * parent = 0, QString grText = QString());
void setGrayedText(const QString &);
QString grayedText() const;
void setText(const QString &);
protected:
void focusInEvent(QFocusEvent *);
void focusOutEvent(QFocusEvent *e);
};
#endif // QWEBSTYLEEDIT_H
#include "qwebstyleedit.h"
#include <QPalette>
QWebStyleEdit::QWebStyleEdit(QWidget * parent,QString grText):QLineEdit(parent)
{
setGrayedText(grText);
base = palette().color(QPalette::Base);
alternate = palette().color(QPalette::AlternateBase);
text = palette().color(QPalette::Text);
brtext = palette().color(QPalette::Midlight);
setBackgroundColor(alternate);
setForegroundColor(brtext);
setItalic(true);
}
void QWebStyleEdit::setGrayedText(const QString &grText)
{
m_grText=grText;
if(QLineEdit::text().isEmpty())
QLineEdit::setText(m_grText);
}
QString QWebStyleEdit::grayedText() const
{
return m_grText;
}
void QWebStyleEdit::focusInEvent(QFocusEvent *e)
{
QLineEdit::focusInEvent(e);
setBackgroundColor(base);
setForegroundColor(text);
if(!modified)
QLineEdit::setText("");
setItalic(false);
}
void QWebStyleEdit::focusOutEvent(QFocusEvent *e)
{
QLineEdit::focusOutEvent(e);
setBackgroundColor(alternate);
setForegroundColor(brtext);
if(QLineEdit::text().isEmpty()){
QLineEdit::setText(m_grText);
modified = false;
setItalic(true);
} else {
modified = true;
}
}
void QWebStyleEdit::setBackgroundColor(QColor color)
{
QPalette p = palette();
p.setColor(QPalette::Base, color);
setPalette(p);
}
void QWebStyleEdit::setForegroundColor(QColor color)
{
QPalette p = palette();
p.setColor(QPalette::Text, color);
setPalette(p);
}
void QWebStyleEdit::setText(const QString &text)
{
QLineEdit::setText(text);
if(text.isEmpty())
modified = false;
else
modified = true;
}
void QWebStyleEdit::setItalic(bool it)
{
QFont fnt(font());
fnt.setItalic(it);
setFont(fnt);
}
а блин, куча косяков
#ifndef QWEBSTYLEEDIT_H
#define QWEBSTYLEEDIT_H
#include <QLineEdit>
#include <QColor>
class QWebStyleEdit : public QLineEdit
{
Q_OBJECT
Q_PROPERTY(QString grayedText READ grayedText WRITE setGrayedText)
QString m_grText;
QColor base,alternate;
QColor fgtext,bgtext;
bool modified;
void setBackgroundColor(QColor);
void setForegroundColor(QColor);
void setItalic(bool);
public:
QWebStyleEdit(QWidget * parent = 0, QString grText = QString());
void setGrayedText(const QString &);
QString grayedText() const;
void setText(const QString &);
QString text () const;
protected:
void focusInEvent(QFocusEvent *);
void focusOutEvent(QFocusEvent *e);
};
#endif // QWEBSTYLEEDIT_H
#include "qwebstyleedit.h"
#include <QPalette>
QWebStyleEdit::QWebStyleEdit(QWidget * parent,QString grText):QLineEdit(parent)
{
setGrayedText(grText);
base = palette().color(QPalette::Base);
alternate = palette().color(QPalette::AlternateBase);
fgtext = palette().color(QPalette::Text);
bgtext = palette().color(QPalette::Midlight);
setBackgroundColor(alternate);
setForegroundColor(bgtext);
setItalic(true);
modified = false;
}
void QWebStyleEdit::setGrayedText(const QString &grText)
{
m_grText=grText;
if(QLineEdit::text().isEmpty())
QLineEdit::setText(m_grText);
}
QString QWebStyleEdit::grayedText() const
{
return m_grText;
}
void QWebStyleEdit::focusInEvent(QFocusEvent *e)
{
QLineEdit::focusInEvent(e);
setBackgroundColor(base);
setForegroundColor(fgtext);
if(!modified)
QLineEdit::setText("");
setItalic(false);
}
void QWebStyleEdit::focusOutEvent(QFocusEvent *e)
{
QLineEdit::focusOutEvent(e);
setBackgroundColor(alternate);
setForegroundColor(bgtext);
if(QLineEdit::text().isEmpty()){
QLineEdit::setText(m_grText);
modified = false;
setItalic(true);
} else {
modified = true;
}
}
void QWebStyleEdit::setBackgroundColor(QColor color)
{
QPalette p = palette();
p.setColor(QPalette::Base, color);
setPalette(p);
}
void QWebStyleEdit::setForegroundColor(QColor color)
{
QPalette p = palette();
p.setColor(QPalette::Text, color);
setPalette(p);
}
void QWebStyleEdit::setText(const QString &text)
{
QLineEdit::setText(text);
if(text.isEmpty())
modified = false;
else
modified = true;
}
QString QWebStyleEdit::text() const
{
if(modified)
return QLineEdit::text();
else
return QString("");
}
void QWebStyleEdit::setItalic(bool it)
{
QFont fnt(font());
fnt.setItalic(it);
setFont(fnt);
}
А можно просто подождать Qt 4.7 Там у QLineEdit есть property placeholderText. Этот код уже есть в исходных текстах SDK Qt 4.6 windows.
Вот возможно так:
В Qt 4.7 должен появится метод setPlaceholderText. В Qt 4.6 он уже реализован, но не доступен. Видимо тролли нас хотят удивить, ну или еще не все отладили. Метод работает, но достучаться можно только через хак:
#include "private/qlineedit_p.h"
...
namespace Hack { template <class To, class From> inline To* d_ptr(From* ptr){ return (To*)QObjectPrivate::get(ptr);}}
...
Hack::d_ptr<QLineEditPrivate>(ui->lineEdit)->placeholderText = "Test";
чет у меня все криво и косо ((
опять поправил - http://z0d5e8n7x.ru/blog/2010/03/24/qlineedit-v-stile-web/
Форум Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)