1

I am new to QT Creator. I did create a menu: Login || Open. When login is clicked I would like to see a line edit and a press button. When Open is clicked I would like to see a picture in the window. Can I change the interface of the same window depending on what I click in the menu bar? How can I do that?

GraphicsMuncher
  • 4,583
  • 4
  • 35
  • 50
skywak
  • 137
  • 3
  • 4
  • 10
  • Have you considered [QStackWidget](http://doc.qt.nokia.com/latest/qstackedwidget.html) ? "The QStackedWidget class provides a stack of widgets where only one widget is visible at a time" – Thomas Vincent Jul 19 '11 at 12:49
  • ni i haven't . thx a lot, can you give me a link with some examples? – skywak Jul 19 '11 at 13:32
  • I tried find some to had a link in the first place but didn't find any ... Give it a try it's kind of straight-forward if you follow the Qt spirit (start with the given piece of code in the Qt doc). If you have any trouble don't hesitate to edit your question. – Thomas Vincent Jul 19 '11 at 13:46
  • The more usual design is to show a separate window which displays the controls you use to log in. It's much simpler to do since you can draw the login window using Creator. If you do it the other way you need to write code to create the controls. – Jay Jul 19 '11 at 14:07

2 Answers2

1

I did something similar to this - an app with several major areas, toggled by an icon bar at the top. I used a QStackWidget to stack the different application areas on top of each other, a set of QActions that I created using the designer, and a QActionGroup to implement the toggling. When the actions are marked as "checkable" and grouped in a QActionGroup, theQToolBar only lets one be active at the time.

Here's a simplified extract of my code:

// MyApp.h
#include <QMainWindow>    
class QAction;
class QActionGroup;

namespace Ui {
    class MyApp;
} 

class MyApp: public QMainWindow
{    
    Q_OBJECT

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

public slots:
    void showSection(QAction* a);

private:
    Ui::MyApp *ui;
    QActionGroup* sections;
};

 

//MyApp.cpp
#include "structureapp.h"
#include "ui_structureapp.h"

#include <QActionGroup>

MyApp::MyApp(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MyApp),
    sections(new QActionGroup(this)),
{
    ui->setupUi(this);

    /* Populate section list */

    /* Page indices for the stack widget*/
    ui->actionSectionOne->      setData(0);
    ui->actionSectionTwo->      setData(1);
    ui->actionSectionThree->    setData(2);

    sections->addAction(ui->actionSectionOne);
    sections->addAction(ui->actionSectionTwo);
    sections->addAction(ui->actionSectionThree);


    ui->mainToolBar->addSeparator();

    connect(sections, SIGNAL(triggered(QAction*)), this, SLOT(showSection(QAction*)));

    /* Show the default section */
    ui->actionContentSection->trigger();
}

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

void MyApp::showSection(QAction *a)
{
    ui->mainArea->setCurrentIndex(a->data().toInt());
}
gnud
  • 77,584
  • 5
  • 64
  • 78
0

Yes, you can. As I explained earlier, each menu entry is a signal, and that's connected to a slot. With two different menu entries, you have two signals, and you'd connect them to two different slots. So, you could name your first slot onLogin, and the second slot onOpen. (It helps to choose descriptive names, so you'll understand your program when you come back on mondays).

Now, it the slot onLogin, you put the code for login. In the slot onOpen, you put the other code. But do consider what happens if you click the two menu entries one after another. Should that even be possible? If not, you may need another solution. It's quite common to use a QDialog for a login. When a dialog is active, you can't use the menu of the main application, so you can't accidentily hit onOpen when you're busy with the login.

Community
  • 1
  • 1
MSalters
  • 173,980
  • 10
  • 155
  • 350