首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法更改QFrame的大小

无法更改QFrame的大小
EN

Stack Overflow用户
提问于 2017-07-23 10:42:38
回答 1查看 1.9K关注 0票数 0

mainwindow.cpp

代码语言:javascript
复制
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFormLayout>
#include <QLabel>
#include <QLineEdit>
#include <QFrame>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QFrame* loginForm = new QFrame(this);
    QFormLayout* loginFormLayout = new QFormLayout(loginForm);
    QLabel* LogInLabel = new QLabel("<h1> Login </h1>", this);
    QLabel* UsernameLabel = new QLabel("Username: ", this);
    QLabel* PasswordLabel = new QLabel("Password: ", this);
    QLineEdit* usernameLineEdit = new QLineEdit();
    QLineEdit* passwordLineEdit = new QLineEdit();

    usernameLineEdit -> setFrame(false);
    usernameLineEdit -> setMaxLength(30);
    passwordLineEdit -> setFrame(false);
    passwordLineEdit -> setMaxLength(30);
    LogInLabel -> setStyleSheet("QLabel { font-family: Arial font-weight: bold }");
    loginFormLayout -> setHorizontalSpacing(0);
    loginFormLayout -> setVerticalSpacing(0);
    loginFormLayout -> addRow(UsernameLabel, usernameLineEdit);
    loginFormLayout -> addRow(PasswordLabel, passwordLineEdit);
    loginFormLayout -> setFieldGrowthPolicy(QFormLayout::FieldsStayAtSizeHint);
    loginFormLayout -> setFormAlignment(Qt::AlignHCenter | Qt::AlignTop);
    loginForm -> setLayout(loginFormLayout);
    loginForm -> setFrameStyle(QFrame::Panel | QFrame::Plain);
    loginForm -> setFrameRect(QRect(440, 134, 400, 300));
    loginForm -> setLineWidth(20);
    this -> setCentralWidget(loginForm);
}

MainWindow::~MainWindow()
{
    delete ui;
}

我在mainwindow.h中没有改变任何东西。我想要一个QFrame,里面有一个QFormLayout,包含两行QLineEdit对。我希望整个事情是400 of宽和300 of高,在左上角的QFrame (440,134)。你能帮我找出为什么我没有达到上述结果吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-24 17:13:34

使用QMainWindow::setCentralWidget会使作为参数传递的小部件成为QMainWindow布局的主题,因此它的大小从此由布局来管理。要达到预期的结果,请执行以下操作:

  1. 使用QFrame::setFrameRect代替QFrame::setGeometry
  2. 注释掉this -> setCentralWidget(loginForm)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45264161

复制
相关文章

相似问题

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