I want to expand QTableViews' size so each QTableView takes the half size of the window! , how can i do it?
screenshot:
http://i.stack.imgur.com/Rh87o.jpg
Add a QHBoxLayout to your widget/window and then add the tables to it.
The following code on a top level widget..
QHBoxLayout *horizontalLayout;
QTableView *tableView;
QTableView *tableView_2;
horizontalLayout = new QHBoxLayout(Widget);
tableView = new QTableView(Widget);
horizontalLayout->addWidget(tableView);
tableView_2 = new QTableView(Widget);
horizontalLayout->addWidget(tableView_2);
Will give you something like..
The two tables share the available space equally and expand and resize with the main window.
Related
I pragrammaticaly create a QTreeWidget.
Then I pragrammaticaly add some items.
Then I add two QLabel widgets to two items (QTreeWidgetItem) by
myTree->setItemWidget(item1, 0, myLabel1);
myTree->setItemWidget(item2, 0, myLabel2);
And then I try to resize the row of the item pragrammaticaly.
If I use an
item1->setSizeHint(0, QSize(myWidth, myHeight) );
the row chaged. But myLabel1 is not.
If I use an
item1->setSizeHint(0, QSize(myWidth, myHeight) );
myLabel->resize(myWidth, myHeight);
everething is ok but the row of myLabel2 mis adjusting to label by position.
Can I do something to auto-adjusting a widget (by size and position) to a cell of QTreeWidget?
P.S. After any resizing of tree (resize by width or expand/collapse node) widgets updates correctly.
In view of the fact that autofit start after resizing QTreeWidget, there is some method inside that resize widgets in cells.
So I opened QTreeWidget description (https://doc.qt.io/qt-5/qtreewidget.html) and red all of Public Functions and Public Slots. When I did not find any useful function I looked at a parent class (QTreeView). And found
myTree->resizeColumnToContent(0);
Call resizeColumnToContent after resizing any row in a QTreeWidget and widgets will be always fit to cells.
P.S. I am the OP.
you can try adding/updating a widget as an item, for example:
QWidget* wdg = new QWidget;
QPushButton* btIcon = new QPushButton();
QLabel* lb = new QLabel();
QHBoxLayout* layout = new QHBoxLayout(wdg);
layout->addWidget(btIcon);
layout->addWidget(lb);
layout->setAlignment( Qt::AlignCenter );
wdg->setLayout(layout);
myTree->setItemWidget(item1, 0, wdg);
Or just fill the tree with widgets
I have the following to add a QTableView to a QWidget:
QVBoxLayout *vLayout = new QVBoxLayout(this);
QTableView *tableView = new QTableView;
tableView->horizontalHeader()->setStretchLastSection(true);
tableView->verticalHeader()->setStretchLastSection(true);
tableView->verticalHeader()->setVisible(false);
vLayout->addWidget(tableView);
This widget will use model which load data from MySQL... And there is only one line of content, so I would like to make the view just height enough to show one line. How to approach this issue?
I had the same problem and found that this works best:
const auto height = table.horizontalHeader()->sizeHint().height() + table.rowHeight(0);
table.setMinimumHeight(height);
table.setMaximumHeight(height);
You force the table to keep the exact size specified by you: the header size + the size of a single row.
I'd like to have a fixed size QGraphicsView, which I want to add to a layout together with some other widgets. However, the QGraphicsView simply ignores resize(), here is the relevant code:
QGraphicsScene* scene = new QGraphicsScene;
QGraphicsView* view = new QGraphicsView(scene);
view->setBackgroundBrush(QBrush(Qt::black, Qt::SolidPattern));
view->resize(1000, 600);
QVBoxLayout* layout = new QVBoxLayout;
layout->setMargin(0);
layout->addWidget(view);
setLayout(layout);
If I use setFixedSize() instead of resize(), the size is actually being set correctly. However, it seems that the window size is not updated, the window is not centered properly.
How am I supposed to set a fixed size for a QGraphicsView?
I know this is a very old question, but in case anyone else stumbles into it: you can set both minimumSize and maximumSize to the desired target size, and it should work regardless of what layout the QGraphicsView is in.
If I understood you right you want to have QGraphicsView centred inside the window and having fixed size. You need rather then VBoxLayout you should use QGridLayout with spacers, so your form should look like:
<Empty> <VSpacer> <Empty>
<HSpacer> <GraphicsView> <HSpacer>
<Empty> <VSpacer> <Empty>
I am creating a UI in Qt that has a QDockWidget containing a QPushButton and QLineEdit. Please refer to the attached mock-up. I have created the widget components and successfully got them up and running. However they are not positioned the way I want them to. Both the elements should float to the left making the extra space to the right stretch when the window is resized.
The code-
this->searchField = new QLineEdit; //"this" is a QDockWidget subclassed Object
searchField->setFixedWidth(200);
mainMenu = new Menu();
QHBoxLayout *layout= new QHBoxLayout;
QSpacerItem *filler = new QSpacerItem(1000, 10);
layout->addWidget(mainMenu->getMenuBar());
layout->addWidget(this->searchField);
layout->addSpacerItem(filler);
Any suggestion or help would be awesome!
Thanks for your time :)
http://qt-project.org/doc/qt-4.8/layout.html
http://qt-project.org/doc/qt-4.8/qboxlayout.html#addStretch
void QBoxLayout::addStretch ( int stretch = 0 )
Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.
So this is what your new code would look like:
this->searchField = new QLineEdit;
searchField->setFixedWidth(200);
mainMenu = new Menu();
QHBoxLayout *layout= new QHBoxLayout;
layout->addWidget(mainMenu->getMenuBar());
layout->addWidget(this->searchField);
layout->addStretch(); // Added this
Hope that helps.
I want to create a Qt window that contains two layouts, one fixed height that contains a list of buttons at the top and one that fills the remaning space with a layout that centers a widget vertically and horizontally as per the image below.
How should i be laying out my layouts/widgets. Ive tried a few options with nested horizontal and vertical layouts to no avail
Try making the pink box a QWidget with a QHBoxLayout (rather than just making it a layout). The reason is that QLayouts don't provide functionality to make fixed sizes, but QWidgets do.
// first create the four widgets at the top left,
// and use QWidget::setFixedWidth() on each of them.
// then set up the top widget (composed of the four smaller widgets):
QWidget *topWidget = new QWidget;
QHBoxLayout *topWidgetLayout = new QHBoxLayout(topWidget);
topWidgetLayout->addWidget(widget1);
topWidgetLayout->addWidget(widget2);
topWidgetLayout->addWidget(widget3);
topWidgetLayout->addWidget(widget4);
topWidgetLayout->addStretch(1); // add the stretch
topWidget->setFixedHeight(50);
// now put the bottom (centered) widget into its own QHBoxLayout
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addStretch(1);
hLayout->addWidget(bottomWidget);
hLayout->addStretch(1);
bottomWidget->setFixedSize(QSize(50, 50));
// now use a QVBoxLayout to lay everything out
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(topWidget);
mainLayout->addStretch(1);
mainLayout->addLayout(hLayout);
mainLayout->addStretch(1);
If you really want to have two separate layouts--one for the pink box and one for the blue box--the idea is basically the same except you'd make the blue box into its own QVBoxLayout, and then use:
mainLayout->addWidget(topWidget);
mainLayout->addLayout(bottomLayout);