PDA

View Full Version : problem with Inheritance and QTreeView



Kicer
3rd May 2010, 16:10
Hi!

i have a class which implement a simple collapsible widget:



class CollapsibleWidget: public QGroupBox
{
Q_OBJECT

public:
CollapsibleWidget( const QString& t, QWidget* p = 0 );

void setCollapsibleLayout(QLayout* l);

protected:
QBoxLayout *mainLayout;
QWidget *mainWidget;

protected slots:
void collapse(bool on);
};


and here the implementation:



CollapsibleWidget::CollapsibleWidget (const QString& t, QWidget* p):
QGroupBox(t,p)
{
setCheckable(true);

mainWidget=new QWidget();

mainLayout=new QVBoxLayout(this);
mainLayout->addWidget(mainWidget);

connect(this, SIGNAL(toggled(bool)), this, SLOT(collapse(bool)));
}


void CollapsibleWidget::setCollapsibleLayout (QLayout* l)
{
mainWidget->setLayout(l);
}


void CollapsibleWidget::collapse (bool on)
{
mainWidget->setVisible(on);
}


the idea is simple: it's a groupbox with checkbox, if user press the chechbox, the mainWidget is being hidden.

now i create a LogWidget class which inherits from CollapsibleWidget. It's used to display logs
(this class is quite big so i'll only show constructor):



LogWidget::LogWidget(const QString& t, const Symulator *s, const QStringList &levels, QWidget* p):
CollapsibleWidget(t, p), symulator(s), maxLevel(0)
{
id=logWidgetCounter++;
name=t;

level=new QComboBox();

if (levels.size()<2)
level->setVisible(false);

foreach(QString str, levels)
{
level->addItem(str);
}

QStringList header;
header << "komunikat" << "chwila czasu";

model=new QStandardItemModel(this);
model->setHorizontalHeaderLabels(header);

view=new QTreeView();
view->setItemsExpandable(false);
view->setRootIsDecorated(false);
view->setHeaderHidden(false);
view->setModel(model);

QVBoxLayout *l=new QVBoxLayout();
l->addWidget(view);
l->addWidget(level);

setCollapsibleLayout(l);


connect(view, SIGNAL(clicked(QModelIndex)), this, SLOT(selected(QModelIndex)));
connect(symulator, SIGNAL(resetSimulation()), this, SLOT(reset()));
connect(level, SIGNAL(currentIndexChanged(int)), this, SLOT(levelIndexChanged(int)));
}



and now the problem:
i use QTreeView + QStandardItemModel to display log's data. that's working
but as You can see i setup header ( model->setHorizontalHeaderLabels(header); )
Imho it should display the header over log's data, but it doesn't.
The header appears after i add first line to log. but it's doesn't contain data i wanted, the column names are set to "1", "2", ....
When i use model->setHorizontalHeaderLabels(header) again after i add some data to log, it's being displayed correctly.

the weirdest thing is that, if i create QTreeView + QStandardItemModel (same code as in LogWidget constructor) in some other widget (which inherits directly from QWidget) the QTreeView contains correct header.

any suggestions?