I have a QLabel centered in a custom QGraphicsPolygonItem and I'm using a QAction to change the labels text, but when the text changes the label size doesn't change, I want it to resize to the size of the new text and keep it centered. Here's my custom item constrctor:
DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu,
QGraphicsItem *parent)
: QGraphicsPolygonItem(parent){
QGraphicsProxyWidget* pMyProxy = new QGraphicsProxyWidget(this);
QLabel *label = new QLabel();
label->setText(QString("I AM A SQARE DADADA"));
label->setTextInteractionFlags(Qt::TextEditorInteraction);
label->setStyleSheet("QLabel { background-color : red; color : blue; }");
pMyProxy->setWidget(label);
pMyProxy->setPos(this->boundingRect().center()-label->rect().center());
...
Here's the slot I'm using to change the text of the label:
void MainWindow::setItemLabel(){
if(!scene->selectedItems().isEmpty())
{
auto *item = scene->selectedItems().first();
if(!(item->childItems().isEmpty()))
{
auto proxy = static_cast<QGraphicsProxyWidget *>(item->childItems().first());
if(proxy)
{
auto label = qobject_cast<QLabel*>(proxy->widget());
if(label)
{
QDialog *diag = new QDialog(this);
QComboBox *box = new QComboBox();
QLineEdit *lt = new QLineEdit();
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);
QVBoxLayout *mainLayout = new QVBoxLayout();
connect(buttonBox, SIGNAL(accepted()), diag, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), diag, SLOT(reject()));
mainLayout->addWidget(lt);
mainLayout->addWidget(buttonBox);
diag->setLayout(mainLayout);
if(diag->exec() == QDialog::Accepted){
QString *usrInpt = new QString();
*usrInpt = lt->text();
label->rect().setWidth(usrInpt->length());
label->setText(*usrInpt);
}
}
}
}
}
Here's what I'm getting as a result, this is before triggering the slot above:
And here's what I'm getting after the slot is triggered:
This line isn't doing anything too, and I have no idea why:
label->rect().setWidth(usrInpt->length());
What am I missing?