Results 1 to 11 of 11

Thread: problem with signal/slot mechanismus

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Red face problem with signal/slot mechanismus

    Hello to all, my dear experts!

    I have following line:
    Qt Code:
    1. connect(m_pLeftButtonCaptionInputWidget->ptrTextInput(),
    2. SIGNAL(textChanged()),
    3. this,
    4. SLOT(previewButtonSetText()));
    To copy to clipboard, switch view to plain text mode 
    m_pLeftButtonCaptionInputWidget is declared as follows:
    Qt Code:
    1. #ifndef CDATAINPUTWIDGET_H_
    2. #define CDATAINPUTWIDGET_H_
    3.  
    4. // qt includes
    5. #include <QLabel>
    6. #include <QLineEdit>
    7. #include <QPointer>
    8. #include <QHBoxLayout>
    9. #include <QGroupBox>
    10. #include <QFileDialog>
    11. #include <QStringList>
    12. #include <QPushButton>
    13.  
    14. // forward declarations
    15. class QWidget;
    16. class QString;
    17.  
    18. class CDataInputWidget : public QWidget
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. CDataInputWidget(QWidget* pParent=0,
    24. QString strLabelCaption="Caption",
    25. QString strDefaultValue="Caption",
    26. bool boolCheckable=false,
    27. bool boolFileDialog=false);
    28. ~CDataInputWidget();
    29.  
    30. inline QPointer<QHBoxLayout> layout()
    31. { return m_pLayout; };
    32. inline QPointer<QLabel> label()
    33. { return m_pLabel; };
    34. inline QString textInput()
    35. { return m_pTextInput->text(); };
    36. inline QPointer<QLineEdit> ptrTextInput()
    37. { return m_pTextInput; };
    38. inline QPointer<QGroupBox> inputGroupBox()
    39. { return m_pInputGroupBox; };
    40. inline QPointer<QVBoxLayout> mainLayout()
    41. { return m_pMainLayout; };
    42. inline QPointer<QFileDialog> fileDiaog()
    43. { return m_pFileDialog; };
    44. inline QString path()
    45. { return m_strPath; };
    46. inline void setPath(QString strPath)
    47. { m_strPath=strPath; };
    48. inline QPointer<QPushButton> fileBrowserButton()
    49. { return m_pFileBrowserButton; };
    50.  
    51. private:
    52. QPointer<QHBoxLayout> m_pLayout;
    53. QPointer<QLabel> m_pLabel;
    54. QPointer<QLineEdit> m_pTextInput;
    55. QPointer<QGroupBox> m_pInputGroupBox;
    56. QPointer<QVBoxLayout> m_pMainLayout;
    57. QPointer<QFileDialog> m_pFileDialog;
    58. QString m_strPath;
    59. QPointer<QPushButton> m_pFileBrowserButton;
    60.  
    61. private slots:
    62. void showFileBrowser();
    63. };
    64.  
    65. #endif /*CDATAINPUTWIDGET_H_*/
    To copy to clipboard, switch view to plain text mode 
    and its implemenation is:
    Qt Code:
    1. #include "CDataInputWidget.h"
    2.  
    3. CDataInputWidget::CDataInputWidget(QWidget* pParent,
    4. QString strLabelCaption,
    5. QString strDefaultValue,
    6. bool boolCheckable,
    7. bool boolFileDialog)
    8. : QWidget(pParent)
    9. {
    10. m_pMainLayout=new QVBoxLayout(); // creates new layout
    11. Q_CHECK_PTR(m_pMainLayout); // checks creation
    12.  
    13. m_pLayout=new QHBoxLayout(); // creates new layout
    14. Q_CHECK_PTR(m_pLayout); // checks creation
    15.  
    16. m_pLabel=new QLabel(strLabelCaption, this); // creates new label
    17. Q_CHECK_PTR(m_pLabel); // checks creation
    18.  
    19. m_pTextInput=new QLineEdit(strDefaultValue, this); // creates new text input
    20. Q_CHECK_PTR(m_pTextInput); // checks creation
    21.  
    22. m_pLayout->addWidget(m_pLabel); // adds label to layout
    23. m_pLayout->addWidget(m_pTextInput); // adds text input widget
    24.  
    25. m_pInputGroupBox=new QGroupBox(strLabelCaption, this); // creates new gb
    26. Q_CHECK_PTR(m_pInputGroupBox); // checks creation
    27. if (boolCheckable==true)
    28. {
    29. m_pInputGroupBox->setCheckable(true); // toogles check mode
    30. if (boolFileDialog==true)
    31. {
    32. m_pFileBrowserButton=new QPushButton(tr("Browse ..."), this); // creates new button
    33. Q_CHECK_PTR(m_pFileBrowserButton); // checks creation
    34. connect(m_pFileBrowserButton,
    35. SIGNAL(clicked()),
    36. this,
    37. SLOT(showFileBrowser())); // connect click to slot
    38. m_pLayout->addWidget(m_pFileBrowserButton); // addds button to layout
    39. } // if
    40. m_pInputGroupBox->setLayout(m_pLayout); // sets layout
    41. }; // if
    42. m_pMainLayout->addWidget(m_pInputGroupBox); // adds wudget to layout
    43. setLayout(m_pMainLayout);
    44. }
    45.  
    46. CDataInputWidget::~CDataInputWidget()
    47. {
    48. }
    49.  
    50. void CDataInputWidget::showFileBrowser()
    51. {
    52. m_pFileDialog=new QFileDialog(this, tr("Choose picture"),
    53. "images/application",
    54. "*.png"); // creates new file diaog
    55. Q_CHECK_PTR(m_pFileDialog); // checks creation
    56. if(m_pFileDialog->exec())
    57. {
    58. setPath(m_pFileDialog->selectedFiles().at(0));
    59. m_pTextInput->setText(path());
    60. }
    61. }
    To copy to clipboard, switch view to plain text mode 
    Now, here is the problem. Object, instanatiated from this class does not recognize signal textChanged() according to debug output:
    no such signal QTextEdit::textChanged()
    which I need to notify parent object that on text on button has been changed and it the button caption must be updated according to new text.
    Last edited by wysota; 7th March 2008 at 00:08. Reason: Changed [code] to [quote]
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with signal/slot mechanismus

    Maybe you should try returning a real pointer instead of QPointer? What's the point of doing that, anyway?

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem with signal/slot mechanismus

    What do you mean by "what is the point of doing that, anyway?"?
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with signal/slot mechanismus

    What's the reason for returning QPointers instead of real pointers?

  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem with signal/slot mechanismus

    I do not know, I just work with QPointers, is this wrong?

    Ok, I've changed:
    Qt Code:
    1. QPointer<CDataInputWidget> m_pLeftButtonCaptionInputWidget;
    To copy to clipboard, switch view to plain text mode 
    into
    Qt Code:
    1. CDataInputWidget* m_pLeftButtonCaptionInputWidget;
    To copy to clipboard, switch view to plain text mode 
    I get same result, the slot is not called I get same warning:
    Qt Code:
    1. warning: Object::connect: No such signal QLineEdit::textChanged()
    To copy to clipboard, switch view to plain text mode 
    so there is no difference if I return QPointer or ordinary pointer.
    Qt 5.3 Opensource & Creator 3.1.2

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: problem with signal/slot mechanismus

    Please, try to be more exact with your problems. In the first post you have QTextEdit::textChanged() and now you have QLineEdit::textChanged(). This makes a clear difference because QTextEdit does have such signal whereas QLineEdit doesn't. The corresponding signal of QLineEdit carries a parameter. Refer docs for more details.


    About QPointer, using it everywhere is a faulty approach. You should use QPointer only when you need to.
    Guarded pointers are useful whenever you need to store a pointer to a QObject that is owned by someone else, and therefore might be destroyed while you still hold a reference to it. You can safely test the pointer for validity.
    Why do you expose all the private stuff of CDataInputWidget to the public anyway?
    Last edited by jpn; 7th March 2008 at 12:02. Reason: spelling error
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem with signal/slot mechanismus

    God damn, sorry, It will not happen again. My mistake. You can delete whole post if you want to ...
    Qt 5.3 Opensource & Creator 3.1.2

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with signal/slot mechanismus

    Hey, we're just asking We only want to help you improve your code, not to say you are stupid or something.

  9. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem with signal/slot mechanismus

    Ok, thanks, signal still does not connect, god damn, this is driving me mad. I will take a look at examples and then I return with further questions ...
    Qt 5.3 Opensource & Creator 3.1.2

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with signal/slot mechanismus

    Did you pass the parameter signature for the signal in the connect statement?

  11. The following user says thank you to wysota for this useful post:

    MarkoSan (7th March 2008)

  12. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem with signal/slot mechanismus

    Wysotta, thanks for tip, now I did it as you said in previous post and it works!!! Thank you a lot!!!
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. Replies: 2
    Last Post: 20th September 2007, 13:27
  2. problem with signal/slot
    By ihoss in forum Newbie
    Replies: 2
    Last Post: 24th August 2007, 23:59
  3. Replies: 16
    Last Post: 7th March 2006, 16:57

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.