【问题标题】:setSpacing does not work for QHBoxLayout?setSpacing 不适用于 QHBoxLayout?
【发布时间】:2018-07-30 14:35:54
【问题描述】:

我需要创建一个像 ComboBox 这样的元素,它是 LineEdit 和 Button 的组合。

QHBoxLayout *lineEditWithButtonForm = new QHBoxLayout( this );
lineEditWithButtonForm->setSpacing( 0 );
lineEditWithButtonForm->setContentsMargins( 0, 0, 0, 0 );
m_lineEdit = new LineEdit;
m_lineEdit->setFixedHeight( 25 );
m_lineEdit->setStyleSheet( "QLineEdit{ padding-left: 10px; padding-right: 10px; border-width: 1px; border-style: solid; border-color: rgb(204,204,204) white rgb(204,204,204) rgb(204,204,204); }");
lineEditWithButtonForm->addWidget( m_lineEdit );

m_button = new Button;
m_button->setFixedSize( QSize( 40, 25 ) );
m_button->setText("Button");
m_button->setCursor( QCursor( Qt::PointingHandCursor ) );
m_button->setFocusPolicy( Qt::ClickFocus );
m_button->setStyleSheet( "QAbstractButton{ border-width: 1px; border-style: solid; border-color: rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) white;}");
lineEditWithButtonForm->addWidget( m_button );

但我得到的元素看起来像这样。下侧2个基本元素之间的距离很小,而上侧没有距离。我尝试用 Qt Designer 测试它,结果是一样的。 我的 setStyleSheet 有错误吗?

【问题讨论】:

    标签: css qt qpushbutton qlineedit qlayout


    【解决方案1】:

    好的,我的 setStyleSheet 有错误。应该是:

    m_lineEdit->setStyleSheet( "QLineEdit{ padding-left: 10px; padding-right: 10px; border-width: 1px 0px 1px 1px; border-style: solid; border-color: rgb(204,204,204); }");
    
    m_button->setStyleSheet( "QAbstractButton{ border-width: 1px 1px 1px 0px; border-style: solid; border-color: rgb(204,204,204);}");
    

    【讨论】:

      猜你喜欢
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 2012-09-20
      • 1970-01-01
      • 2011-06-08
      • 2014-12-25
      相关资源
      最近更新 更多