Area of tree displayed by QTreeView not expands to its parent widget? - c++

I use QTreeView and QStandardItemModel to display its contents (read from xml file) in a tree view. The file parser works just OK, but when I use the view to display the data, the sizePolicy just doesn't work.
Why? How to make the area of tree expand?
(It shows like below, which tree view doesn't expand?)
picture of the display of the tree:
code (edited to add "layout" part, but not work, either.):
...
QStandardItemModel *model = new QStandardItemModel();
preOrder(doc.firstChild(), model);
view = new QTreeView(pageDetails);
view->setModel(model);
model->setHorizontalHeaderLabels(QStringList(""));
QHBoxLayout* lay=new QHBoxLayout(pageDetails);
lay->setContentsMargins(0, 0, 0, 0);
lay->addWidget(view);
view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
pageDetails->setLayout(lay);
view->show();

I got the problem solved now:
But still don't know how to remove the header...
The problem is caused by Qt Designer. I use it to add the parent widget ui, but set it a horizonal layout in the designer(somewhat difficult to find).
I just don't know why the code above just doesn't work in this case.
PS:
If I left the code commented:
//QHBoxLayout* lay=new QHBoxLayout(pageDetails);
//lay->setContentsMargins(0, 0, 0, 0);
//lay->addWidget(view);
//view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
//pageDetails->setLayout(lay);
It appears:
PS2:
remove header successfully!:
comment model->setHorizontalHeaderLabels(QStringList(""));
add view->header()->setVisible(false);

Related

Issue with QLayout with QStackedWidget

I am implementing a tab style UI. where tabs are shown by QListWidget and contents are shown by QStackedWidget. on every page of QStackWidget there is layout which allow to insert panel(widget) in QHBoxlayout. at every panel, there are couple of icons which are again in QHBoxLayout. Below is ideal case which I wanted to implement.
But on other page of QStackWidget this is not the case (with less icons) as below
I want to remove extra space ( or align icon to left to eliminate extra space among icons on panels)
I tried spacer, then this happened :(
please help me to correct this thing. My spacer code is as
inline QSpacerItem * buildSpacer(Qt::Orientation orientation)
{
QSpacerItem * pSpacer = nullptr;
if (orientation == Qt::Horizontal)
{
pSpacer = new QSpacerItem(1000, UNIT_VALUE, QSizePolicy::Expanding, QSizePolicy::Minimum);
}
else
{
pSpacer = new QSpacerItem(UNIT_VALUE, 1000, QSizePolicy::Minimum, QSizePolicy::Expanding);
}
return pSpacer;
}
Note
I donot want to use QTabWidget. By the way this issue is also with QTabWidget
Why not using QTabWidget in the first place?
Anyway, instead of creating the QSpacerItem by yourself, you should use addStretch():
my_layout->addWidget(new Widget("widget1"));
my_layout->addWidget(new Widget("widget2"));
my_layout->addStretch(1); // will "eat" extra space

Add QMdiSubWindow to current QStackedLayout

So I have this code:
QStackedLayout *layout = new QStackedLayout;
QMdiArea *mdi1 = new QMdiArea;
mdi1->addSubWindow(new QMdiSubWindow);
layout->addWidget(mdi1);
QMdiArea *a = (QMdiArea *) layout->currentWidget();
a->addSubWindow(new QMdiSubWindow);
Which for some reason doesn't work. What I want to do is get the widget that is being displayed in layout - as a QMdiArea, then add a sub window to it.
P.S. this is a simplified version on the full app. adding a sub window directly to mdi1 will work but it is NOT what I'm looking for (as there are many QMdiArea's in the QStackedLayout).
So the answer was that I needed to use QStackedWidget instead of QStackedLayout.

drop shadow effect on QPushButton text

How do I set a drop-shadow effect on a QPushButton text?
I could set shadow on the entire QPushButton using QGraphicsDropShadowEffect, I however, am not able to find a way to directly set the effect of text inside the QButton.
Edit:
I tried the following, not sure if the syntax is correct
in the .qss file:
MyButton::text
{
shadow: palette(dark);
}
I set the button's drop shadow effect by:
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect( );
effect->setBlurRadius( 5 );
mStartButton->setGraphicsEffect( effect );
Try this:
Set a QLabel iside QPushButton rather than simple text. Then apply shadow effect to the label.
You may need to add extra code for centering the label inside the pushbutton.
mStartButton->setText("");
QLabel *label = new QLabel(mStartButton);
label->setText("<b>Button</b>");
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect( );
effect->setBlurRadius( 5 );
label ->setGraphicsEffect( effect );

Is it possible to partially italicize the text of a QTreeWidgetItem?

I know how to italicize the entire text of a QTreeWidgetItem:
QTreeWidgetItem* elt = new QTreeWidgetItem(item);
QFont font = elt->font(0);
font.setItalic(true);
elt->setText(0, choice);
elt->setFont(0, font);
But is it possible to italicize only part of that text? (e.g. the first one or two words)
It's possible with:
QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
QLabel *label = new QLabel("<i>italics</i>, <b>bold</b>, normal", treeWidget);
treeWidget->setItemWidget(item, 0, label);
but cleaner solution could be by using QTreeView and subclassing QItemDelegate.
There is no such option by default. You need to set a QItemDelegate that is able to render HTML. See this solution.

Modify a tab in a QTabWidget where each tab represents a QTableView

I have a tab widget where every tab is a QTableView. I would like to be able to pass an updated model (QModelIndex) into each tab whenever the contents of that tab need to change.
The alternative (and nastier way) is for me to delete all the tabs, and then recreate them.
I know I can get the widget in the tab by doing something like:
tabWidget->widget(i);
This will return a widget, which is really a QTableView, but I want to update the model that is in that widget without having to delete and recreate the tab.
Thank you!
P.S. This is my current attempt...
for (int i = 0; i < tableView.size(); i++)
{
tabWidget->setCurrentWidget(tableView.at(i));
QTableView* updatedTable = (QTableView*)tabWidget->currentWidget();
updatedTable->setModel(dataModel);
tableView.replace(i, updatedTable);
}
It's not clear why you can't keep the QTableView widget and just change the model, as in your code. Doesn't the view refresh without this tableView.replace thing?
There doesn't appear to be a direct API for replacing the widget you put in with addTab() without going through a tab removal step. But instead of inserting the QTableView directly, you could instead call addTab() on a dummy widget that has a layout in it with a single item. A QStackedLayout, for instance:
QWidget* dummy = new QWidget;
QStackedLayout stackedLayout = new QStackedLayout;
stackedLayout->addWidget(tableView);
dummy->setLayout(stackedLayout);
tabWidget->addTab(dummy);
Then later, when you want to replace the tableView with a new one:
QWidget* dummy = tabWidget->currentWidget();
QStackedLayout newStackedLayout = new QStackedLayout;
newStackedLayout->addWidget(newTableView);
delete dummy->layout();
dummy->setLayout(newStackedLayout);
I still wonder what this is buying you that reusing the old table view couldn't do.