What am I doing wrong with accessing children?



  • QString Qpassword = centralWidget()->findChild<QVBoxLayout*>("lay")->findChild<QTabWidget*>("tabWidget")->findChild<QVBoxLayout*>("encryptLayout")->findChild<QLineEdit*>("pwEdit")->text();

    centralWidget()->findChild<QVBoxLayout*>("lay")->findChild<QLineEdit*>("trace")->setText(Qpassword);

    I have a MainWindow with a centralWidget. That widget has a QVBoxLayout, which has a QTabWidget, which has a QVBoxLayout, which has a QLineEdit widget - so the first line of code there goes through all of those to attempt to access text() of the line edit. Then I try to paste it back into another line edit with the second line of code to see if I'm successfully doing anything correctly.

    Doesn't seem to be working though. Any ideas? Someday I will be good enough not to have to bother you pros with silly questions :(



  • @Darkware well first things first : to get a layout don't use the findChild method, use the layout method : http://doc.qt.io/qt-5.5/qwidget.html#layout. Indeed if I'm not wrong, the layout is not a child of the widget. So that reduces your instruction like so :

    QString Qpassword = centralWidget()->findChild<QTabWidget*>("tabWidget")->findChild<QLineEdit*>("pwEdit")->text();
    
    centralWidget()->findChild<QLineEdit*>("trace")->setText(Qpassword);
    

    But really i find it weird that you have to "browse" your ui family tree to find your widgets. DIdn't you store them in members of your class?



  • Well, the problem is that pwEdit was created in my EncryptionTab class and I am trying to snag its text from a function of another class. Let me break down the hierarchy so that's clearer:

    in main()
    mainWinClass main_win;

    .

    in mainWinClass::mainWinClass()
    setCentralWidget(new mainCont());

    .

    in mainCont::mainCont()
    QVBoxLayout *lay = new QVBoxLayout();
    trace = new QLineEdit("trace");
    lay->addWidget(trace);
    ...and...
    tabWidget = new QTabWidget;
    tabWidget->addTab(new EncryptionTab(), tr("Encryption"));
    lay->addWidget(tabWidget);

    .

    in EncryptionTab::EncryptionTab(QWidget parent)
    QVBoxLayout encryptLayout = new QVBoxLayout;

    QLineEdit pwEdit = new QLineEdit("");
    encryptLayout->addWidget(pwEdit);

    .

    in mainWinClass::encrypt()
    This is where I am trying to grab the password from pwEdit. encrypt() is of the mainWinClass class, while pwEdit is from the EncryptionTab class, so going pwEdit->text() the compiler isn't going to recognize pwEdit because it was defined in EncryptionTab, not mainWinClass.

    I am trying to use the QEditLine "trace" from mainCont::mainCont() as a way to see if I'm successfully grabbing the password or not because I don't know any other way to print stuff out to the screen. Once again, I can't just go trace->setText() because trace is defined in mainCont, not mainWinClass.

    I've tried what you suggested, but still haven't had much luck yet unfortunately.



  • @Darkware oh well that's easy:

    class mainCount{
        private QLineEdit * trace;
        public void setTraceText(QString text){trace->setText(text);}
    ....
    }
    class EncryptionTab{
        private QLineEdit * pwEdit;
        public QString getPassword(){return pwEdit->getText();}
    }
    

    There you go! I don't know where you EncryptTab instance is declared though but this should help you.



  • When I try to use the function like:
    QString Qpassword = EncryptionTab::getPassword();
    it throws the error "illegal call of non-static member function"

    Is it throwing this error because I need to call it like this?
    myEncryptionInstance->getPassword();
    since it needs to know what instance's pwEdit it needs to retrieve?

    If so, I don't have an instance of it called "myEncryptionInstance". If you look at the code I posted before under mainCont(), it's created like this:
    tabWidget->addTab(new EncryptionTab(), tr("Encryption"));
    with no name associated with it.

    Thanks for the help so far. I'm sorry I'm so retarded. I plan on picking up a C++ book the next time I go out because classes are a severe weak point of mine. I've gone from churning out code to being stuck on class problems for like 4 days now.


  • Lifetime Qt Champion

    Hi,

    Add a class member to keep the pointer to the instance of EncryptionTab.

    i.e.

    _encryptionTab = new EncryptionTab;
    tabWidget->add(_encryptionTab, tr("Encryption"));
    

    Take the time to take a look at Qt's documentation, it's full of code samples, examples and tutorials to help you get started (but doesn't replace learning C++)



  • Just wanted to sneak back in here and thank you for the help.

    I got everything working finally!

    http://imgur.com/6icnr6z

    Everything works in it. Encrypts and decrypts the file. Wooo!

    I still have to resize some things to make it look pretty, but I'm on my way.


  • Lifetime Qt Champion

    Are you using layouts ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.