PDA

View Full Version : problem withh returnPressed



:db:sStrong
22nd May 2006, 10:44
Hi volks,
I have a qtable en qtableItems class but i have made one col as QlineEdit. This is the code


QWidget* testTableItem::createEditor() const
{

QLineEdit* le = ( QLineEdit *)(QTableItem::createEditor());
.......
......
}

I have reimplemented createEditor and in here i have made a validator for the qlineEdit.

Now i have a problem with the validator, i dont want to write 1, 2 inside the qline but i should be able to write 11 and 12 and in order to implement this i want to use events like pressReturned signal of qlineedit. I dont have qlineedit and is it possible to use pressRetuned signal in above situation?

thanx in advance

wysota
22nd May 2006, 11:08
Why can't you use regular Qt validating mechanisms (meaning a subclass of QValidator)?

:db:sStrong
22nd May 2006, 11:13
Why can't you use regular Qt validating mechanisms (meaning a subclass of QValidator)?

i use regular expression but it is not doing what i want. I want to type the integers bigger than 2 and smaller than 16000, if i am not able to write 1 and 2 how can i type 11 and 12?

thats why i want to use pressReturned in order to ignore 1 and 2 if type but do accept 11 and 12 and onwards..

wysota
22nd May 2006, 12:49
i use regular expression but it is not doing what i want. I want to type the integers bigger than 2 and smaller than 16000, if i am not able to write 1 and 2 how can i type 11 and 12?
How about using QIntValidator?

Besides, you can do it with regular expressions as well, something like this should work:


QRegExp("([3-9][0-9]{0,3})|(2[0-9]{1,3})|(1(([0-6][0-9]{0,3})|([7-9][0-9]{0-2})))");

:db:sStrong
22nd May 2006, 13:23
How about using QIntValidator?

Besides, you can do it with regular expressions as well, something like this should work:


QRegExp("([3-9][0-9]{0,3})|(2[0-9]{1,3})|(1(([0-6][0-9]{0,3})|([7-9][0-9]{0-2})))");


this reg doesnt work i am not able to type anything :(

munna
22nd May 2006, 13:26
Why dont you use QIntValidator (as suggested by wysota) and set the range ?

:db:sStrong
22nd May 2006, 13:48
Why dont you use QIntValidator (as suggested by wysota) and set the range ?

i am using now QIntValidator:


QIntValidator* validator = new QIntValidator( 3,16000 , le );
validator->setRange(3,16000);
le->setValidator(validator);
le->clear();

but i am still able to type 1 and 2 i want to have this: >2 and <=16000. the top is good but the bottom value is not good in above code.

wysota
22nd May 2006, 13:53
i am using now QIntValidator:


QIntValidator* validator = new QIntValidator( 3,16000 , le );
validator->setRange(3,16000);
le->setValidator(validator);
le->clear();

but i am still able to type 1 and 2 i want to have this: >2 and <=16000. the top is good but the bottom value is not good in above code.

You can type "1" because to type "1000" you have to be able to type "1" :) It's just not being accepted as a valid value, but you have to be able to type it in. I think that if the lineedit loses focus, the value will be corrected to a valid one. But maybe it'd be easier for you to use QSpinBox?

munna
22nd May 2006, 13:56
I think you need to use QSpinBox.

If you are very particular about QLineEdit then try



QWidget *testTableItem::createEditor() const
{
QLineEdit *lineEdit = new QLineEdit(viewport());
lineEdit->setFrame(false);
//use connect here.
return lineEdit;
}

:db:sStrong
22nd May 2006, 13:57
You can type "1" because to type "1000" you have to be able to type "1" :) It's just not being accepted as a valid value, but you have to be able to type it in. I think that if the lineedit loses focus, the value will be corrected to a valid one. But maybe it'd be easier for you to use QSpinBox?
no i have to use qlineedit in order to clear. the users should only type >2 and <16000 i am going to use focus now lets see what happens..

wysota
22nd May 2006, 13:59
So tell me something. How do you intend to enter "1111" (which is a valid value) if you forbid to enter a "1"?

wysota
22nd May 2006, 14:14
Here is my code, try it:


#include <QApplication>
#include <QIntValidator>
#include <QLineEdit>
#include <QDialog>
#include <QVBoxLayout>

class MyIntValidator : public QIntValidator {
public:
MyIntValidator(int l, int t, QObject *p=0) : QIntValidator(l,t,p){}
void fixup(QString &input) const;
};

void MyIntValidator::fixup(QString &input) const {
int v = input.toInt();
if(v<bottom()) input = QString::number(bottom());
if(v>top()) input = QString::number(top());
}

int main(int argc, char **argv){
QApplication app(argc, argv);
QDialog dlg;
QVBoxLayout *layout = new QVBoxLayout;
QLineEdit *le = new QLineEdit(&dlg);
QLineEdit *other = new QLineEdit(&dlg);
layout->addWidget(le);
layout->addWidget(other);
dlg.setLayout(layout);
QIntValidator *validator = new MyIntValidator(3,16000, le);
le->setValidator(validator);
dlg.show();
return app.exec();
}

:db:sStrong
22nd May 2006, 17:26
Here is my code, try it:



hi I have change ur code according to my situation because i dont use QLineEdit as a class i have qtable and just one COL is made QlineEdit, this is the code:


QWidget* TestTableItem::createEditor() const
{

QLineEdit* le = ( QLineEdit *)(QTableItem::createEditor());
QString input;
int v = input.toInt();

if (le)
{
QIntValidator* validator = new QIntValidator( 3,16000 , le );
if(v > validator->bottom()) input = QString::number(validator->bottom());
if(v <validator->top()) input = QString::number(validator->top());
validator->fixup(input);
le->setValidator(validator);

}

return le;
}

the fixup doesnt work good because i am still able to write 1 and 2 :(

wysota
22nd May 2006, 18:47
Maybe you didn't notice -- I subclassed QIntValidator. The default implementation of fixup() does nothing so you have to subclass the validator and implement it.

:db:sStrong
23rd May 2006, 10:25
Maybe you didn't notice -- I subclassed QIntValidator. The default implementation of fixup() does nothing so you have to subclass the validator and implement it.

oke i see i have also subclass QIntvalidator:

class MyIntValidator : public QIntValidator {

public:

MyIntValidator(int l, int t, QObject *p=0) : QIntValidator(l,t,p){}

void fixup(QString &input) const;

};

void MyIntValidator::fixup(QString &input) const {

int v = input.toInt();

if(v<bottom()) input = QString::number(bottom());

if(v>top()) input = QString::number(top());

}

QWidget* TestTableItem::createEditor() const
{

QLineEdit* le = ( QLineEdit *)(QTableItem::createEditor());

QIntValidator* validator = new QIntValidator(3,16000 , le );
le->setValidator(validator);

return le;
}


but still its not working, am i doing somthing wrong with subclassing??

wysota
23rd May 2006, 11:03
QIntValidator* validator = new QIntValidator(3,16000 , le );
You are still creating an instance of QIntValidator instead of your subclass.

:db:sStrong
23rd May 2006, 11:35
QIntValidator* validator = new QIntValidator(3,16000 , le );
You are still creating an instance of QIntValidator instead of your subclass.



oh jeeeeeee lol i see it now, i should do this:


MyIntValidator * validator = new MyIntValidator(3,16000,le);

oke thanx alot for ur help :)