Archived:Creating a custom custom line editor from QLineEdit
hamishwillee
(Talk | contribs) m (Hamishwillee - Bot update - Fix ArticleMetaData and other issues) |
hamishwillee
(Talk | contribs) m (Hamishwillee - Add Abstract. Tidy wiki text.) |
||
| Line 1: | Line 1: | ||
| − | {{Archived|timestamp=20120302032925|user=[[User:Hamishwillee|<br />----]]|[[:Category:Qt Quick|Qt Quick]] should be used for all UI development on mobile devices. The approach described in this article (based on | + | [[Category:MeeGo]][[Category:Symbian]][[Category:Qt C++ UI]] |
| − | + | {{Abstract|This example shows how to derive our own Line Widget from {{Qapiname|QLineEdit}}.}} {{Qapiname|QLineEdit}} is a one-line text editor. A line edit allows the user to enter and edit a single line of plain text with a useful collection of editing functions, including undo and redo, cut and paste, and drag and drop. | |
| + | {{Archived|timestamp=20120302032925|user=[[User:Hamishwillee|<br />----]]|[[:Category:Qt Quick|Qt Quick]] should be used for all UI development on mobile devices. The approach described in this article (based on C++ for the Qt UI) deprecated.}} | ||
{{ArticleMetaData <!-- v1.2 --> | {{ArticleMetaData <!-- v1.2 --> | ||
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) --> | |sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) --> | ||
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | |installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | ||
|devices= Emulator | |devices= Emulator | ||
| − | |sdk= | + | |sdk= Qt 4.6.2, Qt 4.7 |
|platform= S60 5th Edition | |platform= S60 5th Edition | ||
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) --> | |devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) --> | ||
| Line 22: | Line 23: | ||
|creationdate= 20101121 | |creationdate= 20101121 | ||
|author= [[User:Skumar rao]] | |author= [[User:Skumar rao]] | ||
| − | |||
| − | |||
}} | }} | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
==Implementation== | ==Implementation== | ||
| Line 43: | Line 34: | ||
QCustomEditLine::QCustomEditLine(QWidget *parent) : QLineEdit(parent)</code> | QCustomEditLine::QCustomEditLine(QWidget *parent) : QLineEdit(parent)</code> | ||
| − | + | Add a function to take the default text once we get it we will refresh the control to re-draw. | |
| − | + | <code cpp> | |
| − | + | emptyMessage = msg; | |
| − | + | drawEmptyMsg = text().isEmpty(); | |
| − | + | update(); | |
| − | + | </code> | |
| − | + | Derive {{Icode|paintEvent( QPaintEvent *ev )}} to draw the text that we want to show when there is no text is entered. | |
| − | + | <code cpp> | |
| − | + | QPainter p(this); | |
| − | + | QFont f = font(); | |
| − | + | f.setItalic(true); | |
| − | + | p.setFont(f); | |
| − | + | QColor color(palette().color(foregroundRole())); | |
| − | + | color.setAlphaF(0.5); | |
| − | + | p.setPen(color); | |
| − | + | QStyleOptionFrame opt; | |
| − | + | initStyleOption(&opt); | |
| − | + | QRect cr = style()->subElementRect(QStyle::SE_LineEditContents, &opt, this); | |
| − | + | cr.setLeft(cr.left() + 2); | |
| − | + | cr.setRight(cr.right() - 2); | |
| − | + | p.drawText(cr, Qt::AlignLeft|Qt::AlignVCenter, emptyMessage);</code> | |
control the drawing to show the default text when control is focused out and blank it when cointrol focused in. | control the drawing to show the default text when control is focused out and blank it when cointrol focused in. | ||
| − | + | Now we are ready to add to our widget. To use do the following: | |
| − | + | <code cpp> | |
| − | + | m_lineout = new QCustomEditLine(this); | |
| − | + | m_lineout->setEmptyMessage("Enter you Email Address"); | |
| + | </code> | ||
| Line 179: | Line 171: | ||
==Reference== | ==Reference== | ||
| − | * | + | * {{Qapiname|QLineEdit}} |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
Revision as of 07:45, 2 March 2012
This example shows how to derive our own Line Widget from QLineEdit. QLineEdit is a one-line text editor. A line edit allows the user to enter and edit a single line of plain text with a useful collection of editing functions, including undo and redo, cut and paste, and drag and drop.
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on C++ for the Qt UI) deprecated.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on C++ for the Qt UI) deprecated.
Article Metadata
Tested with
SDK: Qt 4.6.2, Qt 4.7
Devices(s): Emulator
Compatibility
Platform(s): S60 5th Edition
Article
Keywords: QLineEdit
Created: skumar_rao
(21 Nov 2010)
Last edited: hamishwillee
(02 Mar 2012)
Contents |
Implementation
We will derive a class named QCustomEditLine
class QCustomEditLine : public QLineEdit
QCustomEditLine::QCustomEditLine(QWidget *parent) : QLineEdit(parent)
Add a function to take the default text once we get it we will refresh the control to re-draw.
emptyMessage = msg;
drawEmptyMsg = text().isEmpty();
update();
Derive paintEvent( QPaintEvent *ev ) to draw the text that we want to show when there is no text is entered.
QPainter p(this);
QFont f = font();
f.setItalic(true);
p.setFont(f);
QColor color(palette().color(foregroundRole()));
color.setAlphaF(0.5);
p.setPen(color);
QStyleOptionFrame opt;
initStyleOption(&opt);
QRect cr = style()->subElementRect(QStyle::SE_LineEditContents, &opt, this);
cr.setLeft(cr.left() + 2);
cr.setRight(cr.right() - 2);
p.drawText(cr, Qt::AlignLeft|Qt::AlignVCenter, emptyMessage);
control the drawing to show the default text when control is focused out and blank it when cointrol focused in.
Now we are ready to add to our widget. To use do the following:
m_lineout = new QCustomEditLine(this);
m_lineout->setEmptyMessage("Enter you Email Address");
qcustomeditline.h
#ifndef QCUSTOMEDITLINE_H
#define QCUSTOMEDITLINE_H
#include <QtGui>
class QCustomEditLine : public QLineEdit
{
Q_OBJECT
public:
QCustomEditLine(QWidget *parent = 0);
virtual ~QCustomEditLine();
public:
void setEmptyMessage( const QString &msg );
protected:
void paintEvent( QPaintEvent *ev );
void focusInEvent( QFocusEvent *ev );
void focusOutEvent( QFocusEvent *ev );
private:
QString emptyMessage;
bool drawEmptyMsg;
};
#endif // QCUSTOMEDITLINE_H
qcustomeditline.cpp
#include "qcustomeditline.h"
QCustomEditLine::QCustomEditLine(QWidget *parent)
: QLineEdit( parent ){
}
QCustomEditLine::~QCustomEditLine() {
}
void QCustomEditLine::setEmptyMessage( const QString &msg ) {
emptyMessage = msg;
drawEmptyMsg = text().isEmpty();
update();
}
void QCustomEditLine::paintEvent( QPaintEvent *ev ) {
QLineEdit::paintEvent( ev );
if ( text().isEmpty() ) {
QPainter p(this);
QFont f = font();
f.setItalic(true);
p.setFont(f);
QColor color(palette().color(foregroundRole()));
color.setAlphaF(0.5);
p.setPen(color);
QStyleOptionFrame opt;
initStyleOption(&opt);
QRect cr = style()->subElementRect(QStyle::SE_LineEditContents, &opt, this);
cr.setLeft(cr.left() + 2);
cr.setRight(cr.right() - 2);
p.drawText(cr, Qt::AlignLeft|Qt::AlignVCenter, emptyMessage);
}
}
void QCustomEditLine::focusInEvent( QFocusEvent *ev ) {
if ( drawEmptyMsg ) {
drawEmptyMsg = false;
update();
}
QLineEdit::focusInEvent( ev );
}
void QCustomEditLine::focusOutEvent( QFocusEvent *ev ) {
if ( text().isEmpty() ) {
drawEmptyMsg = true;
update();
}
QLineEdit::focusOutEvent( ev );
}

