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. Unable to Resize Columns [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Unable to Resize Columns [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 11.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.
  • T Offline
    T Offline
    toadybarker
    wrote on 8 Jun 2011, 13:23 last edited by
    #1

    Hi There,

    I am new to -QT4- Qt 4, and am trying to do resize some columns on a QTreeView but nothing I have tried seems to work. The treeView look like this...
    @
    proxyView = new QTreeView;
    proxyView->setRootIsDecorated(false);
    proxyView->setAlternatingRowColors(true);
    proxyView->setAllColumnsShowFocus(true);
    proxyView->setExpandsOnDoubleClick(false);
    proxyView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    proxyView->setModel(proxyModel);
    proxyView->setSortingEnabled(true);
    proxyView->sortByColumn(0, Qt::AscendingOrder);
    proxyView->header()->setMovable(false);
    proxyView->header()->resizeSections(QHeaderView::ResizeToContents);
    @
    The Headers were built in a previous module (In the ProxyModel initialization itself) and then the actual data is read from a database and put into the model. I would like the columns to be the maximum size of the largest data in the column. Do I need to set this up when building the Model, or how do I tell QT to do this?...

    All of the columns seem to display the same size no matter what I try.
    Thanks for any help.

    Edit: It is Qt, not QT, and please use @ tags around your code; Andre

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jim_kaiser
      wrote on 12 Jun 2011, 20:24 last edited by
      #2

      Use
      @
      void QTreeView::resizeColumnToContents ( int column )
      @

      You could set up a trigger on when to do this, i.e. when the model has changed, resize the column to current contents.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        toadybarker
        wrote on 14 Jun 2011, 13:29 last edited by
        #3

        I believe I tried to use that method. I called it with the first, second and third columns, but it still made them the same size. Maybe I didn't use it correctly. Can you post an example? Thanks for any help you can provide...

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jim_kaiser
          wrote on 14 Jun 2011, 13:50 last edited by
          #4

          Works for me! Try this.. Create a new Qt GUI project. Add a QTreeView and a QPushButton (resizeButton is the name I used) in the main window in designer. The MainWindow.cpp is as follows:

          @
          #include "mainwindow.h"
          #include "ui_mainwindow.h"

          #include <QtGui/QTreeView>
          #include <QtGui/QStandardItemModel>

          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);

          QStandardItemModel* model = new QStandardItemModel(this);
          QList<QStandardItem*> rowItems;
          
          rowItems << new QStandardItem(tr("this is a test string"));
          rowItems << new QStandardItem(tr("this is another test string"));
          rowItems << new QStandardItem(tr("this is yet another test string"));
          
          model->appendRow(rowItems);
          
          ui->treeView->setModel(model);
          

          }

          MainWindow::~MainWindow()
          {
          delete ui;
          }

          void MainWindow::on_resizeButton_clicked()
          {
          ui->treeView->resizeColumnToContents(0);
          ui->treeView->resizeColumnToContents(1);
          ui->treeView->resizeColumnToContents(2);
          }
          @

          To test it.. you could just add a button to your existing view and do the resize in the slot like in the above example.

          So, maybe you're not calling the resize at the right time... maybe the columns are reset after you call the resize. Check your code and debug to see..

          1 Reply Last reply
          0
          • T Offline
            T Offline
            toadybarker
            wrote on 14 Jun 2011, 14:20 last edited by
            #5

            ahh..... that worked. I originally wanted it to do that from the get go though... guess I could call that function at the right point....or maybe leave the button there anyway and let the user do it.....
            Thanks.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jim_kaiser
              wrote on 14 Jun 2011, 15:37 last edited by
              #6

              bq. ahh….. that worked. I originally wanted it to do that from the get go though… guess I could call that function at the right point…

              That would be the way to go. When some change happens which need a resize of columns do the resize. Connect to the signals which get emitted on a change in the value of a column and do the resize everytime that happens. Could you edit the first post and add [ Solved ] to the title.

              Cheers :)

              1 Reply Last reply
              0

              1/6

              8 Jun 2011, 13:23

              • Login

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