c++ - QLabel 更改文本后未调整大小

标签 c++ qt qt5 qgraphicsitem qlabel

我有一个以自定义 QGraphicsPolygonItem 为中心的 QLabel,我正在使用 QAction 来更改标签文本,但是当文本更改时标签大小不会改变,我希望将其调整为新文本的大小并保持其居中。这是我的自定义项目构造函数:

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());
...

这是我用来更改标签文本的插槽:

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);
                }
            }
        }
    }
}

这是我得到的结果,这是在触发上面的插槽之前: BeforeSlotTriggere 这是触发插槽后我得到的结果: AfterSlotTriggered

这条线也没有做任何事情,我不知道为什么:

label->rect().setWidth(usrInpt->length());

我错过了什么?

最佳答案

您可以使用 adjustSize() 调整 QLabel 的大小,但即使如此,QGraphicsProxyWidget 也不会更改其大小,从而导致问题要坚持,要纠正,我们必须覆盖 boundingRect() 并返回适当的大小,除此之外,如果您正在创建一个内部包含标签的类,那么您设置文本的方式并不合适您可以创建一个 setText() 方法来更新 QLabel,而无需编写大量代码:

class GraphicsProxyWidget: public QGraphicsProxyWidget{
public:
    using QGraphicsProxyWidget::QGraphicsProxyWidget;
    QRectF boundingRect() const{
        if(widget())
            return QRectF(widget()->rect());
        return QGraphicsProxyWidget::boundingRect();
    }
};

class DiagramItem: public QGraphicsPolygonItem{
    QLabel *label;
    GraphicsProxyWidget *pMyProxy ;
public:
    explicit DiagramItem(DiagramType diagramType, QMenu *contextMenu, QGraphicsItem *parent=nullptr):QGraphicsPolygonItem(parent) {
        label = new QLabel;
        pMyProxy = new GraphicsProxyWidget(this);
        pMyProxy->setWidget(label);
        label->setTextInteractionFlags(Qt::TextEditorInteraction);
        label->setStyleSheet("QLabel { background-color : red; color : blue; }");
        setText("I AM A SQARE DADADA");
        ...
    }
    void setText(const QString & text){
       label->setText(text);
       label->adjustSize();
       pMyProxy->setPos(boundingRect().center()-label->rect().center());
    }
};

然后 setItemLabel 方法将如下所示:

void MainWindow::setItemLabel(){
    if(!scene->selectedItems().isEmpty())
    {
        auto *item = scene->selectedItems().first();
        DiagramItem *it = static_cast<DiagramItem *>(item);
        if(it){
            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){
                it->setText(lt->text())
            }
        }
    }
}

示例:

#include <QApplication>
#include <QGraphicsProxyWidget>
#include <QGraphicsView>
#include <QLabel>
#include <QTimer>

class GraphicsProxyWidget: public QGraphicsProxyWidget{
public:
    using QGraphicsProxyWidget::QGraphicsProxyWidget;
    QRectF boundingRect() const{
        if(widget())
            return QRectF(widget()->rect());
        return QGraphicsProxyWidget::boundingRect();
    }
};

class DiagramItem: public QGraphicsPolygonItem{
    QLabel *label;
    GraphicsProxyWidget *pMyProxy ;
public:
    explicit DiagramItem(QGraphicsItem *parent=nullptr):QGraphicsPolygonItem(parent) {
        label = new QLabel;
        pMyProxy = new GraphicsProxyWidget(this);
        pMyProxy->setWidget(label);
        label->setTextInteractionFlags(Qt::TextEditorInteraction);
        label->setStyleSheet("QLabel { background-color : red; color : blue; }");
        setText(QString("I AM A SQARE DADADA"));
        setBrush(Qt::green);
    }
    void setText(const QString & text){
       label->setText(text);
       label->adjustSize();
       pMyProxy->setPos(boundingRect().center()-label->rect().center());
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsView w;
    QGraphicsScene *scene = new QGraphicsScene;
    w.setScene(scene);
    auto it = new DiagramItem;
    QPolygonF myPolygon({QPointF(-120, -80), QPointF(-70, 80),
                         QPointF(120, 80), QPointF(70, -80),
                         QPointF(-120, -80)});
    it->setPolygon(myPolygon);
    scene->addItem(it);
    QTimer::singleShot(1000, [it](){
        it->setText("some text");
    });
    w.show();
    return a.exec();
}

enter image description here

enter image description here

关于c++ - QLabel 更改文本后未调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49910514/

相关文章:

C++ 服务器页面

c++ - Qt:mouseMoveEvent并干扰子对象的hoverEnterEvent

qt - Qml 和模糊图像

c++ - gstreamer 代码无法链接所有 GST 元素

c++ - QFuture<void> 检测异常

c++ - fstream C++ 的相对路径

c++ - 需要帮助足球模拟

c++ - 计算一点到另一点的距离

c++ - 使用自定义项目类的 QGraphicsScene::removeItem() 后崩溃

c++ - windeployqt 不会为调试应用程序部署 qwindowsd.dll