Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Trouble with slot execution
-
Please help. I had faced a runtime error. My program falls when execution reaches a specific line in slot-method. The problem row of code had found. But failure cause is not clear to me.
//IT IS MY CLASS WITH PROBLEM SLOT () class RegisterForm : public QDialog { Q_OBJECT public: QLineEdit * name; QLineEdit * pass1; QLineEdit * pass2; private: QPushButton * newKey; QPushButton * editKey; QPushButton * removeKey; QPushButton * okButton; QSettings * settings; QTableView * keysTable; TableModel * model; void checkPass(); public: RegisterForm(QSettings *settings, QWidget *parent = 0); ~RegisterForm(); public slots: void slotAddKey(); void slotAddKey(QString name, QString address); void slotEditKey(); void slotRemoveKey(); void slotPassEdited(); }; //CONSTRUCTOR TO BE USED IN ME PROGRAM RegisterForm::RegisterForm(QSettings *settings, QWidget *parent/*=0*/) : QDialog(parent), settings(settings) { QLineEdit * name = new QLineEdit; QLineEdit * pass1 = new QLineEdit("zxc"); QLineEdit * pass2 = new QLineEdit("abc"); QVBoxLayout * mainLayout = new QVBoxLayout; QFormLayout * inputLayout = new QFormLayout; inputLayout->addRow("Enter your &name",name); inputLayout->addRow("fill &password",pass1); inputLayout->addRow("&repeat password",pass2); QVBoxLayout * keysLayout = new QVBoxLayout; QPushButton * newKey = new QPushButton("New key"); QPushButton * editKey = new QPushButton("Edit Key"); QPushButton * removeKey = new QPushButton("Remove key"); QPushButton * okButton = new QPushButton("Ok"); keysLayout->addStretch(1); keysLayout->setSpacing(10); keysLayout->addWidget(newKey); keysLayout->addWidget(editKey); keysLayout->addWidget(removeKey); keysLayout->addStretch(1); keysLayout->addWidget(okButton); QHBoxLayout * midLayout = new QHBoxLayout; midLayout->addLayout(keysLayout); QTableView * keysTable = new QTableView; midLayout->addWidget(keysTable); mainLayout->addLayout(inputLayout); mainLayout->addLayout(midLayout); this->setLayout(mainLayout); connect(pass1, SIGNAL(editingFinished()), SLOT(slotPassEdited())); //CONNECTIONG OF PROBLEM SLOT connect(pass2, SIGNAL(editingFinished()), SLOT(slotPassEdited())); //ANOTHER CONNECTIONG OF PROBLEM SLOT connect(newKey, SIGNAL(clicked()), SLOT(slotAddKey())); connect(editKey, SIGNAL(clicked()), SLOT(slotEditKey())); connect(removeKey, SIGNAL(clicked()), SLOT(slotRemoveKey())); connect(okButton, SIGNAL(clicked()), SLOT(accept())); } //MY PROBLEM METHOD DEFINITION void RegisterForm::slotPassEdited() { qDebug()<<"slotPassEdited - START"; this->pass1->text(); //JUST FOR TEST - FALLS AT THIS ROW qDebug()<<"this->pass1->text()"; QPalette pal1 = QPalette(); qDebug()<<": "<< name->text(); if ((pass1->text() == "") || (pass2->text() == "")){ qDebug()<<"111"; return; }else if(pass1->text() != pass1->text()){ qDebug()<<"222"; pal1.setColor(QPalette::Text, Qt::red); pass1->setPalette(pal1); pass2->setPalette(pal1); return; } qDebug()<<"333"; pal1.setColor(QPalette::Text, Qt::black); //pass1->setPalette(pal1); //pass2->setPalette(pal1); qDebug()<<"444"; }
I note that the error is not related to a slot using concretely. So I tried to use the code in a separate method with slot's content and then called it from the slot. The result is the same. This problem is also caused by any use of the members of the class ("pass1" and "pass2") inside the slot-function.
Помогите. Я столкнулся с ошибкой времени выполнения приложения. По достижении программой конкретной строки в методе слоте, она (программа) ломается. Проблемная строка найдена, но причина поломки не ясна для меня.
Отмечу что ошибка не связана со слотом, как с таковым, так как я пробовал использовать отдельный метод с содержимым слота и затем вызывал его из слота. Результат тот же. Проблема также проявляет себя во время любого использования полей класса ("pass1", "pass2") внутри слота. Собственно, где я мог "накосячить"?
-
Hi,
QLineEdit * pass1 = new QLineEdit("zxc");
This line is wrong. You initialized a local variable called
pass1
. This is not the same as the member variable calledpass1
!Replace it with:
this->pass1 = new QLineEdit("zxc");
-
@JKSH said:
his line is wrong. You initialized a local variable called pass1
oops, its my silly fault. Thanks for help, and sorry for stupid questions.
-
@Warbit said:
oops, its my silly fault. Thanks for help, and sorry for stupid questions.
You're welcome :-) Please don't feel bad; I think it was a perfectly valid question.
Happy coding!