PDA

View Full Version : How could I make the QLineEdit UpperCase without using InputMask?



uygar
11th February 2009, 09:50
How could I make the QLineEdit UpperCase without using InputMask.
I don't want to use it because when I use it inputMask like ">AAAAAAA" the curser doesn't starts begenning of the line. It start where ever I click in the line.

I need to delegate the lines by using "promote to" with QtDesigner.

Can you please write an example delegate class for QLineEdit

I did it for line in a QTableWidget but not an indepandent QLineEdit
It is Like:


QWidget *AlternatifDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QLocale::setDefault(QLocale(QLocale::Turkish, QLocale::Turkey));
QLineEdit *editor = new QLineEdit(parent);
QFont font;
font.setCapitalization(QFont::AllUppercase);
editor->setFont(font);
return editor;
}
void AlternatifDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QLocale turkish(QLocale::Turkish);
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QLineEdit *qline = static_cast<QLineEdit*>(editor);
QFont font;
font.setCapitalization(QFont::AllUppercase);
editor->setFont(font);
qline->setText(value);
}
void AlternatifDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QLocale turkish(QLocale::Turkish);
QLineEdit *qline = static_cast<QLineEdit*>(editor);
QFont font;
font.setCapitalization(QFont::AllUppercase);
editor->setFont(font);
QString value = qline->text();
model->setData(index, value.toUpper());
}

spirit
11th February 2009, 10:04
a little bit tricky, but works fine :cool:


...
connect(le, SIGNAL(textEdited(const QString &)), SLOT(toUpper(const QString &)));
...
void Test::toUpper(const QString &text)
{
QLineEdit *le = qobject_cast<QLineEdit *>(sender());
if (!le)
return;
le->setText(text.toUpper());
}

caduel
11th February 2009, 10:04
you can:
* set a QValidator that converts the letters to upper case
* intercept key events and convert to key
* connect to textChanged and change the text again (drawbacks when editing not at the end...)

I suggest you set a QValidator

uygar
11th February 2009, 10:43
I found that codes


pqUpcaseValidator::pqUpcaseValidator( QWidget* Parent, const char* Name )
: QValidator( Parent, Name )
{}

QValidator::State pqUpcaseValidator::validate( QString& Text , int& Pos ) const
{
Text = Text.upper();
return Acceptable;
}

What kind of document I should write?
is that currect?


#ifndef MYUPPERLINE_H
#define MYUPPERLINE_H
#include <QtGui>
class MyUpperLine2 : public QLineEdit
{
public:
MyUpperLine2(QWidget* parent = 0):QLineEdit(parent) { }

pqUpcaseValidator::pqUpcaseValidator( QWidget* Parent, const char* Name )
: QValidator( Parent, Name )
{}

QValidator::State pqUpcaseValidator::validate( QString& Text , int& Pos ) const
{
Text = Text.upper();
return Acceptable;
}
};
and is it enough to promote it by designer?

talk2amulya
11th February 2009, 10:53
well, once u have written the validator, u have to call setValidator() for QLineEdit..that should do it. And yes, ur class would be good enough for promoting once u have set the validator for QLineEdit

uygar
11th February 2009, 14:10
a little bit tricky, but works fine :cool:


...
connect(le, SIGNAL(textEdited(const QString &)), SLOT(toUpper(const QString &)));
...
void Test::toUpper(const QString &text)
{
QLineEdit *le = qobject_cast<QLineEdit *>(sender());
if (!le)
return;
le->setText(text.toUpper());
}


I can't delegate but this solition is OK for now. Thank you all.:D

uygar
11th February 2009, 15:42
I got a problem about one of Capital letters.
In Turkish (utf-8, Latin5), alfabet is a litle bit differents from English alfabet.
we have lower case "i" and it's upper case is" İ".
At the same time ;
we have lower case "ı" and upper case "I".

When caps lock off if I hit "ı" or "i" it returns "I" that is wrong the correct is if I hit "i" it must return "İ".:confused:
When caps lock on, it work correct.

I tried: (but nothing chaged)

void formCari::toUpper(const QString &text)
{
QLocale turkish(QLocale::Turkish);
QLineEdit *SSSSSSS = qobject_cast<QLineEdit *>(sender());
if (!SSSSSSS)
return;
SSSSSSS->setText(text.toUpper());
}

P.S. if you can not read the letters that I write, I can explain. the upper case of "i" is "I" with top point as the lower one. And all so, lower case of "I" is like "i" but with out point.

caduel
11th February 2009, 16:12
you don't seem to actually use that QLocale you created...?

uygar
11th February 2009, 16:26
you don't seem to actually use that QLocale you created...?

but how I?

caduel
11th February 2009, 16:39
Probably with QLocale::setDefault()
It would probably be good to set that at startup and not change this (global) setting too often. I am not sure if it will really affect the workings of toUpper. But it is worth a try.

uygar
12th February 2009, 07:23
Actually I did it but not changed