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


  • Qt Champions 2017

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

    ?



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


  • Qt Champions 2017

    @f.lerdino

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

    like
    page1 = new QWidget(this);



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



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

    }


  • Qt Champions 2017

    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;

    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



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

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

    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?


  • Qt Champions 2017

    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.



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


  • Moderators

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


  • Qt Champions 2017

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



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


  • Qt Champions 2017

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.