PDA

View Full Version : How to center QTableView itself



Sir_of_bad_coding
3rd February 2013, 14:27
Hello, I have five days of experience with Qt.
I've completed my program for the most part, but there's something I just can't get to work.

I want to place QTableView (which has 6 columns) to the middle of the window.
I've tried layouts but that's apparently not what I want.
I've also tried QTableView's move method but that doesn't work, either.
Here's my latest try:


model = new customTable(999, 6, this);

model->setHorizontalHeaderItem(0, new QStandardItem(tr("Manufacturer")));
model->setHorizontalHeaderItem(1, new QStandardItem(tr("Model")));
model->setHorizontalHeaderItem(2, new QStandardItem(tr("Type")));
model->setHorizontalHeaderItem(3, new QStandardItem(tr("Available")));
model->setHorizontalHeaderItem(4, new QStandardItem(tr("Misc")));
model->setHorizontalHeaderItem(5, new QStandardItem(tr("Cost")));

model->table->setModel(model);
model->table->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);


model->table->adjustSize();
model->table->move(QApplication::desktop()->screen()->rect().center() - model->table->rect().center());

setCentralWidget(model->table);

Doesn't work... the table just isn't moved. (customTable is a class I made for my own needs - like custom SLOTs and SIGNALs. etc; it inherits from QStandardItemModel and has a public QTableView *table.)
What do I have in mind?
Well, I want my table to be in the middle (ala Wordpad), with a blue background.
I've tried skimming through a lot of classes and methods, but nothing I've tried so far worked. (I also tried QHBoxLayout, but that's obviously not something I want?)

Thank you in advance... and sorry if the solution is easy, I'm just not experienced enough with Qt.
(I'm using Qt 4.8; C++.)

Lesiok
3rd February 2013, 15:56
Use horizontal layout with two horizontal spacer on both sides of table view. Like on picture.

Santosh Reddy
3rd February 2013, 17:25
- Add the table to a layout (QGridLayout/QHBoxLayout/QVBoxLayout)
- Set the required widget allignment for the table (Qt::AlignCenter/Qt::AlignHCenter/Qt::AlignVCenter)
- Then set the layout onto a QWidget
- Set this QWidget as the central widget of QMainWindow

Here is how the code would look



{
...
model = new customTable(999, 6, this);

model->setHorizontalHeaderItem(0, new QStandardItem(tr("Manufacturer")));
model->setHorizontalHeaderItem(1, new QStandardItem(tr("Model")));
model->setHorizontalHeaderItem(2, new QStandardItem(tr("Type")));
model->setHorizontalHeaderItem(3, new QStandardItem(tr("Available")));
model->setHorizontalHeaderItem(4, new QStandardItem(tr("Misc")));
model->setHorizontalHeaderItem(5, new QStandardItem(tr("Cost")));

model->table->setModel(model);
model->table->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
model->table->adjustSize();

QWidget * widget = new QWidget;
QGridLayout * layout = new QGridLayout;

layout->addWidget(model->table);
layout->setAlignment(table, Qt::AlignCenter);

widget->setLayout(layout);

setCentralWidget(widget);
...
}

Sir_of_bad_coding
3rd February 2013, 18:20
Thanks, Santosh Reddy!
There's still one little problem, though.
http://i.imgur.com/bUP1TwG.png

(I already figured out how to set margins.)
But, I don't want the table to have its own sliders.
Is there any easy way to do it or do I have to re-implement methods?
ty

(Man... Qt is really overwhelmingly big)

Santosh Reddy
3rd February 2013, 18:28
You can turn off the scroll bars


table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);

Sir_of_bad_coding
3rd February 2013, 18:48
Hm thanks ... but that's still not entirely it...
I don't want the QWidget to have the sliders but I do want the MainWindow to have sliders which would control qwidget scrolling

Santosh Reddy
3rd February 2013, 18:55
MainWindow cannot have sliders, only widgets in side MainWindow can have sliders...

Sir_of_bad_coding
3rd February 2013, 19:08
Ah thanks for clearing this up