How to get widget from other widget , when findChild dosnt work , using promoted class



  • hi
    i have some simple hierarchy of widgets
    looks something like this :
    @ MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    MainWindow->resize(423, 479);
    MainWindow->setLayoutDirection(Qt::LeftToRight);
    MainWindow->setLocale(QLocale(QLocale::English, QLocale::UnitedStates));
    centralwidget = new QWidget(MainWindow);
    centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    horizontalLayout = new QHBoxLayout(centralwidget);
    horizontalLayout->setSpacing(0);
    horizontalLayout->setContentsMargins(0, 0, 0, 0);
    horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
    StreamViewWidget = new StreamView(centralwidget);
    StreamViewWidget->setObjectName(QString::fromUtf8("StreamViewWidget"));
    horizontalLayout_2 = new QHBoxLayout(StreamViewWidget);
    horizontalLayout_2->setSpacing(0);
    horizontalLayout_2->setContentsMargins(0, 0, 0, 0);
    horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
    streamList = new StreamList(StreamViewWidget);
    streamList->setObjectName(QString::fromUtf8("streamList"));
    streamList->setStyleSheet(QString::fromUtf8("background-color: rgb(171, 251, 255);"));

        horizontalLayout_2->addWidget(streamList);
    
    
        horizontalLayout->addWidget(StreamViewWidget);
    
        MainWindow->setCentralWidget(centralwidget);
        menubar = new QMenuBar(MainWindow);
        menubar->setObjectName(QString::fromUtf8("menubar"));
        menubar->setGeometry(QRect(0, 0, 423, 22));
        MainWindow->setMenuBar(menubar);
        statusbar = new QStatusBar(MainWindow);
        statusbar->setObjectName(QString::fromUtf8("statusbar"));
        MainWindow->setStatusBar(statusbar);
    

    @

    now StreamViewWidget and streamList; are promoted , and i have there class
    now from StreamViewWidget i want get access to streamList.
    so i try to do this but it fails and im getting empty result .

    @StreamView::StreamView(QWidget parent) :QWidget(parent)
    {
    pStreamList = this->findChild<StreamList
    >("streamList");
    QList<StreamList *> widgets = this->findChildren<StreamList *>("streamList");

    }@

    also when i do in StreamView QObject::dumpObjectTree()
    im getting : so i guess its not a child of StreamView , but how can i get the streamList?
    QWidget::centralwidget
    QHBoxLayout::horizontalLayout
    StreamView::

    any idea what im doing wrong here ?
    Thanks



  • At the first step you should check for Why "findChild" doesn't work? Maybe the reason is conflicts between names on your object creation and where you're finding that object name.
    @streamList->setObjectName(QString::fromUtf8("streamList"));@
    then
    @pStreamList = this->findChild<StreamList*>("streamList");@ shouldn't be like this? @pStreamList = this->findChild<StreamList*>(QString::fromUtf8("streamList"));@

    and other solution is passing the object's pointer to the other widget and you'll have direct access to your widget.

    However using same names for object name and the class name would be a problem itself!



  • Class name and object name can be the same, it does not matter.

    You can see, whether the streamList is in the child list at all:

    @
    foreach(const QObject *obj, this->children()) {
    qDebug() << obj;
    }
    @

    Check if the streamList is in the debug output.


Log in to reply
 

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