タブインデックスを切り替えるたびに、新しいフォームを作成します。私はこれがあなたの問題の原因であるかどうかはわかりませんが、間違いなく問題です。
フォームが1回だけ作成されるようにする必要があります。たとえば、MainWindowのコンストラクタでそれらを作成し、それらのポインタをメンバ変数に格納することができます。また、必要なレイアウトをすでに割り当てておく必要があります。
インデックスを切り替えると、DisplayHistory()または同等のメソッドを呼び出すだけです。
PS:あなたはまだ来年あなたのコードを理解したい場合、あなたはおそらくもっと言えば、名前を見つける必要がありますここで「」そのスロットの;-)
[編集] は、サンプルヘッダとcppのものよりファイル。それがそのままコンパイルされないかもしれないとアドバイスし、調整が必要な場合がありますが、私はあなたに一般的な考えを示したいと思います。
ヘッダファイル
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
// forward declarations
class CalendarForm;
class HistoryForm;
class StatisticsForm; // or whatever your 3rd form is called
class MainWindow : QMainWindow
{
public:
MainWindow(QWidget *parent);
~MainWindow();
private slots:
void currentTabChanged(int tabIndex);
private:
// these are called "member variables" as they are a member of a class
// Sample naming convention: m_ for members, p for Pointer, you can use
// whatever you prefer, as long as you use it consistently.
CalendarForm* m_pCalendar;
HistoryForm* m_pHistory;
StatisticsForm* m_pStatistics;
// not sure about this part but it is generated by Qt anyway
Ui::MainWindowClass* ui;
};
// not sure about this part but it is generated by Qt anyway
namespace Ui {
class MainWindowClass;
}
#endif // MAINWINDOW_H
CPPファイル:
// these includes should be present already anyway
#include "MainWindow.h"
#include "ui_mainwindow.h"
#include "CalendarForm.h"
#include "HistoryForm.h"
#include "StatisticsForm.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
m_pCalendar = new CalendarForm();
m_pHistory = new HistoryForm();
m_pStatistics = new StatisticsForm();
ui->tabWidget->addTab(m_pCalendar);
ui->tabWidget->addTab(m_pHistory);
ui->tabWidget->addTab(m_pStatistics);
connect(ui->tabWidget, SIGNAL(currentChanged (int)),
this , SLOT (currentTabChanged(int));
}
MainWindow::~MainWindow()
{
delete m_pCalendar;
delete m_pHistory;
delete m_pStatistics;
}
void MainWindow::currentTabChanged(int tabIndex)
{
switch(tabIndex)
{
case 0:
// update calculator if necessary
break;
case 1:
// Assumption: You need to update the view every time you activate
// the tab. If this assumption is wrong, you don't need this slot
// at all, just call the method once in the constructor.
m_pHistory->DisplayHistory();
break;
case 2:
// update statistics if necessary
break;
default:
break;
}
}
:) uは私はコンストラクタでたpHisを作成することを意味しました? – alfah
:)またはグローバルobj? "メンバ変数でそれらへのポインタを格納する"について詳しく説明できますか? P.Sはい私はコードを正しく取得すると変更されます;) – alfah
@alfahサンプルコードを追加しました.-) –