PDA

View Full Version : Dynamic object's method.



kalwi
30th July 2013, 14:03
Hi, I wrote code to generating simple form:

QDialog *dialogWindow = new QDialog(this);
dialogWindow->setWindowTitle("Adding text");
dialogWindow->setFixedSize(350, 200);
QGridLayout *layout = new QGridLayout(dialogWindow);
QPlainTextEdit *text = new QPlainTextEdit();
QDialogButtonBox *buttonBox = new QDialogButtonBox(dialogWindow);
buttonBox->addButton(QDialogButtonBox::Ok);
buttonBox->addButton(QDialogButtonBox::Cancel);
layout->addWidget(text);
layout->addWidget(buttonBox);
dialogWindow->setLayout(layout);
dialogWindow->connect(buttonBox, SIGNAL(accepted()), dialogWindow, SLOT(accept()));
dialogWindow->connect(buttonBox, SIGNAL(rejected()), dialogWindow, SLOT(reject()));But, I didn't know how to write method onTextChanged to QPlainTextEdit object? Any ideas?

yeye_olive
30th July 2013, 14:21
Sure, connect the QPlainTextEdit's textChanged() (http://qt-project.org/doc/qt-5.0/qtwidgets/qplaintextedit.html#plainText-prop) signal to a custom slot where you put your code. You should learn basics about signals and slots (http://qt-project.org/doc/qt-5.1/qtcore/signalsandslots.html) and widget applications (http://qt-project.org/doc/qt-5.1/qtdoc/gettingstartedqt.html) first.

kalwi
30th July 2013, 14:49
Yes, but I would like to do if QPlainTextEdit->toPlainText() return empty string, ok button is disabled else it is enabled. I can use signals and slots, but I didn't know how do that in this case.

yeye_olive
30th July 2013, 15:34
In the slot connected to textChanged() that I mentioned in my previous post, call toPlainText().isEmpty() on your QPlainTextEdit to know whether its text is empty, then setEnabled() on the button as needed.

kalwi
30th July 2013, 18:24
But to do that I need an additional method and two pointers to QPlainTextEdit and QPushButton, because I create a QDialog in the method of the main form, right? Otherwise can not be?

anda_skoa
31st July 2013, 08:39
Indeed.

In any case, creating a form that way is not very common, best practise is to create a subclass of, in this case QDialog, and encapsulate all its internals into that class.

That way the user of the dialog/form only deals with the class' public API, independent on how it is actually implemented internally.

Cheers,
_

kalwi
31st July 2013, 11:23
Ok, thanks.