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. QT crashed when I try to do onglets->setCurrentIndex(0);
Forum Updated to NodeBB v4.3 + New Features

QT crashed when I try to do onglets->setCurrentIndex(0);

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 3.4k Views 1 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome
    Since its not part of UI
    Are you sure you didnt create a local variable by accident ?
    Like
    QTabWidget *onglets; // in .h
    BUT where you create it
    QTabWidget *onglets = new QTabWidget ()
    and not
    onglets = new QTabWidget ();

    ?

    1 Reply Last reply
    2
    • F Offline
      F Offline
      f.lerdino
      wrote on last edited by
      #3

      I have just put this in the public: in the file .h
      QWidget *fenetre;
      QWidget *page1;
      QWidget *page2;
      QWidget *page3;
      QTabWidget *onglets;

      is this the problem?

      mrjjM JonBJ 2 Replies Last reply
      0
      • F f.lerdino

        I have just put this in the public: in the file .h
        QWidget *fenetre;
        QWidget *page1;
        QWidget *page2;
        QWidget *page3;
        QTabWidget *onglets;

        is this the problem?

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

        @f.lerdino

        Nope, but they must be new'ed BEFORE you use them.

        like
        page1 = new QWidget(this);

        1 Reply Last reply
        2
        • F f.lerdino

          I have just put this in the public: in the file .h
          QWidget *fenetre;
          QWidget *page1;
          QWidget *page2;
          QWidget *page3;
          QTabWidget *onglets;

          is this the problem?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #5

          @f.lerdino
          Show us the code you have on onglets before you ever get to MainWindow::playaction() ?

          1 Reply Last reply
          0
          • F Offline
            F Offline
            f.lerdino
            wrote on last edited by
            #6

            MainWindow::MainWindow()
            {
            createActions();
            createMenus();
            createToolbars();
            createVertbars();

            QVBoxLayout *layout = new QVBoxLayout;
            QWidget *widget = new QWidget;
            widget->setLayout(layout);
            widget->setStyleSheet("padding: 10px;" "border-style: solid;" "border-width: 5px;" "border-radius: 4px;");
            setCentralWidget(widget);
            
            /****************************/
            QWidget fenetre;
            // 1 : Créer le QTabWidget
            QTabWidget *onglets = new QTabWidget(&fenetre);
            onglets->setGeometry(30, 20, 240, 160);
            
            // 2 : Créer les pages, en utilisant un widget parent pour contenir chacune des pages
            QWidget *page1 = new QWidget;
            QWidget *page2 = new QWidget;
            QLabel *page3 = new QLabel; // Comme un QLabel est aussi un QWidget (il en hérite), on peut aussi s'en servir de page
            
            // 3 : Créer le contenu des pages de widgets
            
                // Page 1
                /*QLineEdit *lineEdit = new QLineEdit("Entrez votre nom");
                QPushButton *bouton1 = new QPushButton("Cliquez ici");
                QPushButton *bouton2 = new QPushButton("Ou là…");*/
                QVBoxLayout *vbox1 = new QVBoxLayout;
                /*vbox1->addWidget(lineEdit);
                vbox1->addWidget(bouton1);
                vbox1->addWidget(bouton2);*/
                /* View pie chart */
                QPieSeries *series1 = new QPieSeries();
                    series1->setName("Fossil fuels");
                    series1->append("Oil", 353295);
                    series1->append("Coal", 188500);
                    series1->append("Natural gas", 148680);
                    series1->append("Peat", 94545);
            
                    QPieSeries *series2 = new QPieSeries();
                    series2->setName("Renewables");
                    series2->append("Wood fuels", 319663);
                    series2->append("Hydro power", 45875);
                    series2->append("Wind power", 1060);
            
                    QPieSeries *series3 = new QPieSeries();
                    series3->setName("Others");
                    series3->append("Nuclear energy", 238789);
                    series3->append("Import energy", 37802);
                    series3->append("Other", 32441);
            
                    DonutBreakdownChart *donutBreakdown = new DonutBreakdownChart();
                    donutBreakdown->setAnimationOptions(QChart::AllAnimations);
                    donutBreakdown->setTitle("Total consumption of energy in Finland 2010");
                    donutBreakdown->legend()->setAlignment(Qt::AlignRight);
                    donutBreakdown->addBreakdownSeries(series1, Qt::red);
                    donutBreakdown->addBreakdownSeries(series2, Qt::darkGreen);
                    donutBreakdown->addBreakdownSeries(series3, Qt::darkBlue);
            
                    QChartView *chartView = new QChartView(donutBreakdown);
                    chartView->setRenderHint(QPainter::Antialiasing);
            
                    vbox1->addWidget(chartView);
                /**/
                page1->setLayout(vbox1);
            
                 // Page 2
                QProgressBar *progress = new QProgressBar;
                progress->setValue(50);
                QSlider *slider = new QSlider(Qt::Horizontal);
                QPushButton *bouton3 = new QPushButton("Valider");
                QVBoxLayout *vbox2 = new QVBoxLayout;
                /*vbox2->addWidget(progress);
                vbox2->addWidget(slider);
                vbox2->addWidget(bouton3);*/
                page2->setLayout(vbox2);
            
                // Page 3 (je ne vais afficher qu'une image ici, pas besoin de layout)
                page3->setPixmap( QPixmap (":/images/qt5.png") );
                page3->setAlignment(Qt::AlignCenter);
            
                // 4 : ajouter les onglets au QTabWidget, en indiquant la page qu'ils contiennent
                onglets->addTab(page1, "Graphe");
                onglets->addTab(page2, "Stats");
                onglets->addTab(page3, "Info-app");
            
                /***************************/
                onglets->setLayout(layout);
                setCentralWidget(onglets);
            
            setWindowTitle(tr("TEST"));
            setUnifiedTitleAndToolBarOnMac(true);
            
            onglets->setCurrentIndex(0);
            

            }

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

              So you do
              QTabWidget *onglets = new QTabWidget(&fenetre);
              (this is local variable)

              Do you also have one in . h file ?
              one named onglets

              You are newing a new copy. not the one from .h file
              so later u use a invalid pointer as its not newed. ( i guess)

              can you please also show .h file ?

              1 Reply Last reply
              1
              • F Offline
                F Offline
                f.lerdino
                wrote on last edited by
                #8

                #ifndef MAINWINDOW_H
                #define MAINWINDOW_H

                #include <QMainWindow>

                class DiagramScene;

                QT_BEGIN_NAMESPACE
                class QAction;
                class QToolBox;
                class QSpinBox;
                class QComboBox;
                class QFontComboBox;
                class QButtonGroup;
                class QLineEdit;
                class QGraphicsTextItem;
                class QFont;
                class QToolButton;
                class QAbstractButton;
                class QGraphicsView;
                class QTabWidget;
                QT_END_NAMESPACE

                class MainWindow : public QMainWindow
                {
                Q_OBJECT

                public:
                MainWindow();

                public slots:
                void playaction();

                private slots:
                void sendToBack();
                void about();
                void setVariables(const QString &newValue);

                private:
                void createActions();
                void createMenus();
                void createToolbars();
                void createVertbars();

                /*QGraphicsView *view;*/
                
                QAction *exitAction;
                QAction *addAction;
                
                QAction *toFrontAction;
                QAction *sendBackAction;
                QAction *aboutAction;
                
                QAction *toPlayAction;
                
                QMenu *fileMenu;
                QMenu *itemMenu;
                QMenu *aboutMenu;
                

                QToolBar *textToolBar;
                QToolBar *editToolBar;
                QToolBar *pointerToolbar;

                QComboBox *sceneScaleCombo;
                QComboBox *fontSizeCombo;
                QFontComboBox *fontCombo;
                
                QToolBar *fontToolBar;
                
                QLineEdit *textbox;
                
                QWidget *fenetre;
                QWidget *page1;
                QWidget *page2;
                QWidget *page3;
                QTabWidget *onglets;
                

                };

                #endif // MAINWINDOW_H

                1 Reply Last reply
                0
                • F f.lerdino

                  MainWindow::MainWindow()
                  {
                  createActions();
                  createMenus();
                  createToolbars();
                  createVertbars();

                  QVBoxLayout *layout = new QVBoxLayout;
                  QWidget *widget = new QWidget;
                  widget->setLayout(layout);
                  widget->setStyleSheet("padding: 10px;" "border-style: solid;" "border-width: 5px;" "border-radius: 4px;");
                  setCentralWidget(widget);
                  
                  /****************************/
                  QWidget fenetre;
                  // 1 : Créer le QTabWidget
                  QTabWidget *onglets = new QTabWidget(&fenetre);
                  onglets->setGeometry(30, 20, 240, 160);
                  
                  // 2 : Créer les pages, en utilisant un widget parent pour contenir chacune des pages
                  QWidget *page1 = new QWidget;
                  QWidget *page2 = new QWidget;
                  QLabel *page3 = new QLabel; // Comme un QLabel est aussi un QWidget (il en hérite), on peut aussi s'en servir de page
                  
                  // 3 : Créer le contenu des pages de widgets
                  
                      // Page 1
                      /*QLineEdit *lineEdit = new QLineEdit("Entrez votre nom");
                      QPushButton *bouton1 = new QPushButton("Cliquez ici");
                      QPushButton *bouton2 = new QPushButton("Ou là…");*/
                      QVBoxLayout *vbox1 = new QVBoxLayout;
                      /*vbox1->addWidget(lineEdit);
                      vbox1->addWidget(bouton1);
                      vbox1->addWidget(bouton2);*/
                      /* View pie chart */
                      QPieSeries *series1 = new QPieSeries();
                          series1->setName("Fossil fuels");
                          series1->append("Oil", 353295);
                          series1->append("Coal", 188500);
                          series1->append("Natural gas", 148680);
                          series1->append("Peat", 94545);
                  
                          QPieSeries *series2 = new QPieSeries();
                          series2->setName("Renewables");
                          series2->append("Wood fuels", 319663);
                          series2->append("Hydro power", 45875);
                          series2->append("Wind power", 1060);
                  
                          QPieSeries *series3 = new QPieSeries();
                          series3->setName("Others");
                          series3->append("Nuclear energy", 238789);
                          series3->append("Import energy", 37802);
                          series3->append("Other", 32441);
                  
                          DonutBreakdownChart *donutBreakdown = new DonutBreakdownChart();
                          donutBreakdown->setAnimationOptions(QChart::AllAnimations);
                          donutBreakdown->setTitle("Total consumption of energy in Finland 2010");
                          donutBreakdown->legend()->setAlignment(Qt::AlignRight);
                          donutBreakdown->addBreakdownSeries(series1, Qt::red);
                          donutBreakdown->addBreakdownSeries(series2, Qt::darkGreen);
                          donutBreakdown->addBreakdownSeries(series3, Qt::darkBlue);
                  
                          QChartView *chartView = new QChartView(donutBreakdown);
                          chartView->setRenderHint(QPainter::Antialiasing);
                  
                          vbox1->addWidget(chartView);
                      /**/
                      page1->setLayout(vbox1);
                  
                       // Page 2
                      QProgressBar *progress = new QProgressBar;
                      progress->setValue(50);
                      QSlider *slider = new QSlider(Qt::Horizontal);
                      QPushButton *bouton3 = new QPushButton("Valider");
                      QVBoxLayout *vbox2 = new QVBoxLayout;
                      /*vbox2->addWidget(progress);
                      vbox2->addWidget(slider);
                      vbox2->addWidget(bouton3);*/
                      page2->setLayout(vbox2);
                  
                      // Page 3 (je ne vais afficher qu'une image ici, pas besoin de layout)
                      page3->setPixmap( QPixmap (":/images/qt5.png") );
                      page3->setAlignment(Qt::AlignCenter);
                  
                      // 4 : ajouter les onglets au QTabWidget, en indiquant la page qu'ils contiennent
                      onglets->addTab(page1, "Graphe");
                      onglets->addTab(page2, "Stats");
                      onglets->addTab(page3, "Info-app");
                  
                      /***************************/
                      onglets->setLayout(layout);
                      setCentralWidget(onglets);
                  
                  setWindowTitle(tr("TEST"));
                  setUnifiedTitleAndToolBarOnMac(true);
                  
                  onglets->setCurrentIndex(0);
                  

                  }

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #9

                  @f.lerdino
                  I have no idea whether this matters/causes your problem, but you seem to have:

                  setCentralWidget(widget);
                  ...
                  setCentralWidget(onglets);
                  
                  

                  Is that right?

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    f.lerdino
                    wrote on last edited by
                    #10

                    en fact i dont need the first one ... but nothing changed

                    JonBJ 1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      f.lerdino
                      wrote on last edited by
                      #11

                      Question, Is it possible to pass the *onglets as argument in the function?

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

                        Hi
                        you are doing that i ask in first post.

                        You have
                        QTabWidget *onglets; in h file but new another so that is why you crash.

                        /****************************/
                        QWidget fenetre; // this ones DIES as soon as functions ends. not good.

                        // 1 : Créer le QTabWidget
                        QTabWidget *onglets = new QTabWidget(&fenetre); THis one create a local variable,

                        It is not the one from .H ( and it cant be used outside the function)

                        So that is why you crash.

                        JonBJ 1 Reply Last reply
                        3
                        • F f.lerdino

                          en fact i dont need the first one ... but nothing changed

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #13

                          @f.lerdino
                          Yep, exactly as @mrjj has just written above!

                          1 Reply Last reply
                          0
                          • F f.lerdino

                            Question, Is it possible to pass the *onglets as argument in the function?

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @f.lerdino One more issue is you create a local QWidget on the stack in the constructor:

                            QWidget fenetre;
                            QTabWidget *onglets = new QTabWidget(&fenetre);
                            ``
                            That means it will be destroyed when the constructor finishes.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • mrjjM mrjj

                              Hi
                              you are doing that i ask in first post.

                              You have
                              QTabWidget *onglets; in h file but new another so that is why you crash.

                              /****************************/
                              QWidget fenetre; // this ones DIES as soon as functions ends. not good.

                              // 1 : Créer le QTabWidget
                              QTabWidget *onglets = new QTabWidget(&fenetre); THis one create a local variable,

                              It is not the one from .H ( and it cant be used outside the function)

                              So that is why you crash.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #15

                              @mrjj

                              Purely OOI: I don't use Qt Creator/C++, but in this case where there is a class member variable named foo and then a function local variable also named foo is declared, does the Qt Creator "squiggle underline" the local variable warning that it "overrides the class variable", for the OP to notice? My PyCharm/Python IDE does do that....

                              mrjjM 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @mrjj

                                Purely OOI: I don't use Qt Creator/C++, but in this case where there is a class member variable named foo and then a function local variable also named foo is declared, does the Qt Creator "squiggle underline" the local variable warning that it "overrides the class variable", for the OP to notice? My PyCharm/Python IDE does do that....

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

                                @JonB
                                Not per default. Maybe with c-lang code model.
                                I wish it did. :)

                                JonBJ 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @JonB
                                  Not per default. Maybe with c-lang code model.
                                  I wish it did. :)

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #17

                                  @mrjj
                                  Upgrade to Python ;-) LMAO/ROFL

                                  mrjjM 1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @mrjj
                                    Upgrade to Python ;-) LMAO/ROFL

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

                                    @JonB
                                    :)))
                                    I would - if you give me visible scopes and remove that dreadful Self.
                                    and what is up with ___Var to say its private :)
                                    Joking aside, its really a nice editor. I assume the Pro version is even more
                                    cool than the Community version.

                                    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