Unable to Resize Columns [SOLVED]
-
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
-
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.
-
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...
-
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..
-
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. -
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 :)