简体   繁体   English

QT5 Widgets 大小切换问题

[英]QT5 Switching between Widgets size problem

I try to switch between two different Widgets in QT5.我尝试在 QT5 中的两个不同 Widget 之间切换。 The first problem was that my first window was visible in the background of my second window.第一个问题是我的第一个 window 在我的第二个 window 的背景中可见。 I solve this with a check in "autoFillBackground".我通过检查“autoFillBackground”来解决这个问题。 Now i can switch between them, but if i resize them it only resize the Main content.现在我可以在它们之间切换,但如果我调整它们的大小,它只会调整主要内容的大小。

Both Widgets have a grid layout.两个小部件都有网格布局。

Resize Problem调整大小问题

Im new at QT5, so is there a better way to make a switching between 2 widgets without this problem?我是 QT5 的新手,那么有没有更好的方法可以在没有这个问题的情况下在 2 个小部件之间进行切换?

I try it with wMessages = new Messages();我尝试使用wMessages = new Messages(); and hide() but then my program crash after going back.hide()但是我的程序在返回后崩溃了。


Code:代码:

mainwindow.h:主窗口.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "messages.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_btnMessages_clicked();

private:
    Ui::MainWindow *ui;
    Messages *wMessages;
};
#endif // MAINWINDOW_H

mainwindow.cpp:主窗口.cpp:

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

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

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

void MainWindow::on_btnMessages_clicked()
{
    wMessages = new Messages(this); 
    wMessages->show();
}

messages.h:消息.h:

#ifndef MESSAGES_H
#define MESSAGES_H

#include <QWidget>

namespace Ui {
class Messages;
}

class Messages : public QWidget
{
    Q_OBJECT

public:
    explicit Messages(QWidget *parent = nullptr);
    ~Messages();

    QString NewsGenerator();
private slots:
    void on_bBack_clicked();

    void on_pushButton_clicked();

private:
    Ui::Messages *ui;
};

#endif // MESSAGES_H

messages.cpp:消息.cpp:

#include "messages.h"
#include "ui_messages.h"

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

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

void Messages::on_bBack_clicked()
{
    this->close();
    QWidget *parent = this->parentWidget();
    parent->show();
}

Edit 1 - working Main Window Button (from GM´s answer):编辑 1 - 工作主 Window 按钮(来自 GM 的回答):

void MainWindow::on_btnMessages_clicked()
{
    wPlaner = new Planer();
    QMainWindow::setCentralWidget(wPlaner);
}

Update (based on the comments):更新(基于评论):

The best solution was to use the QStackedWidget to navigate between the views.最好的解决方案是使用QStackedWidget在视图之间导航。

Docu: https://doc.qt.io/qt-5/qstackedwidget.html#details文档 https://doc.qt.io/qt-5/qstackedwidget.html#details


Example-Code for Button:按钮的示例代码:

// return back to first view
void MainWindow::on_btnret_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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