PDA

View Full Version : promoted widgets using QT designer is not calling its constructor



Ratheendrans
3rd April 2011, 08:30
Dear All,

I made a subclass of QLineEdit class and implemented its constructor and keyPressEvent event handler,then I promoted one of the instances of QLineEdit to subclass with QT Designer by right clicking and selecting the promote option. I am able to call keyPressEvent() method in my application. however the constructor of this subclass is never called.

header file.


class printlineEdit : public QLineEdit
{
//Q_OBJECT

public:
//signals:
// void escapePress();
public:

printlineEdit(QWidget *parent = 0);
void keyPressEvent( QKeyEvent *event );
unsigned int ValuePut,val;

};


its corresponding c++ file.


printlineEdit::printlineEdit(QWidget *parent):QLineEdit(parent)
{
ValuePut=0;
//selectionStart ();
//setCursorPosition(0);
printf("Inside printline constr\n");
}

void printlineEdit::keyPressEvent(QKeyEvent *event)
{
bool flag=0;
QString ch("");

switch(event->key())
{
case Qt::Key_Left:
break;
case Qt::Key_Right:

break;
case Qt::Key_Up:
break;
case Qt::Key_Down:

break;
case Qt::Key_Return:
flag=1;
printf("Enter Pressed\n");
break;
}
}
}

Now for the above code the constructor printlineEdit(QWidget *parent) is never called.

Kindly tell me how to implement a constructor in these scenario.

Ratheendran

wysota
3rd April 2011, 08:38
How do you know it is never called?

stampede
3rd April 2011, 08:45
Maybe other constructor for QLineEdit is called, have you tried to implement the other one ?


class printlineEdit : public QLineEdit
{
//Q_OBJECT

public:
//signals:
// void escapePress();
public:

printlineEdit(QWidget *parent = 0);
printlineEdit(const QString& text, QWidget *parent = 0);
...
...
// .cpp
printlineEdit::printlineEdit(const QString& text, QWidget *parent):QLineEdit(text,parent)
{
ValuePut=0;
//selectionStart ();
//setCursorPosition(0);
printf("Inside printline constr 2\n");
}

wysota
3rd April 2011, 08:48
uic only calls the constructor that takes a parent:

void setupUi(QWidget *Form)
{
if (Form->objectName().isEmpty())
Form->setObjectName(QString::fromUtf8("Form"));
Form->resize(400, 300);
lineEdit = new printlineEdit(Form);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
lineEdit->setGeometry(QRect(80, 40, 113, 20));

retranslateUi(Form);

QMetaObject::connectSlotsByName(Form);
} // setupUi

stampede
3rd April 2011, 08:54
uic only calls the constructor that takes a parent
You are right :)
Take a look into the ui_*.h file for this form, this way you'll know what is called for your line edit.

Ratheendrans
3rd April 2011, 09:40
Thanks for your support.

I was able to figure out the issue.

I deleted the promoted widget and created them again,so the text message which I update in the constructor is now visible.

issue is caused because,I used ui designer to put the default text, once I compiled,then I removed the text from ui designer and implemented it in constructor using setText() method. This was creating the confusion.

Another thing I want to ask is there way to call a user defined constructor using the same method.

Ratheendran

wysota
3rd April 2011, 11:15
No, Designer always calls the constructor with a parent argument.