QTableview not fitting the entire dockwidget window
This code generates a tableview for a docked window as shown in image 1 but I want a tableview as shown in image 2. (second image tableview was generated on mainwindow using
setCentralWidget(widget);). When I try to use the same code , i get the error 'setCentralwidget' not declared in this scope as it is not mainwindow. How to get the similar view for a docked window?
ui->deleteButton->setEnabled(false); ui->tableView->setModel(model); ui->tableView->setItemDelegate(new ItemDelegate(this)); ui->tableView->verticalHeader()->setDefaultAlignment(Qt::AlignVCenter | Qt::AlignRight); ui->tableView->horizontalHeader()->setStretchLastSection(true); ui->tableView->resizeColumnsToContents(); ui->tableView->verticalHeader()->setSectionsMovable(true); ui->addButton->setFocus();
hi for a docked window, you would add a
layout to it first and then insert the controls
in that layout. then it will fit all area of window.
Did you call QDockWidget::setWidget ?
Ya I have called that code ...
Ya I have already used this code.
QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(ui->tableView); layout->addWidget(ui->addButton); layout->addWidget(ui->deleteButton); QWidget *widget = new QWidget; widget->setLayout(layout); setCentralWidget(widget);
But I am getting the following error.
error: 'setCentralWidget' was not declared in this scope
This is only valid with a QMainwindow. :)
You would do
(the code u shown)
I already tried dis code but of no use. It is giving the same output.
QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(ui->tableView); layout->addWidget(ui->addButton); layout->addWidget(ui->deleteButton); // QWidget *widget = new QWidget; QDockWidget *DockWidget = new QDockWidget; DockWidget->setLayout(layout); DockWidget->setWidget (DockWidget);
Don't set the layout on DockWidget.
By the way, there's something fishy in your code. Did you create your widget with designer and now trying to use it with a QMainWindow ?
Because it looks like you are trying to create the dock widget from within your custom widget and not from a QMainWindow derived class.
First I have created the dock in the mainwindow and I am creating the table on the dock through a table class. This layout function is in the class.
Suggest me the right way of getting this done.
// Dock Creation this->setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea); this->setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea); this->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea); this->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea); // table creation QDockWidget *dock = new QDockWidget("Way Point Table",this); wayPointTable *wptable = new wayPointTable(); dock->setWidget(wptable); dock->setFixedSize(500, 300); this->addDockWidget(Qt::BottomDockWidgetArea, dock);
WayPointTable class has this code:
ui->tableView->verticalHeader()->setDefaultAlignment(Qt::AlignVCenter | Qt::AlignRight); ui->tableView->horizontalHeader()->setStretchLastSection(true); ui->tableView->resizeColumnsToContents(); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(ui->tableView); layout->addWidget(ui->addButton); layout->addWidget(ui->deleteButton); // QWidget *widget = new QWidget; QDockWidget *DockWidget = new QDockWidget; DockWidget->setLayout(layout); DockWidget->setWidget (DockWidget); // setCentralWidget(widget);
There's no need for any dock widget related code in WayPointTable. Also
DockWidget->setWidget (DockWidget);doesn't really make sense.
One thing to clean, since you used designer for that widget, why are you setting a layout in code ? You should put it directly in through designer.
How to set the tableview to occupy the entire dock window through the designer ? I have put the tableview and the 2 buttons in a formlayout.
Did you set the form layout properly on the widget in designer ?
By the way, why a form layout ?
You should either have everything in a QVBoxLayout or if you want the buttons to be side by side, put them in a QHBoxLayout within the main QVBoxLayout.
These layouts are not giving the required display. I have played with the different layouts. After putting the layout code along with the setcentralwidget, then it had worked when I had used on the mainwindow.
So a table view on top of an add button on top of a delete button ? That's what QVBoxLayout is for.
If you get what's on image1 it means that you didn't apply the QVBoxLayout properly. With it these buttons can't be side by side unless:
- They are in a QHBoxLayout
- Not in a layout at all but you put them side by side on the widget in designer.
Here is a sample that uses a UI and scale with the dock
Maybe it can provide some hint.
Note the UI is widget based for this to work.
I have changed the list widget to tablewidget and added the header columns.
You can observe that the table fits the entire dock. I have done same thing in my other code also but the table does not fit the entire dock (i have used tableview but i guess it should not matter).
Note: the dock code is same.
if u set setFixedSize, it wont be resizeable ?
Well the layout are in the UI file. Inserted as the first thing.
It sounds to me that your OTHER code is not using the layout
The scaling is done via layout and layout is the first thing to insert into
the UI. then the rest of the controls. Matters not which Widgets. All can
be managed by a layout.
I dont want the dock to be resizeable but the tableview to fit the dock entirely. I have commented out all the layout code and put vertical layout in the designer. Still, I get the same problem.
well if the layout is applied correctly, you can see at once in Designer that
it works so there must be something wrong.
To put the vertical layout, you
right click and go to the layout menu ?
I can see the error.
You have dragged a layout from left side.
Those are for floating layouts or layout in layout.
To apply layout, simply right click the form,
and use the Layout menu. That will scale.
Do not drag from the left. Right click. :)
You dont have to remove the red layout. Simply right click outside and apply
layout via the layout menu to the form object.
Thanks a lot!!
I should have sent the UI image earlier. Sorry for the inconvenience.
Np. The drag able layout are a bit non intuitive :)
If you have a completely new form with no widgets, you must
place 1 widget on form for the right click menu to be active . that
fooled me a lot in the beginning. :)