title: QTableWidget functions in a QTabWidget
body: <p>How can I select a QTableWidget cell in a QTabWidget tab? </p>
  
  <p><strong>How I implement:</strong></p>
  
  <p><code>Wbook</code> and <code>Ssheet</code> represent a workbook and spreadsheet. I implement them under <code>ExcelMockWidget</code> container class. I can’t figure out how to access my <code>Ssheet</code> cells once they have been added to a <code>Wbook</code> tab.  I do understand <code>Ssheet</code> is cast into a <code>QWidget</code> once I insert into my <code>Wbook</code>, but would that matter?</p>
  
  <p>Here is my repository for the <a href="https://github.com/CCi-BClark/MockExcel" rel="nofollow">ExcelView Example</a>.</p>
  
  <hr>
  
  <p>Here is my classes:</p>
  
  <p>ExcelMockWidget:</p>
  
  <pre><code>class ExcelMockWidget : public QWidget{
      Q_OBJECT
  public:
      explicit ExcelMockWidget(QWidget *parent = 0);
      ~ExcelMockWidget();
      void addCell(int row, int column, QVariant cell);
      void addSheet(int index, QString title);
      void setSheetProperties(int rows, int columns, QStringList headers);
  signals:
      void indexChanged(int);
  public slots:
      void emitIndexChange(int index);
      void select(int row, int column);
  
  private:
      Wbook *book;
      Ssheet *sheet;
  };
  
  ExcelMockWidget::ExcelMockWidget(QWidget *parent) : QWidget(parent) {
      book = new Wbook;
      QVBoxLayout *container;
      container = new QVBoxLayout;
      container-&gt;addWidget(book);
      setLayout(container);
      connect(book,SIGNAL(changedIndex(int)),this,SLOT(emitIndexChange(int)));
  }
  
  ExcelMockWidget::~ExcelMockWidget(){
  
  }
  
  void ExcelMockWidget::addCell(int row, int column, QVariant cell){
      sheet-&gt;setCell(row,column,cell);
  }
  
  void ExcelMockWidget::addSheet(int index, QString title){
      book-&gt;createTab(index, title, sheet);
  }
  
  void ExcelMockWidget::setSheetProperties(int rows, int columns, QStringList headers){
      sheet = new Ssheet;
      sheet-&gt;setSheetColumnSpan(columns);
      sheet-&gt;setSheetRowSpan(rows);
      sheet-&gt;setSheetHeaders(headers);
  }
  
  void ExcelMockWidget::emitIndexChange(int index){
      emit indexChanged(index);
  }
  
  void ExcelMockWidget::select(int row, int column){
      book-&gt;selectCell(row, column);
  }
  </code></pre>
  
  <p>Wbook:</p>
  
  <pre><code>namespace Ui {
  class Wbook;
  }
  
  class Wbook : public QWidget{
      Q_OBJECT
  
  public:
      explicit Wbook(QWidget *parent = 0);
      ~Wbook();    
      void createTab(int index, QString title, Ssheet *worksheet);
      void selectTab(int tab);
      void selectCell(int row, int column);
  signals:
      void changedIndex(int);
  public slots:
      void emitIndexChange(int index);
  
  protected:
  
  private:
      Ui::Wbook *ui;
  };
  Wbook::Wbook(QWidget *parent) : QWidget(parent), ui(new Ui::Wbook){
      ui-&gt;setupUi(this);
      connect(ui-&gt;tabWidget,SIGNAL(currentChanged(int)),this,SLOT(emitIndexChange(int)));
  }
  
  Wbook::~Wbook(){
      delete ui;
  }
  
  void Wbook::createTab(int index, QString title, Ssheet *worksheet){
      ui-&gt;tabWidget-&gt;insertTab(index, worksheet, title);
  }
  
  void Wbook::selectTab(int tab){
      // 0 for no selection and 1 -&gt; for created tabs.
      ui-&gt;tabWidget-&gt;setCurrentIndex(tab);
  }
  
  void Wbook::selectCell(int row, int column){
      ui-&gt;tabWidget-&gt;currentWidget()-&gt;childAt(row+1,column+1)-&gt;setFocus();
  }
  
  void Wbook::emitIndexChange(int index){
      emit changedIndex(index);
  }
  </code></pre>
  
  <p>Ssheet:</p>
  
  <pre><code>namespace Ui {
  class Ssheet;
  }
  
  class Ssheet : public QWidget{
      Q_OBJECT
  public:
      explicit Ssheet(QWidget *parent = 0);
      ~Ssheet();
      void setSheetTitle(QString title);
      void setSheetRowSpan(int n);
      void setSheetColumnSpan(int n);
      void setSheetHeaders(QStringList headers);
      void setCell(int row, int column, QVariant cell);
  
      void selectRow(int row);
      void selectCell(int row, int column);
  private:
      Ui::Ssheet *ui;
  };
  
  Ssheet::Ssheet(QWidget *parent) : QWidget(parent), ui(new Ui::Ssheet){
      ui-&gt;setupUi(this);
  
      ui-&gt;table-&gt;verticalHeader()-&gt;setVisible(false);
  
      ui-&gt;table-&gt;setEditTriggers(QAbstractItemView::NoEditTriggers);
      ui-&gt;table-&gt;setSelectionBehavior(QAbstractItemView::SelectItems);
      ui-&gt;table-&gt;setSelectionMode(QAbstractItemView::SingleSelection);
      ui-&gt;table-&gt;setStyleSheet("QTableWidget {selection-background-color: black; selection-color: white}");
  
      ui-&gt;table-&gt;setShowGrid(false);
  }
  
  Ssheet::~Ssheet(){
      delete ui;
  }
  
  void Ssheet::setSheetTitle(QString title){
      ui-&gt;table-&gt;setObjectName(title);
  }
  
  void Ssheet::setSheetRowSpan(int n){
      ui-&gt;table-&gt;setRowCount(n);
  }
  
  void Ssheet::setSheetColumnSpan(int n){
      ui-&gt;table-&gt;setColumnCount(n);
  }
  
  void Ssheet::setSheetHeaders(QStringList headers){
      ui-&gt;table-&gt;setHorizontalHeaderLabels(headers);
  }
  
  void Ssheet::setCell(int row, int column, QVariant cell){
      QTableWidgetItem *input = new QTableWidgetItem;
      input-&gt;setData(0,cell);
      ui-&gt;table-&gt;setItem(row-1,column,input);
  }
  
  void Ssheet::selectCell(int row, int column){
      ui-&gt;table-&gt;setCurrentCell(row,column);
  }
  </code></pre>