Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStackwidget Multiple pages Issue[SOLVED]
Forum Updated to NodeBB v4.3 + New Features

QStackwidget Multiple pages Issue[SOLVED]

Scheduled Pinned Locked Moved General and Desktop
23 Posts 3 Posters 6.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marlenet15
    wrote on last edited by marlenet15
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you sure _page7 is properly allocated ?

      What does a run through the debugger tell you ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Are you sure _page7 is properly allocated ?

        What does a run through the debugger tell you ?

        M Offline
        M Offline
        marlenet15
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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?

          M 1 Reply Last reply
          0
          • mrjjM mrjj

            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?

            M Offline
            M Offline
            marlenet15
            wrote on last edited by
            #5

            @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.

            mrjjM 1 Reply Last reply
            0
            • M marlenet15

              @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.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @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?

              M 1 Reply Last reply
              0
              • mrjjM mrjj

                @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?

                M Offline
                M Offline
                marlenet15
                wrote on last edited by
                #7

                @mrjj yes it does.

                mrjjM 1 Reply Last reply
                0
                • M marlenet15

                  @mrjj yes it does.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  M 1 Reply Last reply
                  0
                  • mrjjM mrjj

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

                    M Offline
                    M Offline
                    marlenet15
                    wrote on last edited by
                    #9

                    @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.

                    mrjjM 1 Reply Last reply
                    0
                    • M marlenet15

                      @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.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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.

                      M 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @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.

                        M Offline
                        M Offline
                        marlenet15
                        wrote on last edited by marlenet15
                        #11

                        @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

                        mrjjM 1 Reply Last reply
                        0
                        • M marlenet15

                          @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

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #12

                          @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.

                          M 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @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.

                            M Offline
                            M Offline
                            marlenet15
                            wrote on last edited by
                            #13

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

                            mrjjM 1 Reply Last reply
                            0
                            • M marlenet15

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

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @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
                              
                              M 1 Reply Last reply
                              0
                              • mrjjM mrjj

                                @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
                                
                                M Offline
                                M Offline
                                marlenet15
                                wrote on last edited by
                                #15

                                @mrjj haha it gave me like 27 errors

                                mrjjM 1 Reply Last reply
                                0
                                • M marlenet15

                                  @mrjj haha it gave me like 27 errors

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @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) );
                                  
                                  }
                                  
                                  M 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @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) );
                                    
                                    }
                                    
                                    M Offline
                                    M Offline
                                    marlenet15
                                    wrote on last edited by
                                    #17

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

                                    mrjjM 1 Reply Last reply
                                    0
                                    • M marlenet15

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

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by mrjj
                                      #18

                                      @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.

                                      M 1 Reply Last reply
                                      0
                                      • mrjjM mrjj

                                        @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.

                                        M Offline
                                        M Offline
                                        marlenet15
                                        wrote on last edited by
                                        #19

                                        @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 :(

                                        M 1 Reply Last reply
                                        0
                                        • M marlenet15

                                          @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 :(

                                          M Offline
                                          M Offline
                                          marlenet15
                                          wrote on last edited by
                                          #20

                                          @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.

                                          mrjjM 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved