setSpacing 不适用于 QHBoxLayout?
setSpacing does not work for QHBoxLayout?
我需要创建一个像 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 有错误吗?
好的,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);}");
我需要创建一个像 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 有错误吗?
好的,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);}");