PDA

View Full Version : problem with signal/slot mechanismus



MarkoSan
6th March 2008, 22:44
Hello to all, my dear experts!

I have following line:
connect(m_pLeftButtonCaptionInputWidget->ptrTextInput(),
SIGNAL(textChanged()),
this,
SLOT(previewButtonSetText()));
m_pLeftButtonCaptionInputWidget is declared as follows:
#ifndef CDATAINPUTWIDGET_H_
#define CDATAINPUTWIDGET_H_

// qt includes
#include <QLabel>
#include <QLineEdit>
#include <QPointer>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QFileDialog>
#include <QStringList>
#include <QPushButton>

// forward declarations
class QWidget;
class QString;

class CDataInputWidget : public QWidget
{
Q_OBJECT

public:
CDataInputWidget(QWidget* pParent=0,
QString strLabelCaption="Caption",
QString strDefaultValue="Caption",
bool boolCheckable=false,
bool boolFileDialog=false);
~CDataInputWidget();

inline QPointer<QHBoxLayout> layout()
{ return m_pLayout; };
inline QPointer<QLabel> label()
{ return m_pLabel; };
inline QString textInput()
{ return m_pTextInput->text(); };
inline QPointer<QLineEdit> ptrTextInput()
{ return m_pTextInput; };
inline QPointer<QGroupBox> inputGroupBox()
{ return m_pInputGroupBox; };
inline QPointer<QVBoxLayout> mainLayout()
{ return m_pMainLayout; };
inline QPointer<QFileDialog> fileDiaog()
{ return m_pFileDialog; };
inline QString path()
{ return m_strPath; };
inline void setPath(QString strPath)
{ m_strPath=strPath; };
inline QPointer<QPushButton> fileBrowserButton()
{ return m_pFileBrowserButton; };

private:
QPointer<QHBoxLayout> m_pLayout;
QPointer<QLabel> m_pLabel;
QPointer<QLineEdit> m_pTextInput;
QPointer<QGroupBox> m_pInputGroupBox;
QPointer<QVBoxLayout> m_pMainLayout;
QPointer<QFileDialog> m_pFileDialog;
QString m_strPath;
QPointer<QPushButton> m_pFileBrowserButton;

private slots:
void showFileBrowser();
};

#endif /*CDATAINPUTWIDGET_H_*/and its implemenation is:
#include "CDataInputWidget.h"

CDataInputWidget::CDataInputWidget(QWidget* pParent,
QString strLabelCaption,
QString strDefaultValue,
bool boolCheckable,
bool boolFileDialog)
: QWidget(pParent)
{
m_pMainLayout=new QVBoxLayout(); // creates new layout
Q_CHECK_PTR(m_pMainLayout); // checks creation

m_pLayout=new QHBoxLayout(); // creates new layout
Q_CHECK_PTR(m_pLayout); // checks creation

m_pLabel=new QLabel(strLabelCaption, this); // creates new label
Q_CHECK_PTR(m_pLabel); // checks creation

m_pTextInput=new QLineEdit(strDefaultValue, this); // creates new text input
Q_CHECK_PTR(m_pTextInput); // checks creation

m_pLayout->addWidget(m_pLabel); // adds label to layout
m_pLayout->addWidget(m_pTextInput); // adds text input widget

m_pInputGroupBox=new QGroupBox(strLabelCaption, this); // creates new gb
Q_CHECK_PTR(m_pInputGroupBox); // checks creation
if (boolCheckable==true)
{
m_pInputGroupBox->setCheckable(true); // toogles check mode
if (boolFileDialog==true)
{
m_pFileBrowserButton=new QPushButton(tr("Browse ..."), this); // creates new button
Q_CHECK_PTR(m_pFileBrowserButton); // checks creation
connect(m_pFileBrowserButton,
SIGNAL(clicked()),
this,
SLOT(showFileBrowser())); // connect click to slot
m_pLayout->addWidget(m_pFileBrowserButton); // addds button to layout
} // if
m_pInputGroupBox->setLayout(m_pLayout); // sets layout
}; // if
m_pMainLayout->addWidget(m_pInputGroupBox); // adds wudget to layout
setLayout(m_pMainLayout);
}

CDataInputWidget::~CDataInputWidget()
{
}

void CDataInputWidget::showFileBrowser()
{
m_pFileDialog=new QFileDialog(this, tr("Choose picture"),
"images/application",
"*.png"); // creates new file diaog
Q_CHECK_PTR(m_pFileDialog); // checks creation
if(m_pFileDialog->exec())
{
setPath(m_pFileDialog->selectedFiles().at(0));
m_pTextInput->setText(path());
}
}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.

wysota
6th March 2008, 23:08
Maybe you should try returning a real pointer instead of QPointer? What's the point of doing that, anyway?

MarkoSan
6th March 2008, 23:12
What do you mean by "what is the point of doing that, anyway?"?

wysota
6th March 2008, 23:58
What's the reason for returning QPointers instead of real pointers?

MarkoSan
7th March 2008, 00:19
I do not know, I just work with QPointers, is this wrong?

Ok, I've changed:
QPointer<CDataInputWidget> m_pLeftButtonCaptionInputWidget; into
CDataInputWidget* m_pLeftButtonCaptionInputWidget;I get same result, the slot is not called I get same warning:
warning: Object::connect: No such signal QLineEdit::textChanged()so there is no difference if I return QPointer or ordinary pointer.

jpn
7th March 2008, 07:06
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?

MarkoSan
7th March 2008, 08:25
God damn, sorry, It will not happen again. My mistake. You can delete whole post if you want to ...

wysota
7th March 2008, 10:57
Hey, we're just asking :) We only want to help you improve your code, not to say you are stupid or something.

MarkoSan
7th March 2008, 12:48
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 ...

wysota
7th March 2008, 13:56
Did you pass the parameter signature for the signal in the connect statement?

MarkoSan
7th March 2008, 19:05
Wysotta, thanks for tip, now I did it as you said in previous post and it works!!! Thank you a lot!!!