Results 1 to 8 of 8

Thread: signal acrorss different QStacked Widgets

  1. #1
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default signal acrorss different QStacked Widgets

    I have a GUI with multiple tabs, where each tab contains widgets defined in their own class and .ui.

    I am trying to implement "setCurrentIndex()" of the tabWidget when the user clicks on a pushbutton that could be in any of the tab pages.

    Doing it the way below works, but it does not update the tab header/title that is active. For example, if I have a tab widget with page titles: tab0, tab1, tab2, and I change from tab0 to tab2 with setCurrentIndex(2). It will show the right content of tab2, but the tabWidget header still shows tab0 Any ideas why? or if there is a better implementation. Thanks

    QObject *obj = new QStackedWidge;
    foreach( QObject *obj, this->parent()->parent()->children())
    {
    if(obj->objectName()=="qt_tabwidget_stackedwidget")
    {
    QStackedWidget *tab = static_cast<QStackedWidget*>(obj);
    tab->setCurrentIndex(2);
    }
    }

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,476
    Thanks
    252
    Thanked 708 Times in 698 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: signal acrorss different QStacked Widgets

    If you want tabs, why are you using QStackedWidget and not QTabWidget?

  3. #3
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: signal acrorss different QStacked Widgets

    Because I cannot find any widget defined as tabwidget using children(), even though the tab was defined originally as a QTabWidget.

    The program compiles when I use QTabWidget rather than QStackedWidget; but it crashes without any error messages at run time.

    Is there a better way to do? Thanks.

  4. #4
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: signal acrorss different QStacked Widgets

    If you're creating the QTabWidget and QPushButtons yourself (regardless if you're using Qt Designer or directly by code) then you can subclass them.
    By subclassing the QTabWidget, you can add a slot to it that receives the 'clicked' signals from the buttons.
    By subclassing the QPushButtons, you can add a member property that indicates the tab index that you should switch to.

    The QTabWidget knows the widgets that you're adding as pages to it (or you can retrieve them with widget(index) if they are already added), so its new slot can easily be connected to each of the signals from the buttons since you have visibility of them.
    The slot could be something like this:

    Qt Code:
    1. void MyTabWidget::onButtonClick()
    2. {
    3. MyPushButton* button = qobject_cast< MyPushButton* >( sender() );
    4. setCurrentIndex( button.getDesiredIndex() );
    5. }
    To copy to clipboard, switch view to plain text mode 

    If you're using Qt Designer, you'll need promote the QTabWidget and the QPushButtons to their corresponding subclasses.
    EDIT: Forgot the scope operator, and changed from static_cast to qobject_cast as the below suggestion.
    Last edited by Kryzon; 26th February 2015 at 00:23.

  5. The following user says thank you to Kryzon for this useful post:

    hass26 (26th February 2015)

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,476
    Thanks
    252
    Thanked 708 Times in 698 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: signal acrorss different QStacked Widgets

    Because I cannot find any widget defined as tabwidget using children(), even though the tab was defined originally as a QTabWidget.
    What are you calling children() on? The tab widget itself? If so, of course you won't see any children of type QTabWidget.

    And static_cast<> is absolutely the wrong thing to do. A static cast says, "I don't care what this object really is, treat it as though it was an object of the type I am casting it to", so of course you can cast any pointer to QStackedWidget or anything else, but that doesn't make it so. No wonder your program crashes.

    If you are trying to cast a Qt object instance, you use qobject_cast< QTabWidget * >() and you check that the pointer that is returned is non-NULL before trying to use it.

  7. The following user says thank you to d_stranz for this useful post:

    hass26 (26th February 2015)

  8. #6
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: signal acrorss different QStacked Widgets

    Ok. I found a workaround solution, maybe not the most elegant but It works.

    Qt Code:
    1. QObject *obj = parent->findChild<QTabWidget*>("pagesTabWidget");
    2. QTabWidget *tab = qobject_cast<QTabWidget*>(obj);
    3. tab->setCurrentIndex(1);
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,476
    Thanks
    252
    Thanked 708 Times in 698 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: signal acrorss different QStacked Widgets

    I found a workaround solution, maybe not the most elegant but It works.
    No, it's guaranteed to crash if either the "obj" or "tab" pointers are NULL.

    Why is it so hard for people to understand that the way you avoid segfaults is to check pointers for NULL before using them?

  10. #8
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: signal acrorss different QStacked Widgets

    Yes I did. Thanks.

    Quote Originally Posted by d_stranz View Post
    No, it's guaranteed to crash if either the "obj" or "tab" pointers are NULL.

    Why is it so hard for people to understand that the way you avoid segfaults is to check pointers for NULL before using them?

Similar Threads

  1. Replies: 0
    Last Post: 30th May 2013, 21:59
  2. Replies: 0
    Last Post: 30th April 2012, 15:17
  3. Replies: 2
    Last Post: 12th February 2009, 22:23
  4. Replies: 6
    Last Post: 3rd September 2008, 14:27
  5. QStacked Widget.
    By csvivek in forum Qt Tools
    Replies: 1
    Last Post: 21st May 2008, 13:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.