Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    General and Desktop
    4
    18
    2383
    Loading More Posts
    • 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.
    • F
      f.lerdino last edited by

      Hi, I'm new in this forum so I don't know is the correct forum section for this kind of question.
      I try to do a onglets->setCurrentIndex(0); in order to change when I clicked a Push buttun the index of a QTabWidget created in the MainWindow but with an another function:
      void MainWindow::playaction()
      { onglets->setCurrentIndex(0);
      }

      I also have tried to out every think public but nothing change.

      Do you have any idea?
      thanks

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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 Reply Quote 2
        • F
          f.lerdino last edited by

          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?

          mrjj JonB 2 Replies Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @f.lerdino last edited by mrjj

            @f.lerdino

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

            like
            page1 = new QWidget(this);

            1 Reply Last reply Reply Quote 2
            • JonB
              JonB @f.lerdino last edited by

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

              1 Reply Last reply Reply Quote 0
              • F
                f.lerdino last edited by

                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);
                

                }

                JonB 1 Reply Last reply Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion last edited by mrjj

                  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 Reply Quote 1
                  • F
                    f.lerdino last edited by

                    #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 Reply Quote 0
                    • JonB
                      JonB @f.lerdino last edited by

                      @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 Reply Quote 0
                      • F
                        f.lerdino last edited by

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

                        JonB 1 Reply Last reply Reply Quote 0
                        • F
                          f.lerdino last edited by

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

                          jsulm 1 Reply Last reply Reply Quote 0
                          • mrjj
                            mrjj Lifetime Qt Champion last edited by

                            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.

                            JonB 1 Reply Last reply Reply Quote 3
                            • JonB
                              JonB @f.lerdino last edited by

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

                              1 Reply Last reply Reply Quote 0
                              • jsulm
                                jsulm Lifetime Qt Champion @f.lerdino last edited by

                                @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 Reply Quote 1
                                • JonB
                                  JonB @mrjj last edited by 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....

                                  mrjj 1 Reply Last reply Reply Quote 0
                                  • mrjj
                                    mrjj Lifetime Qt Champion @JonB last edited by

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

                                    JonB 1 Reply Last reply Reply Quote 0
                                    • JonB
                                      JonB @mrjj last edited by

                                      @mrjj
                                      Upgrade to Python ;-) LMAO/ROFL

                                      mrjj 1 Reply Last reply Reply Quote 0
                                      • mrjj
                                        mrjj Lifetime Qt Champion @JonB last edited by

                                        @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 Reply Quote 0
                                        • First post
                                          Last post