Accessing widgets in a QTabWidget



  • Hey there,

    I have a class called mainView and two other classes called browseTab and appTab respectively. mainView contains a QTabWidget that holds an instance of either browseTab or appTab. Both browseTab and appTab contain a QWebView that I need to access. I need to be able to determine which widget (browseTab or appTab) is in the currently active tab, then I need to access the QWebView.
    I have tried something like this to no avail:
    @
    if(this->tabWidget->currentWidget() = browseTab)
    {
    QString url = "http://www.google.com/";
    browseTab.webView->load(url);
    }
    @

    It won't let me do this because browseTab does not refer to a value.
    Any ideas on how I can access these classes inside the QTabWidget?
    Thanks!



  • There should be two '=' signs in the if() of line 1 or you have an attempt at assignment that will fail.

    The attempt to access the webView member of the browseTab object will only succeed if it is a public member variable (which would not normally be the case).



  • it won't compile and says browseTab does not refer to a value. The webView is a public member, the problem lies in trying to access the browseTab or appTab.


  • Moderators

    What is the type of "browseTab"? Is it a widget, or a widget pointer?



  • widget. same with appTab.


  • Moderators

    That means you are trying to compare a value of widget type to a value of widget pointer type:
    [quote]
    @if(this->tabWidget->currentWidget() = browseTab)@
    [/quote]Basic rules of C++ comparisons:

    • Comparison is done using "==".
    • The values on the left and the right must be the same type.


  • So currentWidget() is a widget pointer? How do I get the widget type inside the tabWidget?


  • Moderators

    [quote author="nicky j" date="1396838942"]So currentWidget() is a widget pointer?[/quote]This should answer your question: http://qt-project.org/doc/qt-5/qtabwidget.html#currentWidget
    [quote]How do I get the widget type inside the tabWidget?[/quote]I would do it the other way round -- get a pointer to your browseTab object, and use that in the comparison. Pointer values are integers after all, and it's straightforward to compare integers.

    Do you know how to use the ampersand (&) operator in C++?



  • In few of my implementations I have a similar problem, I know what my tabs are include and just do a dynamic_cast for checking the widget type, e.g.

    @
    TcHmiFileTabWidget *TcHmiEditorView::fileTabWidget(int index)
    {
    QTabWidget *p = ui->tabFiles;

    if(index < 0 || index > p->count()) {
        return NULL;
    }
    
    QWidget *w = p->widget(index);
    if(w == NULL) { return NULL; }
    
    __TcHmiFileTabWidgetDummy *pdummy = dynamic_cast<__TcHmiFileTabWidgetDummy*>(w);
    if(pdummy == NULL) { return NULL; }
    
    return pdummy->pFileTabWidget; 
    

    }
    @


  • Moderators

    [quote author="cbries" date="1396854817"]In few of my implementations I have a similar problem, I know what my tabs are include and just do a dynamic_cast for checking the widget type[/quote]For QObjects (including QWidgets), I recommend qobject_cast over dynamic_cast. There are cases where dynamic_cast might fail but qobject_cast works. See http://qt-project.org/doc/qt-5/qobject.html#qobject_cast



  • No I don't really know how to use the ampersand


  • Moderators

    [quote author="nicky j" date="1397690719"]No I don't really know how to use the ampersand[/quote]It is a core part of C++. Qt makes extensive use of pointers and their related operators (including the '&' operator). Please take time to learn them: http://www.cplusplus.com/doc/tutorial/pointers/ -- it will make your life much easier.


Log in to reply
 

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