Results 1 to 7 of 7

Thread: Dynamic object's method.

  1. #1
    Join Date
    Feb 2013
    Posts
    12
    Qt products
    Qt5
    Platforms
    Windows

    Default Dynamic object's method.

    Hi, I wrote code to generating simple form:
    Qt Code:
    1. QDialog *dialogWindow = new QDialog(this);
    2. dialogWindow->setWindowTitle("Adding text");
    3. dialogWindow->setFixedSize(350, 200);
    4. QGridLayout *layout = new QGridLayout(dialogWindow);
    5. QPlainTextEdit *text = new QPlainTextEdit();
    6. QDialogButtonBox *buttonBox = new QDialogButtonBox(dialogWindow);
    7. buttonBox->addButton(QDialogButtonBox::Ok);
    8. buttonBox->addButton(QDialogButtonBox::Cancel);
    9. layout->addWidget(text);
    10. layout->addWidget(buttonBox);
    11. dialogWindow->setLayout(layout);
    12. dialogWindow->connect(buttonBox, SIGNAL(accepted()), dialogWindow, SLOT(accept()));
    13. dialogWindow->connect(buttonBox, SIGNAL(rejected()), dialogWindow, SLOT(reject()));
    To copy to clipboard, switch view to plain text mode 
    But, I didn't know how to write method onTextChanged to QPlainTextEdit object? Any ideas?

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic object's method.

    Sure, connect the QPlainTextEdit's textChanged() signal to a custom slot where you put your code. You should learn basics about signals and slots and widget applications first.

  3. #3
    Join Date
    Feb 2013
    Posts
    12
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamic object's method.

    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.

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic object's method.

    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.

  5. #5
    Join Date
    Feb 2013
    Posts
    12
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamic object's method.

    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic object's method.

    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,
    _

  7. #7
    Join Date
    Feb 2013
    Posts
    12
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamic object's method.

    Ok, thanks.

Similar Threads

  1. Call object method using qtscript
    By mvbhavsar in forum Newbie
    Replies: 4
    Last Post: 10th March 2013, 11:19
  2. Call method of Object in different Thread
    By Qiieha in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2012, 14:14
  3. dynamic object id
    By jovin in forum Qt Quick
    Replies: 2
    Last Post: 1st May 2012, 03:19
  4. How to generate dynamic object
    By tuxit in forum Qt Quick
    Replies: 0
    Last Post: 17th August 2011, 14:49
  5. How to drag dynamic object in Qml?
    By Franziss in forum Newbie
    Replies: 0
    Last Post: 28th May 2011, 18:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.