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.
  • 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
                                      • M marlenet15

                                        @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 Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by mrjj
                                        #21

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

                                        M 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

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

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

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

                                          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