我是 Qt 的新手并正在试验它。我有一个布局,其代码如下:
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
QVBoxLayout *parentLayout = new QVBoxLayout(this);//MainWindow is a QWidget
this->setStyleSheet("background-color:red");
for(int i=0;i<3;i++){
QHBoxLayout* labelLineEdit = f1();
parentLayout->addLayout(labelLineEdit);
}
parentLayout->setContentsMargins(0,0,40,0);
}
QHBoxLayout* MainWindow::f1()
{
QHBoxLayout *layout = new QHBoxLayout;
QLabel *label = new QLabel("Movie");
label->setStyleSheet("background-color:blue;color:white");
label->setMinimumWidth(300);
label->setMaximumWidth(300);
layout->addWidget(label);
QLineEdit *echoLineEdit = new QLineEdit;
//echoLineEdit->setMaximumWidth(120);//line:99
echoLineEdit->setMaximumHeight(50);
echoLineEdit->setMinimumHeight(50);
echoLineEdit->setStyleSheet("background-color:brown");
layout->addWidget(echoLineEdit);
layout->setSpacing(0);
return layout;
}
我希望我的 lineedit 宽度减小,所以我取消了第 99 行的注释,我的输出如下所示。
在这种情况下,setspacing 和 setContentsMargins 属性不起作用。我哪里错了。Anyhelp 将非常有用。