Adjust widget's position and width in qtreewidget - c++

I created a Qtreewidget which has a Qtreewidgetitem is a widget (combobox, edit box and so on).
For example, this is one part of my code.cpp:
m_pPropertyTree = new QTreeWidget();
m_pPropertyTree->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_pPropertyTree->setColumnCount(2);
m_pPropertyTree->setColumnWidth(0, 155);
m_pPropertyTree->setStyleSheet("QTreeView::item { height: 20px;}");
m_pPropertyTree->setHeaderLabels(QStringList() << "Property" << "Value");
...
QTreeWidgetItem *pButtonItem = new QTreeWidgetItem(m_pPropertyTree);
pButtonItem->setText(0, "Button");
//caption
QTreeWidgetItem *pCaptionItem = new QTreeWidgetItem();
pCaptionItem->setText(0, "caption");
pCaptionItem->setText(1, "Button");
pButtonItem->addChild(pCaptionItem);
//style
QTreeWidgetItem *pStyleItem = new QTreeWidgetItem();
pStyleItem->setText(0, "style");
QComboBox *pCombobox = new QComboBox();
pCombobox->setFixedHeight(20);
pCombobox->addItem("normal");
pCombobox->addItem("bold");
pButtonItem->addChild(pStyleItem);
m_pPropertyTree->setItemWidget(pStyleItem, 1, pCombobox);
And this is what i get:
The combobox has original size which is the red rectangle. I want to adjust it's size or position to the new one, which is the black rectangle size. How can i do that? Thanks.

Related

QComboBox not aligning to right

I have a QHBoxLayout and i want to add some children to the right, and other to the left:
label = new QLabel(...);
layout->addItem(label);
layout->setAlignment(label, Qt::AlignLeft);
select = new QComboBox(...);
layout->addItem(select);
layout->setAlignment(select, Qt::AlignRight);
However select is not been aligned to the right... any idea?
Neither with QCheckBox and QLabel, but works properly using QSlider
QHBoxLayout * layout = new QHBoxLayout(this);
QLabel * label = new QLabel("this is label");
layout->addWidget(label, Qt::AlignLeft);
layout->setAlignment(label, Qt::AlignLeft);
QComboBox * select = new QComboBox(this);
layout->addWidget(select);
layout->setAlignment(select, Qt::AlignRight);

QT C++ Scroll Issues

I have an issue in locking a widget's contents to a scroll-area.
I think the best way to describe is in the pictures attached.
Picture 'a' is normal operation without a scroll area
Picture 'b' is when I attempt to add a scroll area to the widget.
The scroll- area appears but the text is not contained within.
The scroll-area is its own seperate entity with the content appearing away from it.
This is the code which I have placed in my widget:
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
scrollArea->setVisible(true);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setWidget(this);
scrollArea->setGeometry(680, 250, 560, 440);
scrollArea->setBackgroundRole(QPalette::Light);
Any ideas?
Picture a :
Picture b :
Here is the call to the custom child widget from the main GUI:
subalerPane = new subalertsPane(mstrWnd);
subalerPane->setObjectName(subalertspane_params._name);
subalerPane->setGeometry(QRect(subalertspane_params._x, subalertspane_params._y, subalertspane_params._w, subalertspane_params._h));
subalerPane->setPixmaps(QPixmap(subalertspane_params._normalImageDm), QPixmap(subalertspane_params._normalImageNm), QPixmap(subalertspane_params._minimisedImageDm), QPixmap(subalertspane_params._minimisedImageNm));
subalerPane->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
subalerPane->setAttribute(Qt::WA_TranslucentBackground);
subalerPane->setState(subalertspane_params._defaultState);
subalerPane->setUIMode(subalertspane_params._defaultUIMode);
subalerPane->setVisible(true);
subalerPane->raise();
Here is the subalertsPane cpp file:
subalertsPane::subalertsPane(QWidget *parent) :QLabel(parent)
{
subalertsPane::state=bsNormal;
subalertsPane::pressable=true;
subalertsPane::uiMode=bdnDay;
connect(this, SIGNAL(clicked()), this, SLOT(slotClicked()));
connect(this, SIGNAL(released()), this, SLOT(slotReleased()));
statbutts[0] = new statusButton(this);
statbutts[1] = new statusButton(this);
statbutts[2] = new statusButton(this);
statbutts[3] = new statusButton(this);
statbutts[4] = new statusButton(this);
statbutts[5] = new statusButton(this);
statbutts[6] = new statusButton(this);
statbutts[7] = new statusButton(this);
statbutts[8] = new statusButton(this);
statbutts[9] = new statusButton(this);
statbutts[10] = new statusButton(this);
for (int i = 0; i < 11; i++)
{
statbutts[i]->fadeIn();
statbutts[i]->setVisible(false);
}
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
scrollArea->setVisible(true);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setWidget(this);
scrollArea->setGeometry(680, 250, 560, 440);
scrollArea->setBackgroundRole(QPalette::Light);
}
QScrollArea::setWidget() is used for setting a widget inside the scroll area with the the content.
Example: if you want a QLabel with a text inside the scroll area - scrollArea->setWidget(qLabel);
Then add the scroll area to a layout of the view
auto *scrollArea = new QScrollArea(this);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setWidgetResizable(true);
auto *buttonsWidget = new QGroupBox(scrollArea);
scrollArea->setWidget(buttonsWidget);
auto *comboboxesLayout = new QVBoxLayout();
buttonsWidget->setLayout(comboboxesLayout);
mainLayout->addWidget(scrollArea);

How to scale a custom widget within a QListWidget?

I have created a custom widget that includes a vertical layout with a qlabel(that holds an icon) and a qcombobox
and I use them as listwidget items.
They are created based on a file that have 100s of icons.
I am trying to create a slider that scales the pixmap sizes when the app is running in real time.
I can't figure out how to code this properly so I can access this property.
This is the code in the mainwindow.cpp
/*load icons*/
QDir dir (....);
QFileInfoList list = dir.entryInfoList(QDir::AllEntries |
QDir::Dirs|QDir::NoDotAndDotDot);
for(int i=0 ; i < list.length() ; i++)
{
QIcon icon;
icon.addFile(list.at(i).absoluteFilePath(), QSize(), QIcon::Normal,
QIcon::Off);
mypix = icon.pixmap(QSize(128,128));
/*Custom Widget*/
widget.push_back(new QWidget(ui->listWidget));
widget[i]->setMinimumSize(QSize(0, 150));
/*the VB with of label-combo*/
layout.push_back(new QVBoxLayout(widget[i]));
/*Qlabel that holds the icon*/
pic.push_back(new QLabel (widget[i]));
pic[i]->setPixmap(mypix);
layout[i]->addWidget(pic[i]);
box.push_back(new QComboBox(widget[i]));
box[i]->addItem(list.at(i).baseName());
layout[i]->addWidget(box[i]);
QListWidgetItem * qlistwidgetitem = new QListWidgetItem;
ui->listWidget->addItem(qlistwidgetitem);
ui->listWidget->setItemWidget(ui->listWidget->item(i),widget[i]);
}
}
QListWidgetItem has a default size that does not take into account the size of the widget, the solution is to pass the sizeHint() of the widget to QListWidgetItem, also you must not set a height of 0 to the widget, only the minimum width.
QDir dir (...);
const QFileInfoList &infolist = dir.entryInfoList(QDir::AllEntries| QDir::Dirs| QDir::NoDotAndDotDot);
for(const QFileInfo &info: infolist){
QIcon icon;
icon.addFile(info.absoluteFilePath(), QSize(), QIcon::Normal, QIcon::Off);
QPixmap pix = icon.pixmap(QSize(128,128));
QWidget *w = new QWidget(ui->listWidget);
w->setMinimumWidth(150);
QVBoxLayout *lay = new QVBoxLayout(w);
QLabel *lbl = new QLabel(w);
lbl->setPixmap(pix);
QComboBox *combo = new QComboBox(w);
combo->addItem(info.baseName());
lay->addWidget(lbl);
lay->addWidget(combo);
widget << w;
layout << lay;
box << combo;
pic << lbl;
QListWidgetItem *qlistwidgetitem = new QListWidgetItem;
qlistwidgetitem->setSizeHint(w->sizeHint());
ui->listWidget->addItem(qlistwidgetitem);
ui->listWidget->setItemWidget(qlistwidgetitem, w);
}
Update:
If you want to change the size of the icon with a QSlider it is advisable to save the icon so we can use the setData() method, then we associate a slot with the slider and in that slot we insert a new size to the icon and set it to the QLabel
for(const QFileInfo &info: infolist){
[...]
qlistwidgetitem->setData(Qt::UserRole, QVariant::fromValue(icon));
[...]
}
void Widget::on_horizontalSlider_valueChanged(int value)
{
for(int i=0; i< ui->listWidget->count(); i++){
QListWidgetItem *it = ui->listWidget->item(i);
QIcon icon = it->data(Qt::UserRole).value<QIcon>();
pic[i]->setPixmap(icon.pixmap(value, value));
QWidget *w = ui->listWidget->itemWidget(it);
it->setSizeHint(w->sizeHint());
}
}
The complete example can be found in the following link.

Center children widget in parent widget(parent widget was added in layout)

I created a layout contain a parent widget. In that parent widget i created another widget.
My code is similarly to this:
QGridLayout *layout = new QGridLayout();
QWidget *parentWidget = new QWidget();
layout->addWidget(parentWidget );
QWidget *childWidget = new QWidget(parentWidget);
How can i center the child widget in parent widget ?
The problem is we cannot get the true size of parent widget because it's in a layout.
Move the child inside the parent's showeEvent. You can use a bool flag to do it only when the parent is shown for the first time.
void Parent::showEvent(QShowEvent *)
{
if(_first_show)
{
_first_show = false;
_child->move(this->rect().center() - _child->rect().center());
}
}
Proof that it works (red is the parent, and blue is the child):
You can do this by setting fixed size of child widget and placing it inside grid layout of parent widget.
QGridLayout *layout = new QGridLayout();
QWidget *parentWidget = new QWidget();
layout->addWidget(parentWidget );
QWidget *childWidget = new QWidget(parentWidget);
QGridLayout *parentLayout = new QGridLayout();
parentWidget->setLayout(parentLayout);
parentLayout->addWidget(childWidget);
childWidget->setFixedSize(/*some fixed size for child widget*/);

QListWidgetItem - adjust width and height to content

I have got a QListWidgetItem, which has a QWidget and some QLabels. The height of the labels (imageLabel, titleLabel and descriptionLabel) varies depending on the text length. So does the height of the QWidget, which leds to different sizes in QListWidgetItem. So far the parameters for setSizeHint are static:
QListWidgetItem* listWidgetItem = new QListWidgetItem();
listWidgetItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
listWidgetItem->setSizeHint(200, 180));
QWidget* widget = new QWidget();
QVBoxLayout* rootLayout = new QVBoxLayout();
rootLayout->setAlignment(Qt::AlignTop);
QHBoxLayout* contentLayout = new QHBoxLayout();
contentLayout->setAlignment(Qt::AlignLeft);
QLabel* imageLabel = new QLabel();
imageLabel->setPixmap(pixmap);
contentLayout->addWidget(imageLabel, 0, Qt::AlignTop);
QVBoxLayout* informationLayout = new QVBoxLayout();
informationLayout->setAlignment(Qt::AlignTop);
QLabel* titleLabel = new QLabel("<b>" + title + "</b>");
titleLabel->setWordWrap(true);
informationLayout->addWidget(titleLabel);
QLabel* descriptionLabel = new QLabel(description);
descriptionLabel->setWordWrap(true);
informationLayout->addWidget(descriptionLabel);
QLabel* dateLabel = new QLabel(date.toString());
informationLayout->addWidget(dateLabel);
contentLayout->addLayout(informationLayout);
rootLayout->addLayout(contentLayout);
QHBoxLayout* buttonLayout = new QHBoxLayout();
QPushButton* buttonOne = new QPushButton(tr("Button 1"));
QObject::connect(buttonOne, SIGNAL(clicked()), mButtonOneSignalMapper, SLOT(map()));
mButtonOneSignalMapper->setMapping(buttonOne, index);
buttonLayout->addWidget(buttonOne);
QPushButton* buttonTwo = new QPushButton(tr("Button 2"));
QObject::connect(buttonTwo, SIGNAL(clicked()), mButtonTwoSignalMapper, SLOT(map()));
mButtonTwoSignalMapper->setMapping(buttonTwo, index);
buttonLayout->addWidget(buttonTwo);
rootLayout->addLayout(buttonLayout);
widget->setLayout(rootLayout);
mListWidget->addItem(listWidgetItem);
mListWidget->setItemWidget(listWidgetItem, widget);
Is there any way to properly set the sizeHint regarding the width and height of the displayed content used in the QLabels of the QWidget?
For instance the first QListWidgetItem may have a descriptionLabel with a text length of 300 characters and the second QListWidgetItem may have a descriptionLabel with a text length of 1000 characters. So far both QListWidgetItems will have the same size (200px width and 180px height). While it may fit on the first QListWidgetItem, because it has only 300 characters, it may not fit on the second QListWidgetItem, because of the 1000 characters. Therefore I would like to somehow dynamically adjust the size of the QListWidgetItem regarding the needed space (first one will need less than the second one).
The way I see it, you won't be able to get a correct bounding rect for the label, unless you know it's future width, so that you can calculate the number of lines required to display the content. And you won't get the width before the layout with the other widgets is calculated.
An alternate approach might be to use an item delegate. Its sizeHint method has an option parameter with a preliminary rect, from which you can use the width and calculate the height with font metrics.
Concerning your other widgets, you could switch to a QTableWidget and put them in other columns..
The following code is not a working example .. just some clues to get you started..
class ItemDelegate : public QStyledItemDelegate
{
public:
void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
painter->save();
QStyledItemDelegate::paint(painter,option,index);
QString title = index.data(Qt::UserRole).toString();
QFont f = option.font;
painter->setFont(f);
QFontMetrics fm(f);
QRect r = option.rect;
// r = r.adjusted(0, fm.lineSpacing(), 0, 0);
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignTop|Qt::AlignLeft|Qt::TextWordWrap, title, &r);
painter->restore();
}
QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QFont f = option.font;
QRect r = option.rect;
QFontMetrics fm(f);
QString title = index.data(Qt::UserRole).toString();
QRect br = fm.boundingRect(r,Qt::AlignTop|Qt::AlignLeft | Qt::TextWordWrap,title);
return QSize(option.rect.width(),br.height());
}
};
Hope it helps,
Johannes