简体   繁体   English

在不同屏幕之间切换

[英]Switching between different screens

I have an application with a few screens that I would like to load on application launch and keep in memory while the program is running. 我有一个带有几个屏幕的应用程序,我希望在应用程序启动时加载它们,并在程序运行时保留在内存中。 I want to be able to switch between screens on a button click, hiding the current screen while showing the next screen. 我希望能够通过单击按钮在屏幕之间切换,从而在显示下一个屏幕时隐藏当前屏幕。 There is not a direct path through the application, the user may go back and forth through screens in the application. 没有通过应用程序的直接路径,用户可以在应用程序中的屏幕之间来回走动。 I'm using Qt and new to c++. 我正在使用Qt和C ++新手。

The following is skeleton code I would like to get to work (or get advice about a better way to do it). 以下是我想开始工作的基本代码(或获得有关更好方法的建议)。 I'm doing it this way in order to avoid memory leaks from creating new instances of the dialogs when moving back and forth through the application a number of times. 我这样做是为了避免多次在应用程序中来回移动时由于创建新对话框实例而导致内存泄漏。

When I compile this, I get an error Undefined Reference to 'Screen2' in Screen2->show(); 编译此代码时,在Screen2-> show();中出现错误未定义的对“ Screen2”的引用。 in Dialog1::on_pushButton_clicked() in dialog1.cpp. 在dialog1.cpp中的Dialog1 :: on_pushButton_clicked()中。 I tried everything I could to get this to compile without success. 我尽一切努力使它编译成功。 Why isn't Screen2 a valid pointer in dialog1.cpp? 为什么Screen2在dialog1.cpp中不是有效的指针?

I have a Manager which is run from Main and initialises the dialogs. 我有一个从Main运行的Manager,并初始化对话框。

manager.cpp: manager.cpp:

#include "manager.h"
#include "ui_manager.h"

Manager::Manager(QWidget *parent) :
QDialog(parent),
ui(new Ui::Manager)
{
    ui->setupUi(this);
    Screen1 = new Dialog1(this);
    Screen2 = new Dialog2(this);
    Screen1->show();
}

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

manager.h: manager.h:

#ifndef MANAGER_H
#define MANAGER_H
#include <QDialog>
#include "dialog1.h"
#include "dialog2.h"

namespace Ui {
    class Manager;
}

class Manager : public QDialog
{
    Q_OBJECT

public:
    explicit Manager(QWidget *parent = 0);
    ~Manager();

    Dialog1 *Screen1;
    Dialog2 *Screen2;

private:
    Ui::Manager *ui;
};

#endif // MANAGER_H

dialog1.cpp: dialog1.cpp:

#include "dialog1.h"
#include "ui_dialog1.h"
#include "manager.h"
#include "dialog2.h"
#include <QDebug>

extern Dialog1 *Screen1;
extern Dialog2 *Screen2;

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

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

void Dialog1::on_pushButton_clicked()
{
    Screen2->show();
}

dialog1.h: dialog1.h:

#ifndef DIALOG1_H
#define DIALOG1_H

#include <QDialog>
#include "dialog2.h"

class Manager;

namespace Ui {
    class Dialog1;
}

class Dialog1 : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog1(QWidget *parent = 0);
    ~Dialog1();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Dialog1 *ui;
};

#endif // DIALOG1_H

dialog2.cpp: dialog2.cpp:

#include "dialog2.h"
#include "ui_dialog2.h"
#include "dialog1.h"
#include "manager.h"

extern Dialog1 *Screen1;
extern Dialog2 *Screen2;

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

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

void Dialog2::on_pushButton_clicked()
{
    Screen1->show();
}

All help appreciated! 所有帮助表示赞赏!

Regards, James 问候,詹姆斯

I'll explain you easiest way to get it work. 我将向您解释使它工作的最简单方法。

  1. Make screen1 and screen2 private members of manager class. 将screen1和screen2设置为经理类的私有成员。
  2. Add signal buttonPressed() to both screens. 将信号buttonPressed()添加到两个屏幕。
  3. Emit signal on button press in each dialog. 在每个对话框中按下按钮时发出信号。
  4. Add slot to manager class to handle button press in dialogs 将插槽添加到管理器类以处理对话框中的按钮按下
  5. connect signals of dialogs to manager slot 将对话框的信号连接到管理器插槽
  6. Remove this in both dialogs: 在两个对话框中将其删除:

     extern Dialog1 *Screen1; extern Dialog2 *Screen2; 

You can not access them directly because thee are members of your manager object. 您不能直接访问它们,因为它们是您的manager对象的成员。

As a result you will be able to add as many screens as you want. 结果,您将可以根据需要添加任意数量的屏幕。 Each screen won't know anything about others (you were trying to do so). 每个屏幕都不了解其他屏幕(您正在尝试这样做)。

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

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