开发者

Qt Dialog Window Opens in Same Window

I managed to get a QPushButton to open a new window when pressed by using the following code (just snippets of code):

AppDialog::AppDialog(QWidget *parent)
        : QDialog(parent)
{
    QPushButton *button3 = new QPushButton(tr("Apps"));
    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->addWidget(button3);
    setLayout(hLayout);
}


MainWindow::MainWindow()
{
mainMenu = new MainMenu;
setCentralWidget(mainMenu);

app = 0;
readSettings();
}


void MainWindow::AppMenu()
{
    app = new AppDialog(this);
    app->show();
}

This opens in a new window with an "App" button in it. Can anyo开发者_开发百科ne let me know if it's possible and how to open the new app dialog in the same window as the original main menu? It should cover the whole window and look like a normal window with the new menu on it. Ideally after this I could add a "back" button of some sort. I guess this is similar to creating a "wizard" type of interface that is used a lot on the installation wizards and stuff like that.

Bryce

EDIT

This is the source code for implementing QStackedWidgets()

MainMenu::MainMenu(QWidget *parent)
        : QDialog(parent)
{
    QStackedLayout *stackedLayout = new QStackedLayout;

    AppDialog *app = new AppDialog;
    progWidget *program = new ProgWidget;

    QStackedWidget *stackedWidget = new QStackedWidget;
    stackedWidget->addWidget(app);
    stackedWidget->addWidget(program);

    stackedWidget->setCurrentIndex(0);

    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->addWidget(stackedWidget);

    setLayout(vLayout);
}

Where would I put the signals and slots to change the index? Imaging that the app and program widgets are just a few widgets with some QPushButtons on them. I can get them to display separately but haven't figure out how to change them yet.


You might want to look at this question. My answer here is the same... use a QStackedWidget as your main widget, and the stuff you want to be different on each page inside it. (If that is the whole dialog, then make the stacked widget cover the whole dialog). Then you can set the current page of the stacked widget based on whatever logic you want to use, such as clicking a button, and it will hide everything from the previous page and show the new current page.

==EDIT==

To clarify based on your example, you could do something like this:

enum Pages { FIRST, SECOND, LAST };

MainMenu::MainMenu(QWidget *parent)
        : QDialog(parent)
{
    // Create widgets to populate the pages
    QWidget* widgets[3];
    widgets[FIRST] = new QWidget;
    widgets[SECOND] = new QWidget;
    widgets[LAST] = new QWidget;

    // Create buttons to navigate between the pages
    QPushButton* buttons[3];
    buttons[FIRST] = new QPushButton(widgets[FIRST]);
    buttons[FIRST]->setText("Next");
    buttons[FIRST]->setSize(100, 100);
    buttons[FIRST]->show();

    buttons[SECOND] = new QPushButton(widgets[SECOND]);
    buttons[SECOND]->setText("Next");
    buttons[SECOND]->setSize(100, 100);
    buttons[SECOND]->show();

    buttons[LAST] = new QPushButton(widgets[LAST]);
    buttons[LAST]->setText("Start Again");
    buttons[LAST]->setSize(100, 100);
    buttons[LAST]->show();

    // Create stacked widget for the pages
    QStackedWidget *stackedWidget = new QStackedWidget;
    stackedWidget->addWidget(widgets[FIRST]);
    stackedWidget->addWidget(widgets[SECOND]);
    stackedWidget->addWidget(widgets[LAST]);

    stackedWidget->setCurrentIndex(0);

    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->addWidget(stackedWidget);

    setLayout(vLayout);

    // Changes a bunch of signals into one signal with an index.
    QSignalMapper *mapper(this);
    for(int i = 0; i < 3; ++i)
    {
        // Each button maps to the next id, with the last one wrapping around.
        mapper->setMapping(buttons[i], (i + 1) % 3);
        // Make the button click connect to the mapper.
        connect(buttons[i], SIGNAL(clicked()), mapper, SLOT(map()));
    }
    // Connect the collected (and slightly transformed) signal to change the page.
    connect(mapper, SIGNAL(mapped(int)), stackedWidget, SLOT(setCurrentIndex(int)));
}


Don't subclass QDialog. Just create a new QWidget, set it up, and replace the widget you want with it. That way a "back" button is trivial: just do another replace.

You could also call QMainWindow::setCentralWidget() with it, if it's appropriate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜