View Full Version : Header problem

22nd March 2011, 01:55
I have never tried this before, so in a new form I put into the constructor:

model = new QStandardItemModel(this);
ui->tvSpots->setModel(model); // QTableView
header = new QHeaderView(Qt::Horizontal, this);
model->setHeaderData(0, Qt::Horizontal, "Click Connect to start");

The table view shows up, but not the header.
Any ideas?

22nd March 2011, 04:06
try adding:


and try QString("Click Connect to start") for the third argument to setHeaderData().

I'm using code similar to yours and that's the only difference I can see.

27th March 2011, 01:53
Thanks Schnitzel, but unfortunately, that didn't do it.

I'm still working on it, but I added a debug and it says the setHeaderData returned false.
No idea why yet.
I have used this same type of code in a function in my mainwindow and it works perfectly.
So for the moment, I am stumped.

28th March 2011, 18:48
can you attach a minimal project that contains the problem or show the entire code?

28th March 2011, 19:46
Does the model contain any columns?

29th March 2011, 00:15
The model does not contain any columns at the time I attempt to set the header.
However, I worked around the problem by using setHorizontalHeaderLabels and this is working ok.

Thanks folks.

29th March 2011, 00:58
The model does not contain any columns at the time I attempt to set the header.
So no headers will be visible because there are no columns in the model. If you at least set a column count, the header might have something to display.

29th March 2011, 01:38
Does the model contain any columns?

and wysota strikes again :)

@waynew: Not sure why I deserved a thanks... you should give it to wysota.

29th March 2011, 01:59
Well, maybe. But he is a very helpful person :) and he has taught me a lot. But at 67 years old, I know I will never catch up to him.

Actually, I tried setColumnCount but that did not make any difference. The thing I do not understand is that I am doing this for another dialog from my main window and it works fine. The only differences I can see are that it is being done there from a function as opposed to the constructor here and there the model is a QSqlTableModel so of course it has db columns defined, so Wysota, you are probably correct about the columns. I just can't do it that way here since I need to dynamically change the header labels depending on what data is going to be received. So maybe the setHorizontalHeaderLabels is the best solution anyway.

29th March 2011, 03:45
QStandardItemModel behaves differently than other models. If it doesn't have any rows, it ignores its column count and thus you get an empty viewport. You need to provide info about the columns manually just like you did (another way would be to use QStandardItemModel::setHorizontalHeaderItem()).