0

I'm confronted to a big problem actually, I had a QTabWidget that contains multiple QWidget-herited object named tab and I would like to change the QStatusBar in function of the QTabWidget index but the QMainwindow delete the QStatusBar (which is in the Tab Object) each time I change tabs and it causes the application to crash. here is my code

mainwindow.h

#ifndef TAB_H
#define TAB_H

#include <QtWidgets>

class Tab : public QWidget
{
public:
    Tab(int id);
    QStatusBar *sbar;


private:
};

#endif // TAB_H

mainwindow.cpp

#include "mainwindow.h"
#include "tab.h"
MainWindow::MainWindow()
{
tabs = new QTabWidget();

resize(800, 600);

connect(tabs, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int)));

for(int i = 0; i < 20; i++)
{
    tabs->addTab(new Tab(i), QString("%1").arg(i));
}

setCentralWidget(tabs);
}

MainWindow::~MainWindow()
{

}

void MainWindow::currentChanged(int)
{
    Tab *thistab = static_cast<Tab*>(sender());
    setStatusBar(new QStatusBar());
}

tab.h

#ifndef TAB_H
#define TAB_H

#include <QtWidgets>

class Tab : public QWidget
{
public:
    Tab(int id);
    QStatusBar *sbar;


private: 
};

#endif // TAB_H

tab.cpp

#include "tab.h"

Tab::Tab(int id)
{
    sbar = new QStatusBar();
    QHBoxLayout hbl;
    QLabel *ll = new QLabel("StatusBar tester: Tab number : #"+QString("%1").arg(id));
    sbar->addWidget(new QLabel(QString("%1").arg(id)));
    hbl.addWidget(ll);
    setLayout(&hbl);

}

Thanks for your patience

CheshireChild
  • 148
  • 1
  • 8
  • 1
    Ehh...your put the wrong code in `mainwindow.h`, please edit it. – Tay2510 Apr 25 '14 at 22:07
  • Why not have QMainWindow's in each tab? – paulm Apr 25 '14 at 22:47
  • 1
    You are not supposed to create status bars in such way. `QMainWindow` takes ownership of status bars, and you cannot change that. Usually there is no need to create multiple status bars. You should use the same status bar object in all tabs. – Pavel Strakhov Apr 26 '14 at 00:01

0 Answers0