Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Empty QTableWidget vertical header not showing up
Forum Updated to NodeBB v4.3 + New Features

Empty QTableWidget vertical header not showing up

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 5.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

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

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    T 1 Reply Last reply
    2
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #3
      class PersistentHeader : public QHeaderView{
          Q_OBJECT
          Q_DISABLE_COPY(PersistentHeader)
      public:
          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;
              ensurePolished();
              QStyleOptionHeader opt;
              initStyleOption(&opt);
              opt.section = 0;
              QFont fnt =font();
              fnt.setBold(true);
              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));

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      T 2 Replies Last reply
      6
      • SGaistS SGaist

        Hi,

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

        T Offline
        T Offline
        tomatoketchup
        wrote on last edited by
        #4

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

        1 Reply Last reply
        0
        • VRoninV VRonin
          class PersistentHeader : public QHeaderView{
              Q_OBJECT
              Q_DISABLE_COPY(PersistentHeader)
          public:
              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;
                  ensurePolished();
                  QStyleOptionHeader opt;
                  initStyleOption(&opt);
                  opt.section = 0;
                  QFont fnt =font();
                  fnt.setBold(true);
                  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));

          T Offline
          T Offline
          tomatoketchup
          wrote on last edited by
          #5

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

          JonBJ 2 Replies Last reply
          0
          • T tomatoketchup

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #6

            @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....

            SGaistS 1 Reply Last reply
            0
            • JonBJ JonB

              @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....

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              1
              • SGaistS SGaist

                @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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #8

                @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?

                1 Reply Last reply
                0
                • T tomatoketchup

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #9

                  @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()?

                  EDIT:

                      self.tableWidget = QtWidgets.QTableWidget(0, 3)
                      self.centralLayout.addWidget(self.tableWidget)
                      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.

                  EDIT2
                  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.

                  T 1 Reply Last reply
                  0
                  • VRoninV VRonin
                    class PersistentHeader : public QHeaderView{
                        Q_OBJECT
                        Q_DISABLE_COPY(PersistentHeader)
                    public:
                        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;
                            ensurePolished();
                            QStyleOptionHeader opt;
                            initStyleOption(&opt);
                            opt.section = 0;
                            QFont fnt =font();
                            fnt.setBold(true);
                            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));

                    T Offline
                    T Offline
                    tomatoketchup
                    wrote on last edited by
                    #10

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

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @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()?

                      EDIT:

                          self.tableWidget = QtWidgets.QTableWidget(0, 3)
                          self.centralLayout.addWidget(self.tableWidget)
                          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.

                      EDIT2
                      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.

                      T Offline
                      T Offline
                      tomatoketchup
                      wrote on last edited by
                      #11

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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved