Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, December 2006
qFindChild not finding child


Message 1 in thread

Hi,

I want to load a widget dynamically with QUILoader.
Everything works fine but when I want to retrieve pointers to the 
widget's children (pushButtons) in a plug in with qFindChild it can't 
find any.

Here's what I am doing

In the main Application

myWidget = uiLoader.load(&file,MainTab);                       
guiPlugIn->makeConnections(myWidget);


In the plugIn

void PlugInTest::makeConnections(QWidget * myWidget){
    qDebug()<<myWidget->children();  -->Here I can see that there is a 
widget called pushButton that is a child of myWidget
    ui_pushButton = qFindChild<QPushButton*>(myWidget, "pushButton");

    qDebug()<<ui_pushButton; --->>>QObject(0x0)!!!!!!
      
    
connect(ui_pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_Clicked())); 
-->>cannot connect ...
}

Is there something I am missing here?
Why does qFindChild do not find a child when children() function can 
find it??

Elise

--
 [ signature omitted ] 

Message 2 in thread

Nobody knows why findChild or qFindChild might not work??

Elise Taillant wrote:

> Hi,
>
> I want to load a widget dynamically with QUILoader.
> Everything works fine but when I want to retrieve pointers to the 
> widget's children (pushButtons) in a plug in with qFindChild it can't 
> find any.
>
> Here's what I am doing
>
> In the main Application
>
> myWidget = uiLoader.load(&file,MainTab);                       
> guiPlugIn->makeConnections(myWidget);
>
>
> In the plugIn
>
> void PlugInTest::makeConnections(QWidget * myWidget){
>    qDebug()<<myWidget->children();  -->Here I can see that there is a 
> widget called pushButton that is a child of myWidget
>    ui_pushButton = qFindChild<QPushButton*>(myWidget, "pushButton");
>
>    qDebug()<<ui_pushButton; --->>>QObject(0x0)!!!!!!
>         
> connect(ui_pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_Clicked())); 
> -->>cannot connect ...
> }
>
> Is there something I am missing here?
> Why does qFindChild do not find a child when children() function can 
> find it??
>
> Elise
>
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>

-- 
 [ signature omitted ] 

Message 3 in thread

Hi, Elise,

I think qFindChild is ok. But you need do something about the connect.
Perhaps you can read this:
http://doc.trolltech.com/4.2/designer-calculatorbuilder.html

-- 
 [ signature omitted ] 

Message 4 in thread

Thanx Cavendish,
I tried both Qt 4.1 and Qt 4.2, the problem only occurs with Qt4.1, so 
it seems its a feature that has been corrected in qt4.2.
I first tried what is written in the link you sent me, but that don't 
change anything about the fact that qfindChild don't seem to find the 
requested child.

Elise


Cavendish Qi wrote:

> Hi, Elise,
>
> I think qFindChild is ok. But you need do something about the connect.
> Perhaps you can read this:
> http://doc.trolltech.com/4.2/designer-calculatorbuilder.html
>

-- 
 [ signature omitted ] 

Message 5 in thread

Elise Taillant wrote:

> I want to load a widget dynamically with QUILoader.
> Everything works fine but when I want to retrieve pointers to the
> widget's children (pushButtons) in a plug in with qFindChild it can't
> find any.
> 
> Here's what I am doing
> 
> In the main Application
> 
> myWidget = uiLoader.load(&file,MainTab);
> guiPlugIn->makeConnections(myWidget);
> 
> 
> In the plugIn
> 
> void PlugInTest::makeConnections(QWidget * myWidget){
>     qDebug()<<myWidget->children();  -->Here I can see that there is a
> widget called pushButton that is a child of myWidget
>     ui_pushButton = qFindChild<QPushButton*>(myWidget, "pushButton");
> 
>     qDebug()<<ui_pushButton; --->>>QObject(0x0)!!!!!!
>
connect(ui_pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_Clicked()));
> -->>cannot connect ...
> }
> 
> Is there something I am missing here?
> Why does qFindChild do not find a child when children() function can
> find it??

Have you also tried using QObject::findChild() to try and obtain the push
button, or tried

  qFindChild<QWidget*>(myWidget, "pushButton");

to see if it can obtain the push button as a QWidget?

It may be that you need to provide a more complete example, perhaps as part
of a support request. It might also be useful if you could say which
platform you're using Qt on.

-- 
 [ signature omitted ] 

Message 6 in thread

As I said in my previous answer, It seems to occur only with 4.1 and not 
4.2. I'm working on linux.
I'm going to try your ideas even if it works now with Qt4.2.

Here my more complete code :

//function loading a plug in

void  MVStudio::addPlugIn(){

    QString plugInFile = QFileDialog::getOpenFileName(
            this,
            "Choose a plug in ",
            ".","library (*.so *.dll)",0,QFileDialog::DontResolveSymlinks);
    QPluginLoader loader(plugInFile);
    qDebug()<<loader.fileName();
    QObject *plugin = loader.instance();
    if (plugin) {
        InterfaceGUI * guiPlugIn = qobject_cast<InterfaceGUI *>(plugin);
        if (guiPlugIn)
        {  
                POSITION pos= guiPlugIn->getPosition();
                QWidget * myWidget;
                QUiLoader uiLoader;
                QString uiFile = 
plugInFile.remove("lib").remove("so").append("ui");
                QFile file(uiFile);//the ui file to load
                if(file.open(QFile::ReadOnly)){
               
                       
                          myWidget = uiLoader.load(&file,MainTab);
                        guiPlugIn->makeConnections(myWidget);
                        
MainTab->addTab(myWidget,guiPlugIn->getTitle());                       
                  
                file.close();
            }
            else
                qWarning("Cannot find ui file for plugin");
        }
       }
    else qDebug()<<"no plug in";

}


Now in my plug in


void PlugInTest::makeConnections(QWidget * myWidget){
   
    ui_pushButton = 
myWidget->findChild<QPushButton*>(QString("pushButton"));
    ui_radioButton = 
myWidget->findChild<QRadioButton*>(QString("radioButton"));
   
 
    
connect(ui_pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_Clicked()));
    
connect(ui_radioButton,SIGNAL(clicked()),this,SLOT(on_radioButton_Clicked()));
   

}

If I do a dumpObjectTree a myWidget inside makeConnection, I can see 
that there is a push button and a radiobutton. But findChild or 
qFindChild are unable to find them.

Elise






David Boddie wrote:

>Elise Taillant wrote:
>
>  
>
>>I want to load a widget dynamically with QUILoader.
>>Everything works fine but when I want to retrieve pointers to the
>>widget's children (pushButtons) in a plug in with qFindChild it can't
>>find any.
>>
>>Here's what I am doing
>>
>>In the main Application
>>
>>myWidget = uiLoader.load(&file,MainTab);
>>guiPlugIn->makeConnections(myWidget);
>>
>>
>>In the plugIn
>>
>>void PlugInTest::makeConnections(QWidget * myWidget){
>>    qDebug()<<myWidget->children();  -->Here I can see that there is a
>>widget called pushButton that is a child of myWidget
>>    ui_pushButton = qFindChild<QPushButton*>(myWidget, "pushButton");
>>
>>    qDebug()<<ui_pushButton; --->>>QObject(0x0)!!!!!!
>>
>>    
>>
>connect(ui_pushButton,SIGNAL(clicked()),this,SLOT(on_pushButton_Clicked()));
>  
>
>>-->>cannot connect ...
>>}
>>
>>Is there something I am missing here?
>>Why does qFindChild do not find a child when children() function can
>>find it??
>>    
>>
>
>Have you also tried using QObject::findChild() to try and obtain the push
>button, or tried
>
>  qFindChild<QWidget*>(myWidget, "pushButton");
>
>to see if it can obtain the push button as a QWidget?
>
>It may be that you need to provide a more complete example, perhaps as part
>of a support request. It might also be useful if you could say which
>platform you're using Qt on.
>
>  
>

-- 
 [ signature omitted ] 

Message 7 in thread

> As I said in my previous answer, It seems to occur only with 4.1 and not
> 4.2. I'm working on linux.
> I'm going to try your ideas even if it works now with Qt4.2.

I'm under the impression that qFindChild searches based on the objectName property
of the QObject.  Are you naming the widget "pushButton" via setObjectName, or is
that just the variable name?  Perhaps in 4.1 designer didn't set the objectName
property and it does in 4.2?

Caleb

--
 [ signature omitted ] 

Message 8 in thread

"pushButton" is the name of the object as I set it in qt designer.



Caleb Tennis wrote:

>>As I said in my previous answer, It seems to occur only with 4.1 and not
>>4.2. I'm working on linux.
>>I'm going to try your ideas even if it works now with Qt4.2.
>>    
>>
>
>I'm under the impression that qFindChild searches based on the objectName property
>of the QObject.  Are you naming the widget "pushButton" via setObjectName, or is
>that just the variable name?  Perhaps in 4.1 designer didn't set the objectName
>property and it does in 4.2?
>
>Caleb
>
>--
>To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
>List archive and information: http://lists.trolltech.com/qt-interest/
>
>
>  
>

-- 
 [ signature omitted ] 

Message 9 in thread

> "pushButton" is the name of the object as I set it in qt designer.

Right, but there's a difference between the variable name and the object name.

I get this generated code with designer:

    QPushButton *pushButton;

    void setupUi(QWidget *Form)
    {
    Form->setObjectName(QString::fromUtf8("Form"));
    pushButton = new QPushButton(Form);
    pushButton->setObjectName(QString::fromUtf8("pushButton"));


My guess is that in Qt 4.1, it's not calling setObjectName on the pushButton object.

--
 [ signature omitted ] 

Message 10 in thread

Caleb Tennis wrote:

>> "pushButton" is the name of the object as I set it in qt designer.
> 
> Right, but there's a difference between the variable name and the object
> name.
> 
> I get this generated code with designer:
> 
>     QPushButton *pushButton;
> 
>     void setupUi(QWidget *Form)
>     {
>     Form->setObjectName(QString::fromUtf8("Form"));
>     pushButton = new QPushButton(Form);
>     pushButton->setObjectName(QString::fromUtf8("pushButton"));
> 
> 
> My guess is that in Qt 4.1, it's not calling setObjectName on the
> pushButton object.

It shouldn't make a difference because the form is being dynamically loaded
by QUiLoader instead of being compiled into the application.

Looking at the code for QUiLoader in Qt 4.1 and 4.2, I can't see any obvious
changes that would break object name handling. The setObjectName() function
appears to be called for newly-created widgets in each version. It should be
simple to check whether the name is the cause of the problem: just call
QObject::findChild() without a name argument.

David
-- 
 [ signature omitted ] 

Message 11 in thread

Maybe it's coming from the fact that I try findChild in a plugin on a  
pointer to an object loaded by QUiloader in the main application. 
Because if I try findChild just after Quiloader:loader, it works.

David Boddie wrote:

>Caleb Tennis wrote:
>
>  
>
>>>"pushButton" is the name of the object as I set it in qt designer.
>>>      
>>>
>>Right, but there's a difference between the variable name and the object
>>name.
>>
>>I get this generated code with designer:
>>
>>    QPushButton *pushButton;
>>
>>    void setupUi(QWidget *Form)
>>    {
>>    Form->setObjectName(QString::fromUtf8("Form"));
>>    pushButton = new QPushButton(Form);
>>    pushButton->setObjectName(QString::fromUtf8("pushButton"));
>>
>>
>>My guess is that in Qt 4.1, it's not calling setObjectName on the
>>pushButton object.
>>    
>>
>
>It shouldn't make a difference because the form is being dynamically loaded
>by QUiLoader instead of being compiled into the application.
>
>Looking at the code for QUiLoader in Qt 4.1 and 4.2, I can't see any obvious
>changes that would break object name handling. The setObjectName() function
>appears to be called for newly-created widgets in each version. It should be
>simple to check whether the name is the cause of the problem: just call
>QObject::findChild() without a name argument.
>
>David
>  
>

-- 
 [ signature omitted ] 

Message 12 in thread

[For completeness, I'm sending this to qt-interest as well as to the
 intended recipient. Sorry if you get this twice, Elise.]

On Wednesday 13 December 2006 16:08, Elise Taillant wrote:
> As I said in my previous answer, It seems to occur only with 4.1 and not 
> 4.2. I'm working on linux.
> I'm going to try your ideas even if it works now with Qt4.2.
> 
> Here my more complete code :
> 
> //function loading a plug in

[...]

I don't think the problem is in your plugin loading code.

> Now in my plug in

[Reformatting...]

void PlugInTest::makeConnections(QWidget * myWidget){

    ui_pushButton = myWidget->findChild<QPushButton*>(
        QString("pushButton"));
    ui_radioButton = myWidget->findChild<QRadioButton*>(
        QString("radioButton"));

    connect(ui_pushButton,SIGNAL(clicked()),
            this,SLOT(on_pushButton_Clicked()));
    connect(ui_radioButton,SIGNAL(clicked()),
            this,SLOT(on_radioButton_Clicked()));
}

> If I do a dumpObjectTree a myWidget inside makeConnection, I can see 
> that there is a push button and a radiobutton. But findChild or 
> qFindChild are unable to find them.

OK. I've reconstructed a similar plugin and tested it successfully with
both Qt 4.1.4 and 4.2.0. I can send you the code off-list if you want,
but you may find that the problem is either with your abstract interface
class definition or with the plugin class definition.

Here's my interface class definition:

class Interface
{
public:
    virtual ~Interface() {}
    virtual void makeConnections(QWidget * myWidget) = 0;
};

Q_DECLARE_INTERFACE(Interface,
                    "com.trolltech.internal.Interface/1.0")


And here's the plugin class definition:

class Plugin : public QObject, public Interface
{
    Q_OBJECT
    Q_INTERFACES(Interface)

public:
    void makeConnections(QWidget * myWidget);

public slots:
    void on_pushButton_Clicked();
    void on_radioButton_Clicked();
};


Of course, I also had to include the following line with my plugin's
implementation:

Q_EXPORT_PLUGIN2(plugin, Plugin)

Is any of this substantially different to what you've done?

David
-- 
 [ signature omitted ] 

Message 13 in thread

Hi David,
Thank you for working on my problem. Your code is not substantially 
different to mine. Could you send me your code off list so that I can 
check. That's kind of driving me crazy.

Elise

David Boddie wrote:

>[For completeness, I'm sending this to qt-interest as well as to the
> intended recipient. Sorry if you get this twice, Elise.]
>
>On Wednesday 13 December 2006 16:08, Elise Taillant wrote:
>  
>
>>As I said in my previous answer, It seems to occur only with 4.1 and not 
>>4.2. I'm working on linux.
>>I'm going to try your ideas even if it works now with Qt4.2.
>>
>>Here my more complete code :
>>
>>//function loading a plug in
>>    
>>
>
>[...]
>
>I don't think the problem is in your plugin loading code.
>
>  
>
>>Now in my plug in
>>    
>>
>
>[Reformatting...]
>
>void PlugInTest::makeConnections(QWidget * myWidget){
>
>    ui_pushButton = myWidget->findChild<QPushButton*>(
>        QString("pushButton"));
>    ui_radioButton = myWidget->findChild<QRadioButton*>(
>        QString("radioButton"));
>
>    connect(ui_pushButton,SIGNAL(clicked()),
>            this,SLOT(on_pushButton_Clicked()));
>    connect(ui_radioButton,SIGNAL(clicked()),
>            this,SLOT(on_radioButton_Clicked()));
>}
>
>  
>
>>If I do a dumpObjectTree a myWidget inside makeConnection, I can see 
>>that there is a push button and a radiobutton. But findChild or 
>>qFindChild are unable to find them.
>>    
>>
>
>OK. I've reconstructed a similar plugin and tested it successfully with
>both Qt 4.1.4 and 4.2.0. I can send you the code off-list if you want,
>but you may find that the problem is either with your abstract interface
>class definition or with the plugin class definition.
>
>Here's my interface class definition:
>
>class Interface
>{
>public:
>    virtual ~Interface() {}
>    virtual void makeConnections(QWidget * myWidget) = 0;
>};
>
>Q_DECLARE_INTERFACE(Interface,
>                    "com.trolltech.internal.Interface/1.0")
>
>
>And here's the plugin class definition:
>
>class Plugin : public QObject, public Interface
>{
>    Q_OBJECT
>    Q_INTERFACES(Interface)
>
>public:
>    void makeConnections(QWidget * myWidget);
>
>public slots:
>    void on_pushButton_Clicked();
>    void on_radioButton_Clicked();
>};
>
>
>Of course, I also had to include the following line with my plugin's
>implementation:
>
>Q_EXPORT_PLUGIN2(plugin, Plugin)
>
>Is any of this substantially different to what you've done?
>
>David
>  
>

-- 
 [ signature omitted ] 

Message 14 in thread

I just checked my version of Qt 4.1 is 4.1.0.

David Boddie wrote:

>[For completeness, I'm sending this to qt-interest as well as to the
> intended recipient. Sorry if you get this twice, Elise.]
>
>On Wednesday 13 December 2006 16:08, Elise Taillant wrote:
>  
>
>>As I said in my previous answer, It seems to occur only with 4.1 and not 
>>4.2. I'm working on linux.
>>I'm going to try your ideas even if it works now with Qt4.2.
>>
>>Here my more complete code :
>>
>>//function loading a plug in
>>    
>>
>
>[...]
>
>I don't think the problem is in your plugin loading code.
>
>  
>
>>Now in my plug in
>>    
>>
>
>[Reformatting...]
>
>void PlugInTest::makeConnections(QWidget * myWidget){
>
>    ui_pushButton = myWidget->findChild<QPushButton*>(
>        QString("pushButton"));
>    ui_radioButton = myWidget->findChild<QRadioButton*>(
>        QString("radioButton"));
>
>    connect(ui_pushButton,SIGNAL(clicked()),
>            this,SLOT(on_pushButton_Clicked()));
>    connect(ui_radioButton,SIGNAL(clicked()),
>            this,SLOT(on_radioButton_Clicked()));
>}
>
>  
>
>>If I do a dumpObjectTree a myWidget inside makeConnection, I can see 
>>that there is a push button and a radiobutton. But findChild or 
>>qFindChild are unable to find them.
>>    
>>
>
>OK. I've reconstructed a similar plugin and tested it successfully with
>both Qt 4.1.4 and 4.2.0. I can send you the code off-list if you want,
>but you may find that the problem is either with your abstract interface
>class definition or with the plugin class definition.
>
>Here's my interface class definition:
>
>class Interface
>{
>public:
>    virtual ~Interface() {}
>    virtual void makeConnections(QWidget * myWidget) = 0;
>};
>
>Q_DECLARE_INTERFACE(Interface,
>                    "com.trolltech.internal.Interface/1.0")
>
>
>And here's the plugin class definition:
>
>class Plugin : public QObject, public Interface
>{
>    Q_OBJECT
>    Q_INTERFACES(Interface)
>
>public:
>    void makeConnections(QWidget * myWidget);
>
>public slots:
>    void on_pushButton_Clicked();
>    void on_radioButton_Clicked();
>};
>
>
>Of course, I also had to include the following line with my plugin's
>implementation:
>
>Q_EXPORT_PLUGIN2(plugin, Plugin)
>
>Is any of this substantially different to what you've done?
>
>David
>  
>

-- 
 [ signature omitted ] 

Message 15 in thread

I tried to recompile my app and my plugin with Qt 4.1.0 as I did 
something like 10 times yesterday with no results, and now everything 
works fine, plugin loaded, objects found and connections ok.
I don't understand i did not change anything... :'(
May such a problem comes from the compiler?
Thanks for your help
I'll let you know it is happens again..

Elise




Elise Taillant wrote:

> I just checked my version of Qt 4.1 is 4.1.0.
>
> David Boddie wrote:
>
>> [For completeness, I'm sending this to qt-interest as well as to the
>> intended recipient. Sorry if you get this twice, Elise.]
>>
>> On Wednesday 13 December 2006 16:08, Elise Taillant wrote:
>>  
>>
>>> As I said in my previous answer, It seems to occur only with 4.1 and 
>>> not 4.2. I'm working on linux.
>>> I'm going to try your ideas even if it works now with Qt4.2.
>>>
>>> Here my more complete code :
>>>
>>> //function loading a plug in
>>>   
>>
>>
>> [...]
>>
>> I don't think the problem is in your plugin loading code.
>>
>>  
>>
>>> Now in my plug in
>>>   
>>
>>
>> [Reformatting...]
>>
>> void PlugInTest::makeConnections(QWidget * myWidget){
>>
>>    ui_pushButton = myWidget->findChild<QPushButton*>(
>>        QString("pushButton"));
>>    ui_radioButton = myWidget->findChild<QRadioButton*>(
>>        QString("radioButton"));
>>
>>    connect(ui_pushButton,SIGNAL(clicked()),
>>            this,SLOT(on_pushButton_Clicked()));
>>    connect(ui_radioButton,SIGNAL(clicked()),
>>            this,SLOT(on_radioButton_Clicked()));
>> }
>>
>>  
>>
>>> If I do a dumpObjectTree a myWidget inside makeConnection, I can see 
>>> that there is a push button and a radiobutton. But findChild or 
>>> qFindChild are unable to find them.
>>>   
>>
>>
>> OK. I've reconstructed a similar plugin and tested it successfully with
>> both Qt 4.1.4 and 4.2.0. I can send you the code off-list if you want,
>> but you may find that the problem is either with your abstract interface
>> class definition or with the plugin class definition.
>>
>> Here's my interface class definition:
>>
>> class Interface
>> {
>> public:
>>    virtual ~Interface() {}
>>    virtual void makeConnections(QWidget * myWidget) = 0;
>> };
>>
>> Q_DECLARE_INTERFACE(Interface,
>>                    "com.trolltech.internal.Interface/1.0")
>>
>>
>> And here's the plugin class definition:
>>
>> class Plugin : public QObject, public Interface
>> {
>>    Q_OBJECT
>>    Q_INTERFACES(Interface)
>>
>> public:
>>    void makeConnections(QWidget * myWidget);
>>
>> public slots:
>>    void on_pushButton_Clicked();
>>    void on_radioButton_Clicked();
>> };
>>
>>
>> Of course, I also had to include the following line with my plugin's
>> implementation:
>>
>> Q_EXPORT_PLUGIN2(plugin, Plugin)
>>
>> Is any of this substantially different to what you've done?
>>
>> David
>>  
>>
>

-- 
 [ signature omitted ]