QStackwidget Multiple pages Issue[SOLVED]



  • I have added 7 pages into a stackwidget. However, it always crashes when it gets to the last page (The program closes). It works fine with 6 pages and I read that I can have as many pages as I would like. I also tested if the last page was giving me problems but I tested it by commenting out _page5 and it works.

    _stackedWidget = new QStackedWidget;
    _stackedWidget->addWidget(_page1);
    _stackedWidget->addWidget(_page2);
    _stackedWidget->addWidget(_page3);
    _stackedWidget->addWidget(_page4);
    _stackedWidget->addWidget(_page5);
    _stackedWidget->addWidget(_page6);
    _stackedWidget->addWidget(_page7);
    

    Page7 gets called through the signal and slots function by:

    void MainWindow::goToPage7()
    {
        _stackedWidget->setCurrentWidget(_page7);
    }
    

    I have Qt Creator 3.0.1 Based on Qt 5.2.1 (GCC 4.8.2, 64 bit)
    I was wondering if it is a bug and if so to what version should I upgrade it to.


  • Lifetime Qt Champion

    Hi,

    Are you sure _page7 is properly allocated ?

    What does a run through the debugger tell you ?



  • @SGaist It tells me it ended unexpectedly
    I am sure it is because when I comment out one of the _stackedWidget->addWidget(_page5); it works. It is just when I add more than 6 pages that it crashes.


  • Qt Champions 2017

    Hi
    For test.
    If you do it in Designer in a new fresh project.
    Just Add QStackedWidget to Mainwindow.ui
    and right click it to add pages.
    Does it crash when more than 7 there also?



  • @mrjj That was my first option when I was creating the stackwidget. However, I wanted to go from one page to another and when I clicked the button nothing would happen but when I wrote it in code it worked fine.


  • Qt Champions 2017

    @marlenet15
    Ok, that was odd as a stacked widget on UI and
    button where you call void setCurrentIndex(int index)

    should work just fine.

    But the important part.
    Did it crash when adding more that 7 pages that way?



  • @mrjj yes it does.


  • Qt Champions 2017

    @marlenet15
    Ok !?!
    it crashes if you - in a blank new project,
    just right click and add more than 7 pages?



  • @mrjj Oh no. I meant it crashes if I used void setCurrentIndex(int index) instead of setCurrentWidget(_page7);
    It wouldn't crash but it wouldn't allow me to go to the next page since clicking the button wouldn't do anything.


  • Qt Champions 2017

    @marlenet15
    ok.so you had a bug. i guess.
    try this sample
    https://www.dropbox.com/s/xuhzip7p52g4guk/sevenstacked.zip?dl=0
    it can go back and forth.



  • @mrjj I tried running it but nothing happens. The option to build is grayed out.
    and also all the pages that I am adding onto the stackwidget are classes I created so I am not sure it that also created an issue when I tried doing it through .ui


  • Qt Champions 2017

    @marlenet15

    oh, go to Projects (left side icons) and adjust build path to
    somewhere that exists or set it if none.

    Well the stacked controls QWidgets so its ok its your own I think.



  • @mrjj It tells me that 'File 'sevenstacked.pro' has modification time 2.9e+0.4 s in the future' and it just hangs


  • Qt Champions 2017

    @marlenet15
    OK?
    Pretty strange.

    just delete it and make a new .pro file and put this in it
    just a default one.

    #-------------------------------------------------
    #
    # Project created by QtCreator 2015-10-18T21:53:24
    #
    #-------------------------------------------------
    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = sevenstacked
    TEMPLATE = app
    
    
    SOURCES += main.cpp\
            mainwindow.cpp
    
    HEADERS  += mainwindow.h
    
    FORMS    += mainwindow.ui
    


  • @mrjj haha it gave me like 27 errors


  • Qt Champions 2017

    @marlenet15
    Ehh. ok.
    Not sure what is going on.

    Make new default project and then copy mainwindow.cpp/h and mainwindow.ui
    to it ?

    Anyway, was just a stacked widget with 10 pages and 2 buttons to go forth and back between the pages.

    void MainWindow::on_pushButton_11_released() {
    
      int cur = ui->stackedWidget->currentIndex() + 1;
      ui->stackedWidget->setCurrentIndex(cur);
      ui->label->setText( QString::number(cur) );
    
    }
    
    void MainWindow::on_pushButton_12_released() {
      int cur = ui->stackedWidget->currentIndex() - 1;
      ui->stackedWidget->setCurrentIndex(cur);
      ui->label->setText( QString::number(cur) );
    
    }
    


  • @mrjj Ok so yes it does work that way. Do you have any idea why wouldn't work through the code I wrote?


  • Qt Champions 2017

    @marlenet15
    :) super
    Well, I cant see how you create _page1 - _page7
    you must have XX * _page1 = new XX; somewhere.
    and it might crash if you use setCurrentWidget for deallocated
    object or something like that.

    Also using setCurrentWidget, that widget must already be inserted into stacked.

    Most of the time I just insert pages in UI as it is far easier.
    You can even add dynamic also, if needed.



  • @mrjj wouldn't it be page7 that could be deallocated? I have been looking at my code like 50 times and I still can't find the error. I set the pages on the UI and page7 still doesn't work :(



  • @marlenet15 Ok So I commented out 90% of the code and page7 works! So now I have to uncomment section by section to figure out what is making page 7 crash.


  • Qt Champions 2017

    @marlenet15

    well its hard to guess when I cant see all code.

    Do you do

    QWidget *page7 = new QWidget()
    

    when you create them ?
    or now you insert them into the stacked in the UI ?



  • @mrjj I inserted it now on the UI. I figured it out! I had another class where I had an array of QStrings which contains the titles for each page. I had an array of size 6 because I had only focused on the 6 pages I created. However, when I created the 7th page it would crash because the index 6 didn't exist since the array goes from 0-5. Thank you so much for your help!!!


  • Qt Champions 2017

    @marlenet15
    oh good found :)
    Nasty one.


Log in to reply
 

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