layout-> addWidget()导致showFullScreen()出现问题

时间:2018-06-18 15:38:20

标签: qt5 fullscreen qvboxlayout

我在一个继承QMainWindow的类中有一个QDockWidget。这是代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDockWidget>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QRadioButton>
#include <QPushButton>
#include <QCheckBox>
#include <QGroupBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    createDock();

    this->showMaximized();
}

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

void MainWindow::createDock()
{
    //initialize main layout of dock
    layout = new QVBoxLayout;

    //initialize dock
    QDockWidget *dock = new QDockWidget("Filters", this);
    dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    dock->setFeatures(QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);
    //dock->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);

    //make group boxes
    QWidget *filters = new QWidget(this);

    //only show book which are not returned yet
    QCheckBox *notReturnedOnly = new QCheckBox("Only Show books which are not returned");
    layout->addWidget(notReturnedOnly);

    createHistory();
    createIssuedBy();
    createBookIssued();
    createClassGroup();
    createRack();
    createPublisher();

    //resetFilters
    QPushButton *resetFilters = new QPushButton("Reset Filters");
    layout->addWidget(resetFilters, 0, Qt::AlignCenter);

    filters->setLayout(layout);
    dock->setWidget(filters);

    this->addDockWidget(Qt::RightDockWidgetArea, dock);
    dock->show();
}

void MainWindow::createHistory()
{
    //history group box
    QGroupBox *history = new QGroupBox("History");

    QRadioButton *today = new QRadioButton("Today");
    QRadioButton *thisWeek = new QRadioButton("This week");
    QRadioButton *thisMonth = new QRadioButton("This Month");

    //set auto exclusive
    today->setAutoExclusive(false);
    thisWeek->setAutoExclusive(false);
    thisMonth->setAutoExclusive(false);

    //add widgets to history
    QVBoxLayout *historyLayout = new QVBoxLayout;
    historyLayout->addWidget(today);
    historyLayout->addWidget(thisWeek);
    historyLayout->addWidget(thisMonth);
    history->setLayout(historyLayout);

    layout->addWidget(history);
}

void MainWindow::createIssuedBy()
{
    //Issued by group box
    QGroupBox *issuedBy = new QGroupBox("Issued by");

    QLineEdit *student = new QLineEdit;
    QHBoxLayout *issuedByLayout = new QHBoxLayout;
    issuedByLayout->addWidget(student);
    issuedBy->setLayout(issuedByLayout);

    layout->addWidget(issuedBy);
}

void MainWindow::createBookIssued()
{
    //book issued group box
    QGroupBox *bookIssued = new QGroupBox("Book Issued");

    QLineEdit *book = new QLineEdit;
    QHBoxLayout *bookIssuedLayout = new QHBoxLayout;
    bookIssuedLayout->addWidget(book);
    bookIssued->setLayout(bookIssuedLayout);

    layout->addWidget(bookIssued);
}

void MainWindow :: createClassGroup()
{
    QGroupBox *classFilter = new QGroupBox("Class");

    QLineEdit *Class = new QLineEdit;
    QHBoxLayout * classFilterLayout = new QHBoxLayout;
    classFilterLayout->addWidget(Class);
    classFilter->setLayout(classFilterLayout);

    layout->addWidget(classFilter);
}

void MainWindow :: createPublisher()
{
    QGroupBox *publisherFilter = new QGroupBox("Publisher");

    QLineEdit *publisher = new QLineEdit;
    QHBoxLayout *publisherLayout = new QHBoxLayout;
    publisherLayout->addWidget(publisher);
    publisherFilter->setLayout(publisherLayout);

    layout->addWidget(publisherFilter);
}

void MainWindow :: createRack()
{
    QGroupBox *rackFilter = new QGroupBox("Rack Number");

    QLineEdit *rack = new QLineEdit;
    QHBoxLayout *rackLayout = new QHBoxLayout;
    rackLayout->addWidget(rack);
    rackFilter->setLayout(rackLayout);

    layout->addWidget(rackFilter);
}

代码编译时没有错误,并且无异常地运行。事实上即使使用showMaximized()显示Mainwindow,输出也是这样的:screenshot

如您所见,应用程序不是全屏。它上面没有最大化按钮。

有趣的是,如果我从Dock中删除一个随机小部件,则窗口会完美显示。但对于我的应用程序,我需要立即显示所有小部件,并使用全屏幕的窗口。

1 个答案:

答案 0 :(得分:0)

我找到了路。我将所有行编辑都放在一个组框中,一切正常