PDA

View Full Version : Header problem



waynew
22nd March 2011, 00: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);
ui->tvSpots->setHorizontalHeader(header);
model->setHeaderData(0, Qt::Horizontal, "Click Connect to start");
ui->tvSpots->show();

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

schnitzel
22nd March 2011, 03:06
try adding:


ui->tvSpots->horizontalHeader()->setVisible(true);

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.

waynew
27th March 2011, 00: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.

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

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

waynew
28th March 2011, 23: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.

wysota
28th March 2011, 23: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.

schnitzel
29th March 2011, 00: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.

waynew
29th March 2011, 00: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.

wysota
29th March 2011, 02: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()).