首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >插入间隔项后,将小部件保持在中心位置。

插入间隔项后,将小部件保持在中心位置。
EN

Stack Overflow用户
提问于 2022-03-17 05:32:50
回答 1查看 24关注 0票数 0

我想定制一个标题栏:

代码语言:javascript
复制
class TitlebarWidget : public QWidget {
    Q_OBJECT
   public:
    TitlebarWidget(QWidget* parent = nullptr): QWidget(parent) {
        setupUi();
    }
    virtual ~TitlebarWidget();

   private:
    void setupUi(){
        QHBoxLayout* layout = new QHBoxLayout(this);
        layout->setSpacing(0);
        layout->setContentsMargins(0, 0, 0, 0);
  //    layout->addSpacerItem(new QSpacerItem(width(), height(),     
        QSizePolicy::Fixed, QSizePolicy::Fixed));

        m_homeButton = new QPushButton(this);
        m_homeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        m_homeButton->setMinimumWidth(50);
        m_homeButton->setMaximumWidth(50);
        m_homeButton->setText(tr("Home"));
        layout->addWidget(m_homeButton);

        QLabel* label = new QLabel(this);
        label->setText("Titlebar");
        layout->addWidget(label, 0, Qt::AlignCenter);

        m_synButton = new QPushButton(this);
        m_synButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        m_synButton->setMinimumWidth(100);
        m_synButton->setMaximumWidth(100);
        m_homeButton->setText(tr("Sync"));
    }
    QPushButton* m_synButton;
    QPushButton* m_homeButton;
    QPushButton* m_settingButton;
    QRCodeWidget* m_synPhoneWidget;
};

标题栏如下:

Home按钮被左上角的三个圆圈所覆盖,所以我在开头插入一个间隔项:

代码语言:javascript
复制
layout->addSpacerItem(new QSpacerItem(width(), height(), QSizePolicy::Fixed, QSizePolicy::Fixed));

这个标题栏就像:

我发现“标题栏”显然偏离了中心,但我想把它保持在中心位置。谁能帮我找到解决办法或者更好的选择?

EN

回答 1

Stack Overflow用户

发布于 2022-03-17 14:10:21

使用layout->addSpacing()代替,addStretch为我工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71507481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档