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. [Solved] QTableWidget columns with different width
Forum Updated to NodeBB v4.3 + New Features

[Solved] QTableWidget columns with different width

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 103.3k 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.
  • X Offline
    X Offline
    xtingray
    wrote on 23 Feb 2011, 07:00 last edited by
    #1

    Hello there,
    Anyone has some piece of code to understand how to define a QTableWidget object where every column has a different width?
    I tried resizeColumnToContents( int ) and setColumnWidth ( int, int ) with no luck :(

    Thanks!


    Qt Developer

    C 1 Reply Last reply 7 May 2021, 19:24
    0
    • A Offline
      A Offline
      andre
      wrote on 23 Feb 2011, 07:14 last edited by
      #2

      But, that is basically wat you need to do to get different column widths. Could you show the relevant code with your attempt please?

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xtingray
        wrote on 23 Feb 2011, 07:25 last edited by
        #3

        Hi! In my case, every column will contain data with a very specific size. Any clue?
        This is my code, but is not working at all:

        @
        StepsViewer::StepsViewer(QWidget *parent) : QTableWidget(parent)
        {
        setColumnCount(3);
        setColumnWidth(0, 80);
        setColumnWidth(1, 40);
        setColumnWidth(2, 20);
        setHorizontalHeaderLabels(QStringList() << tr("Interval") << tr("Frames") << tr("Add"));
        ...
        }@

        Edit (Andre): fixed code formatting. Please do that yourself next time...


        Qt Developer

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on 23 Feb 2011, 07:32 last edited by
          #4

          OK, so you are inheriting QTableWidget.
          Could you try what happens if you set the column widths after you set the data? At first sight, the above should work, but I believe you if you say it doesnt.

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xtingray
            wrote on 23 Feb 2011, 21:21 last edited by
            #5

            I found the problem! All my fault :S
            In some place from another class I was doing this:
            @
            k->stepViewer = new StepsViewer;
            k->stepViewer->verticalHeader()->hide();
            k->stepViewer->horizontalHeader()->setResizeMode(QHeaderView::Stretch); // <- Aaaargh! :S
            @

            Shame on me! setColumnWidth(int, int) really works!
            My apologies :(


            Qt Developer

            1 Reply Last reply
            0
            • X xtingray
              23 Feb 2011, 07:00

              Hello there,
              Anyone has some piece of code to understand how to define a QTableWidget object where every column has a different width?
              I tried resizeColumnToContents( int ) and setColumnWidth ( int, int ) with no luck :(

              Thanks!

              C Offline
              C Offline
              CEO.
              wrote on 7 May 2021, 19:24 last edited by
              #6

              @xtingray here is for making the column widths fit into the qtable widget.

              QTableWidget-name.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)

              Remember to import QHeaderView from PyQt5.Qtwidgets.

              Your QTablewidget-name should be replaced by the name you used for it in your code

              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