×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Creating a custom custom line editor from QLineEdit

From Nokia Developer Wiki
Jump to: navigation, search
skumar_rao (Talk | contribs)
jimgilmour1 (Talk | contribs)
m (minor spelling)
Line 6: Line 6:
 
|category=Qt
 
|category=Qt
 
|subcategory= Qt GUI
 
|subcategory= Qt GUI
|creationdate=21 December 2010
+
|creationdate=21 November 2010
 
|keywords=QLineEdit
 
|keywords=QLineEdit
 
}}
 
}}
Line 12: Line 12:
 
This example shows how to derive our own Line Widget from QLineEdit.
 
This example shows how to derive our own Line Widget from QLineEdit.
  
=Pre Requirements=
+
=Prerequisites=
  
 
* Qt 4.6.2 or above (tested ok with 4.7 too) Qt Nokia SDK with latest Update will do just fine
 
* Qt 4.6.2 or above (tested ok with 4.7 too) Qt Nokia SDK with latest Update will do just fine
 
* A Qt & Data enabled device [Optional] we will use Qt Nokia SDK's Simulator.
 
* A Qt & Data enabled device [Optional] we will use Qt Nokia SDK's Simulator.
  
we will derive a class named QCustomEditLine
+
We will derive a class named QCustomEditLine
  
 
<code cpp>
 
<code cpp>
Line 24: Line 24:
 
QCustomEditLine::QCustomEditLine(QWidget *parent) : QLineEdit(parent)</code>
 
QCustomEditLine::QCustomEditLine(QWidget *parent) : QLineEdit(parent)</code>
  
add a fuction to take the default text once we get it we will refresh the control to re-draw.
+
add a function to take the default text once we get it we will refresh the control to re-draw.
  
 
     <code cpp>
 
     <code cpp>

Revision as of 12:17, 22 November 2010

Article Metadata
Tested with
Devices(s): Emulator
Compatibility
Platform(s): S60 5th Edition
Symbian
Article
Keywords: QLineEdit
Created: (21 Nov 2010)
Last edited: jimgilmour1 (22 Nov 2010)

This example shows how to derive our own Line Widget from QLineEdit.

Prerequisites

  • Qt 4.6.2 or above (tested ok with 4.7 too) Qt Nokia SDK with latest Update will do just fine
  • A Qt & Data enabled device [Optional] we will use Qt Nokia SDK's Simulator.

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 for example

    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 );
}


--skumar_rao 09:27, 21 November 2010 (UTC)

CustomLineEdit.JPG
CustomLineEdit2.JPG
122 page views in the last 30 days.
×