简体   繁体   中英

QT Maximizing widget in window

I am trying to start my app maximized with the midget also maximized inside the MainWindow. I also need the widget to respond to the resizing of the main window, tried a few things but not quite got it working correctly. Any idea?

Thanks

在此输入图像描述

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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //this->setGeometry(50,50, 1280, 768);
    setWindowState(Qt::WindowMaximized);
    //MainWindow::showMaximized();
    m_pWebView = new QWebView(this);
    //set position and size
    m_pWebView->setGeometry(0,0,this->width(), this->height());
    m_pWebView->load(QUrl("http://csm.nathan"));
    //m_pWebView->show();
}

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

Usually when you create a new project in QtCreator, you should have a "centralWidget" in your QMainWindow.

Just ensure to use a QGridLayout in it:

this->centralWidget()->setLayout(new QGridLayout);

And then add your QWebView to this widget's layout:

this->centralWidget()->layout()->addWidget(m_pWebView);

This should do the job as far as your WebView is the only one element in the grid.

An other solution is to use the QtDesigner:

  • First, add your webview or any other Widget inside your window
  • Then click on the window background
  • Then click on the "Layout in a grid" button

This will do exactly the same but be stored in the .ui file instead of your .cpp file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM