[Qt-interest] Expanding view items in a window
Chris Meyer
cmeyer1969+qt at gmail.com
Thu Jun 25 21:00:50 CEST 2009
In the code below, how do I get the QTableView to expand to fill the
available space?
(This project can be easily place into a new project using Qt Creator
by creating a new "Qt4 Gui Application" project based on a QWidget and
unchecking the 'generate form' at the appropriate point. Then replace
widget.cpp with this code.)
#include "widget.h"
#include <QAbstractTableModel>
#include <QVBoxLayout>
#include <QLabel>
#include <QTableView>
#include <QHeaderView>
class MyTableModel : public QAbstractTableModel
{
public:
virtual int rowCount(const QModelIndex &parent) const;
virtual int columnCount(const QModelIndex &parent) const;
virtual QVariant headerData (int section, Qt::Orientation
orientation, int role ) const;
QVariant data(const QModelIndex &index, int role) const;
};
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setMinimumSize(400,200);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(new QLabel(tr("My table:")));
QTableView *table_view = new QTableView();
table_view->setModel(new MyTableModel());
table_view->setAlternatingRowColors(true);
table_view->verticalHeader()->hide();
layout->addWidget(table_view);
}
Widget::~Widget()
{
}
int MyTableModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return 2;
}
int MyTableModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return 2;
}
QVariant MyTableModel::headerData (int section, Qt::Orientation
orientation, int role ) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
{
if (section == 0)
return tr("Column 1");
else if (section == 1)
return tr("Column 2");
}
return QVariant();
}
QVariant MyTableModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
{
return 100 + index.column() * 10 + index.row();
}
return QVariant();
}
More information about the Qt-interest-old
mailing list