PDA

View Full Version : Resizing QSqlTableModel



teS
12th January 2006, 20:55
Hi.
Is there any way to resize QSqlTableModel (I want to auto resize colums), like this:

http://img58.imageshack.us/img58/1092/qstableres1oc.png

and resize it to this:

http://img58.imageshack.us/img58/8265/qstableres26kj.png


Thanks in Advance for any help.

Corran
12th January 2006, 21:27
For starters, you won't resize the model but rather the table widget you're using to display it (see http://doc.trolltech.com/4.1/qt4-interview.html). This could either be a QTableView or QTableWidget. Get the horizontal header of the table using
QHeaderView* QTableView::horizontalHeader();And resize the section you want using
void QHeaderView::resizeSection(int logicalindex, int size);

teS
12th January 2006, 21:55
Thanks, but I don't realy know how to use it.

I'm creating my QTableView in function Okno::baza() :



db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("magazyn");
db.open();
tabelka = new QSqlTableModel;
tabelka->setTable("produkty");
tabelka->select();
tabelka->setHeaderData(0, Qt::Horizontal, tr("Id"));
tabelka->setHeaderData(1, Qt::Horizontal, tr("Nazwa"));
tabelka->setHeaderData(2, Qt::Horizontal, tr("Ilość"));
tabelka->setHeaderData(3, Qt::Horizontal, tr("Cena Netto"));
tabelka->setHeaderData(4, Qt::Horizontal, tr("Data Ważności"));
view = new QTableView;
view->setModel(tabelka);


In constructor Okno::Okno() I'm adding view do layout:



baza();
centralWidget = new QWidget;
glowny = new QVBoxLayout;
glowny->addWidget(view, 0, Qt::AlignHCenter);
centralWidget->setLayout(glowny);
setCentralWidget(centralWidget);


Class Okno inherits from QMainWindow.

wysota
12th January 2006, 22:00
This could either be a QTableView or QTableWidget.

It can't be QTableWidget. Its setModel() method is private. It can probably be QTreeView, though.

teS
15th January 2006, 02:29
Sorry for refreshing this topic, but I haven't found sloution for my problem yet.
So I'm asking again: Is there any way to resize colums in QTableView ?

wysota
15th January 2006, 09:50
I think the question has already been answered in the second post of this thread.

GreyGeek
19th January 2006, 21:08
Sorry for refreshing this topic, but I haven't found sloution for my problem yet.
So I'm asking again: Is there any way to resize colums in QTableView ?



QSqlQueryModel *viewModel = new QSqlQueryModel(wnui.wholeNameView);
viewModel->setHeaderData(0, Qt::Horizontal, "ID");
viewModel->setHeaderData(1, Qt::Horizontal, "SSN");
viewModel->setHeaderData(2, Qt::Horizontal, "WholeName");
viewModel->setHeaderData(3, Qt::Horizontal, "SSSN");
viewModel->setHeaderData(4, Qt::Horizontal, "SName");
viewModel->setHeaderData(5, Qt::Horizontal, "City");
viewModel->setHeaderData(6, Qt::Horizontal, "Address");

viewModel->setQuery(queryStr);
if (viewModel->lastError().type() == QSqlError::NoError){
wnui.wholeNameView->setModel(viewModel);
if (viewModel->rowCount() > 0){
for (int i = 0; i < viewModel->rowCount(); ++i)
this->wnui.wholeNameView->verticalHeader()->resizeSection(i,20);
for (int i = 0; i < 7; ++i)
wnui.wholeNameView->resizeColumnToContents(i);
}
}
}

where wholeNameView is the name of the QTableView object in my wholenamedlg.ui file. "wnui" was the name assigned to the Ui class in the wholenamedlg class definition in the wholenamedlg.h file, and wholeNameDlgUI is the ObjectName of the wholenamedlg.ui form itself.


private:
Ui::wholeNameDlgUI wnui;