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. Placing TableWidget Header Labels at runtime
Forum Updated to NodeBB v4.3 + New Features

Placing TableWidget Header Labels at runtime

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 348 Views 1 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.
  • DriftwoodD Offline
    DriftwoodD Offline
    Driftwood
    wrote on last edited by
    #1

    When I use this:

    ui->tableWidget->horizontalHeaderItem( 0 )->setText( "Last" );
    

    to set my tablewidget header at column 0, I get a seg fault.
    However, if I do this:

    QTableWidgetItem* col0 = new QTableWidgetItem(QString("L Name"),QTableWidgetItem::Type);
    ui->tableWidget->setHorizontalHeaderItem(0,col0);
    

    it works w/out issue.

    Thing is, I'm sure I was doing it the first way a year or so back. Or am I just doing it wrong? The second way I show here is really kind of silly in its long-winded approach just to Label a column.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Driftwood said in Placing TableWidget Header Labels at runtime:

      Or am I just doing it wrong?

      You did not read the documentation:

      "Returns the horizontal header item for column, column, if one has been set; otherwise returns nullptr."

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        The header uses items also - so they must be allocated.

        But if you find it a bit clunky there is also

        setHorizontalHeaderLabels(const QStringList &labels)

        aka

        setHorizontalHeaderLabels(QStringList() << "L1" << "L2");

        for a shorter version.

        1 Reply Last reply
        1
        • DriftwoodD Offline
          DriftwoodD Offline
          Driftwood
          wrote on last edited by
          #4

          @Christian-Ehrlicher - Thank you.

          @mrjj - I found the QStringList on SO and already have it in place.

          Thank you both for your help.

          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