Qt WindowMaximize 不改变几何形状 (C++)
因此,我正在制作一个 Web 浏览器作为我的第一个 Qt 项目(惊讶!),我想知道为什么调用 setWindowState(Qt::WindowMaximized) 不会更改窗口几何形状。我有这样的代码:
来自 mainwindow.h:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
Ui::MainWindow *ui;
};
来自 mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// this->geometry() is the same here...
setWindowState(Qt::WindowMaximized);
ui->webView->setGeometry(0, 60, geometry().width(), geometry().height()-60);
// ...as it is here.
}
正如您可能知道的那样,我试图在窗口最大化且 QWebView 也最大化的情况下启动应用程序。基本上,每当调整主窗口大小时,我也想调用 ui->webView->setGeometry
来更新高度和宽度。但 MainWindow::geometry
似乎没有更新。我做错了什么?
So I'm making a web browser as my first Qt project (surprise!) and I'm wondering why calling setWindowState(Qt::WindowMaximized)
is not changing window geometry. I have this code:
From mainwindow.h:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
Ui::MainWindow *ui;
};
From mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// this->geometry() is the same here...
setWindowState(Qt::WindowMaximized);
ui->webView->setGeometry(0, 60, geometry().width(), geometry().height()-60);
// ...as it is here.
}
As you may be able to tell, I'm trying to start the application with the window maximized and the QWebView also maximized. Basically, whenever the main window is resized, I also want to call ui->webView->setGeometry
with the update height and width. But MainWindow::geometry
doesn't seem to be updating. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须仔细检查,但在主窗口获得显示事件之前,您的几何图形可能无法正确更新。
但是,我建议您将 QWebView 放在布局中,而不是在每次主窗口更改大小时尝试手动调整其大小。
I would have to double check, but your geometry might not get updated properly until your main window gets a show event.
However, I would suggest you put your QWebView inside of a layout instead of trying to size it manually every time your main window changes size.