Qt World Summit: Register Today!

Empty QTableWidget vertical header not showing up

  • Hi everyone,

    How to make the vertical header visible even if the QTableWidget is empty? I find it very annoying when the header shows and hides again and again. I'm writing a server, and the table contains a list of clients. When there are no clients, the vertical header disappears, which I'd like to avoid.
    I've already tried the following:


    But it doesn't work.

    Thanks in advance!

  • Lifetime Qt Champion


    IIRC, if you want the header to be there, then you have to have at least one row even if empty.

  • class PersistentHeader : public QHeaderView{
        PersistentHeader(Qt::Orientation orientation,QWidget* parent= Q_NULLPTR):QHeaderView(orientation,parent){}
        QSize sizeHint() const Q_DECL_OVERRIDE{
            const QSize original = QHeaderView::sizeHint();
            if(original.width()>0 || original.height()>0)
                return original;
            QStyleOptionHeader opt;
            opt.section = 0;
            QFont fnt =font();
            opt.fontMetrics = QFontMetrics(fnt);
            opt.text = QStringLiteral("0");
            if (isSortIndicatorShown())
                opt.sortIndicator = QStyleOptionHeader::SortDown;
            return style()->sizeFromContents(QStyle::CT_HeaderSection, &opt, QSize(), this);

    Then call something like tableWidget->setVerticalHeader(new PersistentHeader(Qt::Vertical,tableWidget));

  • @SGaist Too bad it can't be done, because it's really annoying. Thanks for the reply!

  • @VRonin Thank you for your reply! I'll try this and tell you if it works, and I'm sure it will.

  • @tomatoketchup
    Qt is not alone in having tables where if there are no rows the column headers do not show. I know there are similar among ASP.NET's table controls, and I too find it very irritating that they insist on some dummy blank row else they keep switching the headers on & off.

    You can't always follow @SGaist 's "you have to have at least one row even if empty". My tables are for SQL models, and it must have just whatever rows there are in the database, I can't add one.

    Having said the above, the code I have uses QTableViews against QSql... models and I don't seem to have any trouble still showing the header even when there are no rows. I wonder how....

  • Lifetime Qt Champion

    @JonB said in Empty QTableWidget vertical header not showing up:

    You can't always follow @SGaist 's "you have to have at least one row even if empty". My tables are for SQL models, and it must have just whatever rows there are in the database, I can't add one.

    I agree with that statement, however, my suggestion was specific to this context: QTableWidget.

  • @SGaist
    Ah, OK, QTableWidget has its own model, doesn't it? Whereas my case I was thinking of is a QTableView with a QSqlTableModel as its model, where the "empty row" would be more troublesome, right?

  • @tomatoketchup
    I can only say that my code, which still shows the column headers (that is what you want, right?) when no rows in model, calls QStandardItemModel::setHorizontalHeaderLabels() (note that is on the model). Are you already using QTableWidget's own setHorizontalHeaderLabels()?


        self.tableWidget = QtWidgets.QTableWidget(0, 3)
        self.tableWidget.setHorizontalHeaderLabels(["Col1", "Col2", "Col3"])
        self.tableWidget.setVerticalHeaderLabels(["Row1", "Row2", "Row3"])

    The above does show the column headers when no rows (QT 5.7). Maybe I'm confusing your "vertical" & "horizontal"? The above does not of course show any row headers as there are 0 rows, what would you expect; if you set the rows (e.g. QtWidgets.QTableWidget(3, 3)) then they do show. Just as if you had no columns (QtWidgets.QTableWidget(3, 0)) the column headers would not show.

    Sorry, I'm only beginning to understand you do want a vertical/row header, not columns. My bad. And you want that to "take up room" even if there are no rows so nothing to show, that's your question, right? In which case obviously @VRonin | @SGaist 's suggestions are needed.

  • @VRonin It works like a charm! Thank you so much!!

  • @JonB Thank you for your reply! No problem, I think I poorly explained the issue.

Log in to reply