Important: Please read the Qt Code of Conduct -

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

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

  • Lifetime Qt Champion

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


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

  • Lifetime Qt Champion


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

    page1 = new QWidget(this);

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

  • MainWindow::MainWindow()

    QVBoxLayout *layout = new QVBoxLayout;
    QWidget *widget = new QWidget;
    widget->setStyleSheet("padding: 10px;" "border-style: solid;" "border-width: 5px;" "border-radius: 4px;");
    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;
        /* 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->append("Wood fuels", 319663);
            series2->append("Hydro power", 45875);
            series2->append("Wind power", 1060);
            QPieSeries *series3 = new QPieSeries();
            series3->append("Nuclear energy", 238789);
            series3->append("Import energy", 37802);
            series3->append("Other", 32441);
            DonutBreakdownChart *donutBreakdown = new DonutBreakdownChart();
            donutBreakdown->setTitle("Total consumption of energy in Finland 2010");
            donutBreakdown->addBreakdownSeries(series1, Qt::red);
            donutBreakdown->addBreakdownSeries(series2, Qt::darkGreen);
            donutBreakdown->addBreakdownSeries(series3, Qt::darkBlue);
            QChartView *chartView = new QChartView(donutBreakdown);
         // Page 2
        QProgressBar *progress = new QProgressBar;
        QSlider *slider = new QSlider(Qt::Horizontal);
        QPushButton *bouton3 = new QPushButton("Valider");
        QVBoxLayout *vbox2 = new QVBoxLayout;
        // Page 3 (je ne vais afficher qu'une image ici, pas besoin de layout)
        page3->setPixmap( QPixmap (":/images/qt5.png") );
        // 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");


  • Lifetime Qt Champion

    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 ?

  • #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    class DiagramScene;

    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;

    class MainWindow : public QMainWindow


    public slots:
    void playaction();

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

    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

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


    Is that right?

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

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

  • Lifetime Qt Champion

    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.

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

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion

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

  • @mrjj
    Upgrade to Python ;-) LMAO/ROFL

  • Lifetime Qt Champion

    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.

Log in to reply