title: Hide or Show QStackedWidget Items in Qt
body: <p>I want to show or hide items in <code>QStackedWidget</code>. When I press Enter button it should show a stacked element and when I press say a left button it should hide.
  I use <code>QStackedWidget</code> and <code>QListWidget</code>. My code:</p>
  
  <p><code>mymainwindow.h</code>:</p>
  
  <pre><code>#ifndef MYMAINWINDOW_H
  #define MYMAINWINDOW_H
  
  class mymainwindow : public QMainWindow
  {
      Q_OBJECT
  public:
      mymainwindow();
  
  protected:
    void keyPressEvent(QKeyEvent *event);
  
  private:
    QStackedWidget *stack;
    QListWidget *list;
    QVBoxLayout *vertical;
    QWidget *widget;
  };
  
  #endif
  </code></pre>
  
  <p><code>mymainwindow.cpp</code>:</p>
  
  <pre><code>#include "mymainwindow.h"
  
  mymainwindow::mymainwindow() : QMainWindow()
  {
      stack = new QStackedWidget();
      list = new QListWidget();
  
      stack-&gt;addWidget(new QLineEdit("Hello U have clicked the first menu"));
      stack-&gt;addWidget(new QLineEdit("Second ListWidget Item"));
      stack-&gt;addWidget(new QLineEdit("Last Widget Item"));
  
      widget = new QWidget();
  
      QLabel *label = new QLabel("Main Window");
  
      list-&gt;addItem("New Item 1");
      list-&gt;addItem("New Item 2");
      list-&gt;addItem("New Item 3");
  
      list-&gt;setFixedSize(200,100);
  
      QVBoxLayout *vertical = new QVBoxLayout();
      vertical-&gt;addWidget(label);
      vertical-&gt;addWidget(list);
      vertical-&gt;addWidget(stack);
  
      widget-&gt;setLayout(vertical);
      setCentralWidget(widget);
  }
  
  void mymainwindow::keyPressEvent(QKeyEvent *event)
  {
      switch (event-&gt;key()) {
      case Qt::Key_Down:
          connect(list, SIGNAL(currentRowChanged(int)), stack, SLOT(setCurrentIndex(int)));
          break;
      case Qt::Key_Up:
          connect(list, SIGNAL(currentRowChanged(int)), stack, SLOT(setCurrentIndex(int)));
          break;
      case Qt::Key_Left:
          break;
      }
   }
  </code></pre>