setSpacing不适用于QHBoxLayout吗?

时间:2018-07-30 14:35:55

标签: css qt qpushbutton qlineedit qlayout

我需要创建一个像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 );

但是我得到的元素看起来像这样。下侧的两个基本元件之间的距离很小,而上侧的距离没有。我尝试使用Qt Designer进行测试,结果是相同的。 我的setStyleSheet是否出错?

enter image description here

1 个答案:

答案 0 :(得分:0)

好的,我对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);}");