I am using a QTableWidget with 4-5 columns in a dialog. The dialog is resizable, I want table widget columns to resize according to dialog size i.e. if I increase dialog width, columns which are initially set with large width should expand more than the columns which were set with less width.
In short, I want relative resizing like column1 should occupy 20%, column2 occupy 50% of my table width (which increases with dialog width) and so on.
How this can be achieved for QTableWidget in Qt ?
Any solution, pointers or hints would be very helpful.
It should just be a matter of updating the column widths whenever your dialog resizes.
MyDialog::resizeEvent(QResizeEvent *event) {
int width = ui->tableWidget->size().width();
ui->tableWidget->setColumnWidth(0, width * .2);
ui->tableWidget->setColumnWidth(1, width * .5);
...
}
You could also subclass QTableWidget directly and do this same thing.
Related
I am trying to set the minimum height of the rows in the QComboBox dropdown menu without changing their width or the size of the QComboBox itself.
By default the width and height of the row items are calculated by its data. To make the dropdown list resize to the width of the data I am calling
view()->setMinimumWidth(view()->sizeHintForColumn(0);
in the combobox's ::showPopup().
However, since I have different font sizes in rows, I would like to enforce a minimum height on each row.
I tried to use setSizeHint on each QStandardItem that is added in the first column to the rows but without success. I tried:
item->setSizeHint(QSize(item->sizeHint().width(), minHeight));
(https://stackoverflow.com/a/10749345/1981832)
But this lead to the dropdown menu not resizing to the data's width.
So, I tried to leave the width "unset". However, both
item->setSizeHint(QSize(QSize().width(), minHeight));
and
item->setSizeHint(QSize(0, minHeight));
did not work. Does anyone have an idea how I can enforce a minimum height on the rows of the QComboBox without messing up the automatic width calculation?
Just use this lines:
QComboBox combo;
QListView *view = new QListView(&combo);
view->setStyleSheet("QListView::item{height: 100px}");
combo.setView(view);
Or write this code in qss file:
QListView::item {
height: 30px;
}
After that use:
QComboBox::setView(QAbstractItemView *itemView)
I need a FlexTable in GWT where I can add items at run time. The FlexTable is inside a ScrollPanel, so that when horizontal content exceeds display area a scroll bar appears.
The problem is when there are 2-3 columns they are spaced widely but as column count increases, the cell takes the minimum required area of 100px.
I need to ensure that Cell always takes 100px only. I have been doing following but no success.
myflextable.getCellFormatter().setWidth(0, col, "100px");
Please try with getFlexCellFormatter().setWidth() method.
Please check also that have you given flextable width to 100%.
The problem may be that the flextable is set to be too wide initially. It will fill the scrollpanel by default, and then as a result the columns will stretch to their maximum width.
You should set the width of the flextable explicitly in the beginning, and it will automatically resize correctly when more columns are added at run time.
Here is an example for when you have initially one row and two 100px columns:
ft = new FlexTable();
ft.addStyleName("flextable");
ft.setWidth("200px");
for (int x = 0; x < 2; x++) {
ft.setText(x, 0, "cell");
ft.getCellFormatter().addStyleName(x, 0, "testcell");
}
scrollpanel.add(ft);
and your css should say this:
.flextable {
table-layout: fixed;
}
.testcell {
width : 100px;
}
Note that the flextable is initially set to 200px width, as we start with two columns.
In the CSS file, the table-layout attribute must be set otherwise the columns will get smaller than 100px as you add more (until the cells reach their minimum width, defined by their content).
Also, remember to add the stylename "testcell" to any new cells that you add at runtime.
I have a datagrid that is updated periodically and the number of rows inside it grows steadily over time. It is inside of a parent div with a height of 60% of the screen.
If I set autoheight to, say, 5 rows, the table works properly. When a sixth row is added, a scrollbar appears within the datagrid and I can scroll up/down and the headers remain fixed at the top and visible. Unfortunately, once I have a lot of data, this is a waste of space -- I have 60% of the screen's height to work with, but only 5 rows are being shown at a time.
If I set autoheight to false, the scrollbar that appears is attached to the parent div. Scrolling up/down allows me to see the data, but the headers at the top of the grid scroll out of view.
Is there a way to ask the datagrid to show as many rows as it can fit and provide a scrollbar for the rest?
---- EDIT ----
Setting autoheight to false would be exactly what I want if the datagrid would resize itself along with the parent when I resize my browser. Is there a good way to achieve that?
I think you're looking for grid.resize(); If you look at the file "Grid.js.uncompressed.js" in the dojox nightly files here: http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/grid/ You can see exactly what it does. The dataGrid should inherit this method, if you're using it. One way to use it is to resize the containing div based on the window's height, and then resize the grid inside:
function changeHeight() {
document.getElementById("div Id in which the dojo grid is there").style.height ="your desired height";
dijit.byId('Id of the dojo grid').resize();
dijit.byId('Id of the dojo grid').update();
}
Another option method is do do something like this:
function resizeGrid() {
// do whatever you need here, e.g.:
myGrid.resize();
myGrid.update();
}
dojo.addOnLoad(function() {
dojo.connect(window, "onresize", resizeGrid);
});
I have a QTableWidget, QTextEdit, and cancel & okay buttons.
I want these widgets to stay in the same position relative to each other, and the QTableWidget to expand if the dialog window is expanded or size changed...
How can I do that?
You need to look into Qt's layout system - using layouts will handle automatically resizing your objects based on the size of their parent.
A combination of using QWidget::setSizePolicy() and QBoxLayout::setStretch() (or more likely QBoxLayout::insertWidget(..., int stretch = 0, ...)) will allow you to acheive the behaviour you refer to where only certain objects expand to fill available space, while others remain a constant size.
Addressing the image you've given above as an example:
Aside from dragging and dropping objects into the form, to achieve this solution I have:
Set the vertical sizePolicy of textEdit to Fixed.
Set a height in textEdit's minimumSize for the sizePolicy to use.
Set layoutStretch in centralWidget to 1,0, i.e. assign the minimum possible space for the elements contained in horizontalLayout and give any remaining space to tableWidget.
I'm using a Table View.
I'd like to remove the cell padding (or margin) so I can squeeze more cells in less space. How can this be achieved?
The cells size is set to 32 pixels on QT designer, if I set it smaller, the cells contents don't show and an ellipsis appears. (...)
alt text http://img692.imageshack.us/img692/3484/tableviewpng.png
Recommend you to use this code:
QTableView *tableView = new QTableView(this);
tableView->setModel(model_);
QHeaderView *verticalHeader = tableView->verticalHeader();
verticalHeader->setDefaultSectionSize(verticalHeader->fontMetrics().height()+2);
// or ...
QHeaderView *horizontalHeader = tableView->horizontalHeader();
horizontalHeader->setStretchLastSection(false);
horizontalHeader->resizeSection(/* your personal height */);
PS: Also I have noticed, that if in tableView too much rows or columns, for example about 20K rows or more, this functions resizeSection() may be too slow...